18
TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++) QUESTION 1 Write a C++ program using Taylor’s series method to solve the equation =3x+y 2 to approximate y when x=0.1, given that y=1 when x=0. #include <iostream> #include <cmath> #include <iomanip> using namespace std; int main() { double x0=0,y0=1 , h=0.1,y1,y2,y3,y4, y; y1=3*x0 + y0*y0; y2=3+ 2*y0*y1; y3=2*y1*y1 + 2*y0*y2; y4=6*y1*y2 + 2*y0*y3; y= y0+ (y1*h) + (y2*pow(h,2))/2 + (y3*pow(h,3))/6 + (y4*pow(h,4))/24; cout << "The value of y when x=0.1 is " << setprecision(5) <<fixed << y << endl; return 0; } //Output: The value of y when x=0.1 is 1.12722 //Alternative way for question 1 #include <iostream> #include <cmath> #include <iomanip> using namespace std; #define h 0.1 double f(double x,double y) { return 3*x + y*y; } double ff(double x,double y) {

C++ TUTORIAL 9

Embed Size (px)

Citation preview

TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++)

QUESTION 1

Write a C++ program using Taylor’s series method to solve the equation

=3x+y2 to approximate y when x=0.1, given that y=1 when x=0.

#include <iostream>

#include <cmath>

#include <iomanip>

using namespace std;

int main()

{

double x0=0,y0=1 , h=0.1,y1,y2,y3,y4, y;

y1=3*x0 + y0*y0;

y2=3+ 2*y0*y1;

y3=2*y1*y1 + 2*y0*y2;

y4=6*y1*y2 + 2*y0*y3;

y= y0+ (y1*h) + (y2*pow(h,2))/2 + (y3*pow(h,3))/6 + (y4*pow(h,4))/24;

cout << "The value of y when x=0.1 is " << setprecision(5) <<fixed << y << endl;

return 0;

}

//Output:

The value of y when x=0.1 is 1.12722

//Alternative way for question 1

#include <iostream>

#include <cmath>

#include <iomanip>

using namespace std;

#define h 0.1

double f(double x,double y)

{

return 3*x + y*y;

}

double ff(double x,double y)

{

TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++)

return 3 + 2*y*f(x,y);

}

double fff(double x, double y)

{

return 2*f(x,y)*f(x,y) + 2*y*ff(x,y);

}

double ffff(double x, double y)

{

return 6*f(x,y)*ff(x,y) + 2*y*fff(x,y);

}

void taylor(double x,double y[])

{

int i;

cout << "i\t\tx\t\ty " << endl;

for ( i=0;i<=4;i++)

{

y[i+1]=y[i]+h*f(x,y[i] ) + (h*h/2)*ff(x,y[i]) + (pow(h,3)/6)*fff(x,y[i])+

(pow(h,4)/24)*ffff(x,y[i]) ;

cout << i << "\t\t" << setprecision(1)<<fixed << x << "\t\t" << setprecision(5) <<

fixed << y[i] << endl;

x= x+ h;

}

}

int main()

{

double x0,y[100];

cout << "Enter x0 and y0 : ";

cin >> x0 >> y[0];

taylor(x0,y);

return 0;

}

//Output:

Enter x0 and y0 : 0 1

i x y

0 0.0 1.00000

1 0.1 1.12722

2 0.2 1.32071

TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++)

QUESTION 2

Write a C++ program using Taylor’s series method to solve

+4y=x2 ,

y(0)=1 to approximate y(0.2).

#include <iostream>

#include <cmath>

#include <iomanip>

using namespace std;

int main()

{

double x0=0,y0=1 , h=0.2,y1,y2,y3,y4, y;

y1= x0*x0 -4*y0;

y2= 2*x0- 4*y1;

y3= 2- 8*x0+16*y1; // y3= 2- 4*y2;

y4= -8+32*x0 -64*y1; // y4= -4*y3;

y= y0+ (y1*h) + (y2*pow(h,2))/2 + (y3*pow(h,3))/6 + (y4*pow(h,4))/24;

cout << "The value of y when x=0.2 is " << setprecision(5) <<fixed << y << endl;

return 0;

}

