12
TUTORIAL 10 SJEM2231: STRUCTURED PROGRAMMING (C++) QUESTION 1 Write a C++ program using second order RK method with h=0.1 to find y(2.5) for = -xy 2 , y(2)=1 #include<iostream> #include<cmath> #include<iomanip> using namespace std; double f(double x,double y) { double func; func= -x*y*y; return func; } void main() { float x,x0,xn,y,y0, h,exact,error,k1,k2; cout << "Enter the stepsize h: "; cin >> h; cout << "Enter x0,xn and y0: "; cin >> x0 >> xn >> y0; x=x0; y=y0; cout << "\nX\tY\t\tExact\t\tError\n";

C++ TUTORIAL 10

Embed Size (px)

Citation preview

Page 1: C++ TUTORIAL 10

TUTORIAL 10 SJEM2231: STRUCTURED PROGRAMMING (C++)

QUESTION 1

Write a C++ program using second order RK method with h=0.1 to find

y(2.5) for

= -xy2, y(2)=1

#include<iostream>

#include<cmath>

#include<iomanip>

using namespace std;

double f(double x,double y)

{

double func;

func= -x*y*y;

return func;

}

void main()

{

float x,x0,xn,y,y0, h,exact,error,k1,k2;

cout << "Enter the stepsize h: ";

cin >> h;

cout << "Enter x0,xn and y0: ";

cin >> x0 >> xn >> y0;

x=x0;

y=y0;

cout << "\nX\tY\t\tExact\t\tError\n";

Page 2: C++ TUTORIAL 10

while(x < xn)

{

k1=h*f(x,y);

k2=h*f((x+h/2),(y + k1/2));

y= y+ k2;

x=x+h;

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

error= exact-y;

cout << setprecision(1) <<fixed << x << "\t"<< setprecision(6) << fixed << y << "\t";

cout << setprecision(6) << exact << "\t" << fabs(error) << endl;

}

}

//Output:

Enter the stepsize h: 0.1

Enter x0,xn and y0: 2 2.5 1

X Y Exact Error

2.1 0.833950 0.829876 0.004074

2.2 0.709463 0.704225 0.005238

2.3 0.613199 0.607903 0.005296

2.4 0.536859 0.531915 0.004944

2.5 0.475051 0.470588 0.004462

2.6 0.424136 0.420168 0.003967

Page 3: C++ TUTORIAL 10

TUTORIAL 10 SJEM2231: STRUCTURED PROGRAMMING (C++)

QUESTION 2

Write a C++ function program using RK2 method to solve

= sin(y) ,

with y(0)=1 from x=0 to 0.5 with step size h=0.1.

#include<iostream>

#include<cmath>

#include<iomanip>

using namespace std;

double f(double(x),double(y))

{

return sin(y);

}

int main()