//Output:

The value of y when x=0.2 is 0.45387

TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++)

//Alternative for question 2

#include <iostream>

#include <cmath>

#include <iomanip>

using namespace std;

#define h 0.2

double f(double x,double y)

{

return x*x - 4*y;

}

double ff(double x,double y)

{

return 2*x - 4*f(x,y);

}

double fff(double x, double y)

{

return 2- 8*x + 16*f(x,y);

}

double ffff(double x, double y)

{

return -8 + 32*x- 64*f(x,y);

}

void taylor(double x,double y[])

{

int i;

TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++)

cout << "i\t\tx\t\ty " << endl;

for ( i=0;i<=2;i++)

{

y[i+1]=y[i]+h*f(x,y[i] ) + (h*h/2)*ff(x,y[i]) + (pow(h,3)/6)*fff(x,y[i])+

(pow(h,4)/24)*ffff(x,y[i]) ;

cout << i << "\t\t" << setprecision(1)<<fixed << x << "\t\t" << setprecision(5) << fixed

<< y[i] << endl;

x= x+ h;

}

}

int main()

{

double x0,y[100];

cout << "Enter x0 and y0 : ";

cin >> x0 >> y[0];

taylor(x0,y);

return 0;

}

//Output:

Enter x0 and y0 : 0 1

i x y

0 0.0 1.00000

1 0.2 0.45387

2 0.4 0.21894

TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++)

QUESTION 3

Write a C++ program to solve

= -xy2 , 𝑦(2)=1 in the interval 2< x <3

with h=0.1 using Euler’s method. Compare the results with exact solution from 𝑦= 2/(x2 -2)

#include<iostream>

#include <cmath>

#include<iomanip>

using namespace std;

#define F(x,y) -x*y*y

void main()

{

double y0,y,x,x0,xn,h, exact,error;

cout <<"Enter the value of range(x0 and xn): ";

cin >> x0 >> xn;

cout << "Enter the value of y0: ";

cin >> y0;

cout <<"Enter the h: ";

cin >> h;

cout << "\n\nx0= "<< x0 << "\t\t" << "y0= " << y0;

x=x0;

y=y0;

while(x<xn)

{

TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++)

y= y + h * F(x,y);

x=x+h;

exact = 2/(x*x-2);

error= exact-y;

cout << "\nx= " << setprecision(1) <<fixed << x << "\t";

cout << setprecision(4) <<fixed << exact<< "\t" <<setprecision(4) <<fixed

<< y << "\t";

cout <<setprecision(4) << fixed << fabs(error) << endl;

}

}

//Output:

Enter the value of range(x0 and xn): 2 3

Enter the value of y0: 1

Enter the h: 0.1

x0= 2 y0= 1

x= 2.1 0.8299 0.8000 0.0299

x= 2.2 0.7042 0.6656 0.0386

x= 2.3 0.6079 0.5681 0.0398

x= 2.4 0.5319 0.4939 0.0380

x= 2.5 0.4706 0.4354 0.0352

x= 2.6 0.4202 0.3880 0.0322

x= 2.7 0.3781 0.3488 0.0292

TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++)

x= 2.8 0.3425 0.3160 0.0265

x= 2.9 0.3120 0.2880 0.0240

x= 3.0 0.2857 0.2640 0.0217

//Alternative way for question no 3

#include<iostream>

#include<cmath>

#include<iomanip>

using namespace std;

//Given dy/dx

float f(float(x),float(y))

{

return (-x*y*y);

}

int main()

{

double y[100],x[100], exact,error,percent_error;

int n,i;

float h;

//Entering the initial values of x & y

cout<<"Enter the value of x0: ";

cin>>x[0];

cout<<"Enter The Value of y0: ";

cin>>y[0];

TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++)

cout<<"Enter the number of Iterations: ";

cin>>n;

cout<<"Enter The Value of Step Size: ";

cin>>h;

cout<<"\nIterations\tx\ty\tExact value\tError\t Percentage Error"<<endl;

//Calculating the value of x

for(i=1;i<=n;i++)

{

x[i]=x[i-1]+h;

}

//Calculating the value of y

for(i=1;i<=n;i++)

{

y[i]=y[i-1]+(h*f(x[i-1],y[i-1]));

}

//Printing result

for(i=0;i<=n;i++)

{

exact = 2/(x[i]*x[i]-2);

error= exact-y[i];

percent_error= (fabs(error)/exact)*100;

cout << i<<"\t\t"<< setprecision(2) <<x[i]<<"\t";

cout << setprecision(4)<< y[i] << "\t" << exact << "\t\t" ;

cout << setprecision(4) <<fixed << fabs(error) << "\t ";

TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++)

cout << setprecision(5) << percent_error << endl;

}

return 0;

}

//Output:

Enter the value of x0: 2

Enter The Value of y0: 1

Enter the number of Iterations: 10

Enter The Value of Step Size: 0.1

Iterations x y Exact value Error Percentage Error

0 2 1 1 0.0000 0.00000

1 2.10 0.8000 0.8299 0.0299 3.60000

2 2.20 0.6656 0.7042 0.0386 5.48480

3 2.30 0.5681 0.6079 0.0398 6.54182

4 2.40 0.4939 0.5319 0.0380 7.14753

5 2.50 0.4354 0.4706 0.0352 7.48768

6 2.60 0.3880 0.4202 0.0322 7.66332

7 2.70 0.3488 0.3781 0.0292 7.73341

8 2.80 0.3160 0.3425 0.0265 7.73413

9 2.90 0.2880 0.3120 0.0240 7.68862

10 3.00 0.2640 0.2857 0.0217 7.61210

TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++)

QUESTION 4

Write a C++ program to find y(1) from

= x+y , y(0)=1 using Euler’s

method by using h=0.2. The exact solution is y=-1-x+2ex

#include<iostream>

#include<cmath>

#include<iomanip>

using namespace std;

//Given dy/dx

float f(float(x),float(y))

{

return (x+y);

}

int main()

{

double y[100],x[100], exact,error,percent_error;

int n,i;

float h;

//Entering the initial values of x & y

cout<<"Enter the value of x0: ";

cin>>x[0];

cout<<"Enter The Value of y0: ";

cin>>y[0];

cout<<"Enter the number of Iterations: ";

TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++)

cin>>n;

cout<<"Enter The Value of Step Size: ";

cin>>h;

cout<<"\nIterations\tx\ty\tExact value\tError\t Percentage Error"<<endl;

//Calculating the value of x

for(i=1;i<=n;i++)

{

x[i]=x[i-1]+h;

}

//Calculating the value of y

for(i=1;i<=n;i++)

{

y[i]=y[i-1]+(h*f(x[i-1],y[i-1]));

}

//Printing result

for(i=0;i<=n;i++)

{

exact = -1-x[i] +2*exp(x[i]);

error= exact-y[i];

percent_error= (fabs(error)/exact)*100;

cout << i<<"\t\t"<< setprecision(1) <<x[i]<<"\t";

cout << setprecision(4)<< y[i] << "\t" << exact << "\t\t" ;

TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++)

cout << setprecision(4) <<fixed << fabs(error) << "\t ";

cout << setprecision(5) << percent_error << endl;

}

return 0;

}

//Output:

Enter the value of x0: 0

Enter The Value of y0: 1

Enter the number of Iterations: 5

Enter The Value of Step Size: 0.2

Iterations x y Exact value Error Percentage Error

0 0 1 1 0.0000 0.00000

1 0.2 1.2000 1.2428 0.0428 3.44427

2 0.4 1.4800 1.5836 0.1036 6.54497

3 0.6 1.8560 2.0442 0.1882 9.20821