{

long double y[100],x[100], k[100][100];

int n,i;

float h;

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;

Page 4: C++ TUTORIAL 10

cout<<"\nIterations\tx\ty\tK1\t\tK2"<<endl;

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

{

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

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

y[i]=y[i-1]+ k[2][i];

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

}

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

{

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

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

cout << setprecision(3)<< k[1][i] << "\t\t" << k[2][i] << 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.1

Iterations x y K1 K2

0 0 1 0.0841 0.0863

1 0.1 1.0863 0.0885 0.0905

2 0.2 1.1768 0.0923 0.094

Page 5: C++ TUTORIAL 10

TUTORIAL 10 SJEM2231: STRUCTURED PROGRAMMING (C++)

3 0.3 1.2708 0.0955 0.0968

4 0.4 1.3677 0.0979 0.0988

5 0.5 1.4665

//Alternative way for question 2

#include<iostream>

#include<cmath>

using namespace std;

double f(double x,double y)

{

double func;

func= sin(y);

return func;

}

void main()

{

float x,x0,xn,y,y0,n, h,k1,k2;

cout << "Enter no. of subintervals: ";

cin >> n;

cout << "Enter x0,xn and y0: ";

cin >> x0 >> xn >> y0;

h=(xn-x0)/n ;

Page 6: C++ TUTORIAL 10

x=x0;

y=y0;

cout << "\nX\t\tY\n";

while(x<xn)

{

k1=h*f(x,y);

k2=h*f((x+h/2),(y + k1/2));

y= y+ k2;

x=x+h;

cout << x << "\t\t"<< y <<endl;

}

}

//Output

Enter no. of subintervals: 5

Enter x0,xn and y0: 0 0.5 1

X Y

0.1 1.08635

0.2 1.17681

0.3 1.27082

0.4 1.36766

0.5 1.46647

Page 7: C++ TUTORIAL 10

TUTORIAL 10 SJEM2231: STRUCTURED PROGRAMMING (C++)

QUESTION 3

Write a C++ program using fourth order RK method with h=0.1 to

approximate y(1.5) for

= 2xy, y(1)=1. Compute the errors using exact

solution, y=ex2-1

#include<iostream>

#include<cmath>

#include<iomanip>

using namespace std;

double f(double x,double y)

{

double func;

func= 2*x*y;

return func;

}

void main()

{

double x,x0,xn,y,y0, exact,error, h,k1,k2,k3,k4;

cout << "Enter the stepsize h: ";

cin >> h;

cout << "Enter x0,xn and y0: ";

cin >> x0 >> xn >> y0;

x=x0;

y=y0;

cout << "\nX\tY\tExact\tError\n";

Page 8: C++ TUTORIAL 10

while(x <xn)

{

k1=f(x,y);

k2=f((x+h/2),(y + k1*h/2));

k3=f((x+ h/2) ,(y+k2*h/2));

k4=f(x+h, y+h*k3);

y= y+ (h/6)*(k1+2*k2+2*k3+k4);

x=x+h;

exact=exp(x*x -1);

error= exact - y;

cout << setprecision(1) << fixed << x << "\t"<< setprecision(4) <<

fixed << y <<"\t" ;

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

}

}

//Output:

Enter the stepsize h: 0.1

Enter x0,xn and y0: 1 1.5 1

X Y Exact Error

1.1 1.2337 1.2337 0.0000

1.2 1.5527 1.5527 0.0000

1.3 1.9937 1.9937 0.0000

1.4 2.6116 2.6117 0.0001

1.5 3.4902 3.4903 0.0001

Page 9: C++ TUTORIAL 10

TUTORIAL 10 SJEM2231: STRUCTURED PROGRAMMING (C++)

QUESTION 4

Do Problem 1 with RK4. Compare the results between RK2 and RK4.

#include<iostream>

#include<cmath>

#include<iomanip>

using namespace std;

double f(double x,double y)

{

double func;

func= -x*y*y;

return func;

}

void main()

{

double x,x0,xn,y,y0,h,exact,error,k1,k2,k3,k4;

cout << "Enter the stepsize h: ";

cin >> h;

cout << "Enter x0,xn and y0: ";

cin >> x0 >> xn >> y0;

x=x0;

y=y0;

cout << "\nX\tY\tExact\t\tError\n";

while(x <xn)

{

Page 10: C++ TUTORIAL 10

k1=f(x,y);

k2=f((x+h/2),(y + k1*h/2));

k3=f((x+ h/2) ,(y+k2*h/2));

k4=f(x+h, y+h*k3);

y= y+ (h/6)*(k1+2*k2+2*k3+k4);

x=x+h;

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

error= exact-y;

cout << setprecision(1) << fixed << x << "\t"<< setprecision(6) <<

fixed << y << "\t";

cout <<setprecision(6) << exact << "\t" << fabs(error) << endl;

}

}

//Output:

Enter the stepsize h: 0.1

Enter x0,xn and y0: 2 2.5 1

X Y Exact Error

2.1 0.829885 0.829876 0.000010

2.2 0.704237 0.704225 0.000011

2.3 0.607914 0.607903 0.000011

2.4 0.531924 0.531915 0.000010

2.5 0.470596 0.470588 0.000008

Thus, RK4 is better than RK2 since RK4 has smaller error difference than RK2 for y(2.5)

Page 11: C++ TUTORIAL 10

TUTORIAL 10 SJEM2231: STRUCTURED PROGRAMMING (C++)

QUESTION 5

Write a C++ function program using classical RK4 method with h=0.2 to

obtain y(1) for

= y-x, y(0)=2.

#include<iostream>

#include<cmath>

#include<iomanip>

using namespace std;

double f(double x,double y)

{

double func;

func= y-x;

return func;

}

void main()

{

double x,x0,xn,y,y0,h,k1,k2,k3,k4;

cout << "Enter the stepsize h: ";

cin >> h;

cout << "Enter x0,xn and y0: ";

cin >> x0 >> xn >> y0;

x=x0;

y=y0;

cout << "\nX\tY\n";

while(x <xn)

{

k1=f(x,y);

Page 12: C++ TUTORIAL 10

k2=f((x+h/2),(y + k1*h/2));

k3=f((x+ h/2) ,(y+k2*h/2));

k4=f(x+h, y+h*k3);

y= y+ (h/6)*(k1+2*k2+2*k3+k4);

x=x+h;

cout << setprecision(1) << fixed << x << "\t"<< setprecision(6) <<

fixed << y << endl;

}

}

//Output:

Enter the stepsize h: 0.2

Enter x0,xn and y0: 0 1 2

X Y

0.2 2.421400

0.4 2.891818

0.6 3.422106

0.8 4.025521

1.0 4.718251