4 0.8 2.3472 2.6511 0.3039 11.46256

5 1.0 2.9766 3.4366 0.4599 13.38324

QUESTION 5

Write a C++ program to solve

= -2xy2, y(0)=1 in the interval

0≤ x ≤0.5 with h=0.1 using Euler’s method. The exact value is

y= 1/(x2+1)

#include<iostream>

#include<cmath>

#include<iomanip>

TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++)

using namespace std;

//Given dy/dx

float f(float(x),float(y))

{

return (-2*x*y*y);

}

int main()

{

double y[100],x[100], exact,error,percent_error;

int n,i;

float h;

//Entering the initial values of x & y

cout<<"Enter the value of x0: ";

cin>>x[0];

cout<<"Enter The Value of y0: ";

cin>>y[0];

cout<<"Enter the number of Iterations: ";

cin>>n;

cout<<"Enter The Value of Step Size: ";

cin>>h;

cout<<"\nIterations\tx\ty\tExact value\tError\t Percentage Error"<<endl;

TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++)

//Calculating the value of x

for(i=1;i<=n;i++)

{

x[i]=x[i-1]+h;

}

//Calculating the value of y

for(i=1;i<=n;i++)

{

y[i]=y[i-1]+(h*f(x[i-1],y[i-1]));

}

//Printing result

for(i=0;i<=n;i++)

{

exact = (1/(x[i]*x[i]+1));

error= exact-y[i];

percent_error= (fabs(error)/exact)*100;

cout << i<<"\t\t"<< setprecision(1) <<x[i]<<"\t";

cout << setprecision(4)<< y[i] << "\t" << exact << "\t\t" ;

cout << setprecision(4) <<fixed << fabs(error) << "\t ";

cout << setprecision(5) << percent_error << endl;

}

return 0;

}

TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++)

//Output :

Enter the value of x0: 0

Enter The Value of y0: 1

Enter the number of Iterations: 5

Enter The Value of Step Size: 0.1

Iterations x y Exact value Error Percentage Error

0 0 1 1 0.0000 0.00000

1 0.1 1.0000 0.9901 0.0099 1.00000

2 0.2 0.9800 0.9615 0.0185 1.92000

3 0.3 0.9416 0.9174 0.0242 2.63266

4 0.4 0.8884 0.8621 0.0263 3.05314

5 0.5 0.8253 0.8000 0.0253 3.15629

QUESTION 6

Write a C++ program to solve

= x+y2 with y(1)=0 at x=1.3 using

Euler’s Method

#include<iostream>

#include<cmath>

#include<iomanip>

using namespace std;

//Given dy/dx

float f(float(x),float(y))

{

return (x+ y*y);

}

TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++)

int main()

{

double y[100],x[100];

int n,i;

float h;

//Entering the initial values of x & y

cout<<"Enter the value of x0: ";

cin>>x[0];

cout<<"Enter The Value of y0: ";

cin>>y[0];

cout<<"Enter the number of Iterations: ";

cin>>n;

cout<<"Enter The Value of Step Size: ";

cin>>h;

cout<<"\nIterations\tx\ty"<<endl;

//Calculating the value of x

for(i=1;i<=n;i++)

{

x[i]=x[i-1]+h;

}

//Calculating the value of y

for(i=1;i<=n;i++)

{

TUTORIAL 9; SJEM2231: STRUCTURED PROGRAMMING (C++)

y[i]=y[i-1]+(h*f(x[i-1],y[i-1]));

}

//Printing result

for(i=0;i<=n;i++)

{

cout << i<<"\t\t"<< setprecision(2) <<x[i]<< "\t" << setprecision(5)<< y[i] <<

endl;

}

return 0;

}

//Output:

Enter the value of x0: 1

Enter The Value of y0: 0

Enter the number of Iterations: 3

Enter The Value of Step Size: 0.1

Iterations x y

0 1 0

1 1.1 0.1

2 1.2 0.211

3 1.3 0.33545