Vous êtes sur la page 1sur 60

Numerical Methods Algorithm in C++

The scope of these lectures is to study the numerical methods frequently used in Physics and to write (where possible) their C++ codes. The main topics include. The Solution of Nonlinear Equations f(x) = 0
o Bisection Method o Newton Raphson Method o Secant Method

The Solution of Linear Systems AX = B


o Gauss Jordan Method o Cramers Rule o Jacobi Method o Gauss Seidel iterative Method o Exercise

Numerical Integration
o Riemanns Sum o Trapezoidal Rule o Simpsons 1/3 Rule o Simpsons 3/8 Rule o Monte Carlo -1 Method o Monte Carlo -2 Method

Ordinary Differential Equations


o Euler Method o Euler Improved Method o Runge Kutta 4th order Method o Runge Kutta Fehlberg Method

The Solution of Nonlinear Equations f(x) = 0


Bisection Method or interval halving method The bisection method is one of the bracketing methods for finding roots of equations. Given a function f(x)=0 and an interval which might contain a root, perform a predetermined number of iterations using the bisection method. The interval must be large enough to avoid discontinuity. The method works like this if [a, b] is the interval in which the root of f(x) lies then we compute f(a) and f(b). Now we compute f(x) at M=a+b/2 and then calculate f(M). Now multiply f(a) with f(M) if the answer of this is negative than the root lies in [a, M] if not (i.e. positive) then root lies in [M, b]. Example: Consider the non-linear equation f(x) = x3+x2-3x-3=0 find the root in the interval [1, 2] Sol:Now And f(1) = -4 f(2) = 3

Sign change between [1, 2] guarantees a root in this interval. Now consider mid point i.e. M = 1.5 than f(1.5) = -1.475 And f(1)f(1.5) = +ive value Hence we take the interval [1.5, 2] and repeat the process again and again till we find that x = 1.731925 is a root to the above equation up to 6 decimal places.

C++ program for the above example #include<iostream.h> #include<conio.h> #include<math.h> #include<fstream.h> double f(double x) { double value; value=pow(x,3)+pow(x,2)-3*x-3; return value; } void main() { clrscr(); double a,b,M,ans,comp; int i; ofstream res("bisectionresults.txt"); cout<<"Enter the values of a and b:\n"; cin>>a>>b; for(i=0;i<100;i++) { cout<<"f("<<a<<") = "<<f(a)<<endl; res<<"f("<<a<<") = "<<f(a)<<endl; cout<<"f("<<b<<") = "<<f(b)<<endl; res<<"f("<<b<<") = "<<f(b)<<endl; M=(a+b)/2.0;

cout<<"f("<<M<<") = "<<f(M)<<endl; cout<<"_____________________________________________\n"; res<<"f("<<M<<") = "<<f(M)<<endl; res<<"_____________________________________________\n"; comp=f(a)*f(M); if(comp<0) b=M; else a=M; ans=M; } cout<<"\n\nThe root of the equation is: "<<ans; res<<"\n\nThe root of the equation is: "<<ans; getch(); } Use the following functions also and verify the Answers given with each 1- f(x) = ex-3x=0, a=1, b=2, Ans: 1.512 2- f(x) = e-x-sin(x/2), a=0, b=1, Ans: 0.445.

Newton Raphson Method The method states that if xn is an approximate to an exact root of the equation f(x) = 0 then a better approximation is in general given by xn+1 where xn+1 = xn - f(xn)/f(xn) Example illustrates the method Example: Using Newton Raphson method find the solution of e-x - sinx = 0 correct up to four decimal places near x0 = 0.5. Sol: f(x) = e-x sinx hence f(x) = -(e-x + cosx) Now xn+1 = xn - e-x sinx /-(e-x + cosx) for n = 0 for n = 1 for n = 2 x1 = 0.5885 x2 = 0.586 x3 = 0.5885

hence x = 0.5885 is the root of the equation.

C++ program for the above example #include<iostream.h> #include<conio.h> #include<math.h> #include<fstream.h> double f(double x) { double value; value=1.0/exp(x)-sin(x); return value; } double df(double x) { double value; value=-(1.0/exp(x)+cos(x)); return value; } void main() { clrscr(); double init,Xn1,Xn,ans; int i; ofstream res("New_Raph.txt"); cout<<"Enter the values of intial point:\n";

cin>>init; Xn=init; for(i=1;i<=100;i++) { Xn1=Xn-(f(Xn)/df(Xn)); cout<<"X"<<i<<": "<<Xn1<<endl; res<<"X"<<i<<": "<<Xn1<<endl; cout<<"____________________________________________\n"; res<<"_____________________________________________\n"; if((Xn1-Xn)<0.0001) { ans=Xn1; break; } else Xn=Xn1; } cout<<"\n\nThe root of the equation is: "<<ans; res<<"\n\nThe root of the equation is: "<<ans; getch(); } Use the following functions also and verify the Answers given with each 1- f(x) = x3-3x-3, at x=2: Ans = 2.1038.. 2- f(x) = ex 3x, at x = 0: Ans = 0.62

Secant Method The basic disadvantage with Newton Raphson method is that it needs to evaluate the derivative of f(x). This can be avoided by using the definition of derivatives. That is we can replace f(x) with the gradient of the secant of f(x) defined as f'(xn) ~= f(xn) - f(xn-1) / xn xn-1 putting this in xn+1 = xn - f(xn)/f(xn) we get xn+1 = xn f(xn) (xn xn-1) / f(xn) - f(xn-1) on solving we get

xn+1 = xn-1 f(xn) xn f(xn-1) / f(xn) f(xn-1) this is known as secant formula. Note that this formula requires two values namely x0 and x1, between whom the root lays, for the evaluation of the formula. Example: Use the secant method to estimate the root of the equation f(x) = sinx 5x + 2 = 0, given that x0 = 0.4 and x1 = 0.6. Sol: For n = 1 the formula becomes x2 = x0 f(x1) x1 f(x0) / f(x1) f(x0) putting values and solving we find x2 = 0.494, x3 = 0.4950 and x4 = 0.4950. hence 0.4950 is the root of the equation.

C++ program for the above example #include<iostream.h> #include<conio.h> #include<math.h> #include<fstream.h> double f(double x) { double value; value=sin(x)-5*x+2; return value; } void main() { clrscr(); double init,final,Xn1,Xn,Xn_1,ans; int i; ofstream res("Secant.txt"); cout<<"Enter the values of intial and final point:\n"; cin>>init>>final; Xn=init; Xn_1=final; for(i=1;i<=100;i++) { Xn1=(Xn_1*f(Xn)-Xn*f(Xn_1))/(f(Xn)-f(Xn_1)); cout<<"X"<<1+i<<": "<<Xn1<<endl;

res<<"X"<<1+i<<": "<<Xn1<<endl; cout<<"____________________________________________\n"; res<<"_____________________________________________\n"; if((Xn1-Xn)<0.0001) { ans=Xn1; break; } else Xn=Xn1; } cout<<"\n\nThe root of the equation is: "<<ans; res<<"\n\nThe root of the equation is: "<<ans; getch(); } Use the following functions also and verify the Answers given with each 1- f(x) = e-x 3x = 0, x0 = 0.4 and x1 = 0.9, Ans: 0.6192

The Solution of Linear Systems AX = B


Simultaneous linear equations arise in many practical situations. These equations can be solved either by direct method or by iterative methods. We discuss both direct and indirect (iterative) methods. Consider the system of algebraic equations a11x1+ a12x2+ a13x3+. a1nxn = c1 a21x1+ a22x2+ a2nxn = c2 .. .. an1x1+ an2x1+ an3x1+ .. annx1 = cn we can write the above system as AX = C Where A is a11 a21 a12 a22 a13 a1n a23 a1n

an1 And X is x1 x2 . . xn an2 an3 ann And B is b1 b2 . . bn

Gauss Jordan Method In this method we eliminate coefficient of x1 from each equation except from first, then eliminate x2 from second equation and so on for the next equations. i.e. making the diagonal elements of the matrix of the coefficients equal to 1 and all others equal to zero. Example: Solve the following system of equation using Gauss Jordan method 2x1+ 2x2+4x3 = 18 1x1+ 3x2+2x3 = 13 3x1+ 1x2+3x3 = 14 Sol: x1
2 1 3 1 1 3 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0

x2
2 3 1 1 3 1 1 2 -2 1 1 -2 0 1 0 0 1 0 0 1 0

x3
4 2 3 2 2 3 2 0 -3 2 0 -3 2 0 -3 2 0 1 0 0 1

c
18 13 14 9 13 14 9 4 -13 9 2 -13 7 2 -9 7 2 3 1 2 3

Hence x1=1, x2 = 2 and x3 = 3

C++ program for the above example #include<iostream.h> #include<conio.h> #include<stdlib.h> #include<dos.h> #include<fstream.h> void main() { clrscr(); cout<<"Enter the augmented matrix row by row\n\n\n"; cout<<"\nYou should enter (12) numbers.. then press ENTER\n"; float arr[3][4]; float t=1; for(int i=0; i<3 ;i++) //Enering matrix for(int j=0; j<4; j++) cin>>arr[i][j]; clrscr(); //Making 1st element of 1st row 1 by deviding whole row with 1st element //other wise not bothering if (arr[0][0] != 1) { t = (1/(arr[0][0])); arr[0][0] = t * arr[0][0]; } arr[0][1] = t * arr[0][1]; arr[0][2] = t * arr[0][2]; arr[0][3] = t * arr[0][3]; //making mt1 equal to first element of row two float mt1=arr[1][0]; //muliplying first element of row 2 with 1 of 1st row //to make 1st element of 2nd row zero.Applying the same to Others also arr[1][0] = -mt1*arr[0][0]+arr[1][0]; arr[1][1] = -mt1*arr[0][1]+arr[1][1]; arr[1][2] = -mt1*arr[0][2]+arr[1][2]; arr[1][3] = -mt1*arr[0][3]+arr[1][3]; float mt2=arr[2][0];

//muliplying first element of row 3 with 1 of 1st row //to make 1st element of row 3 zero arr[2][0] = -mt2*arr[0][0]+arr[2][0]; arr[2][1] = -mt2*arr[0][1]+arr[2][1]; arr[2][2] = -mt2*arr[0][2]+arr[2][2]; arr[2][3] = -mt2*arr[0][3]+arr[2][3]; //now making 2nd element of 2nd row equal to 1 t=1; if (arr[1][1] != 1) // start second pivot { t = (1/(arr[1][1])); arr[1][1] = t * arr[1][1]; } //no need to multi arr[1][0] with t since it is zero arr[1][2] = t * arr[1][2]; arr[1][3] = t * arr[1][3]; //making 2nd element of 1st row zero float mt3=arr[0][1]; arr[0][1] = -mt3*arr[1][1]+arr[0][1]; arr[0][2] = -mt3*arr[1][2]+arr[0][2]; arr[0][3] = -mt3*arr[1][3]+arr[0][3]; //making second element of 3rd row zero float mt4 = arr[2][1]; arr[2][1] = -mt4*arr[1][1]+arr[2][1]; arr[2][2] = -mt4*arr[1][2]+arr[2][2]; arr[2][3] = -mt4*arr[1][3]+arr[2][3]; t=1; if (arr[2][2] != 1) // start third pivot { t = (1/(arr[2][2])); arr[2][2] = t * arr[2][2]; } //making 3rd element of row three equal to 1 arr[2][3] = t * arr[2][3]; //making remaining elements of row 2 and 3 zero float mt5 = arr[1][2]; arr[1][2] = -mt5*arr[2][2]+arr[1][2]; arr[1][3] = -mt5*arr[2][3]+arr[1][3]; float mt6 = arr[0][2];

arr[0][2] = -mt6*arr[2][2]+arr[0][2]; arr[0][3] = -mt6*arr[2][3]+arr[0][3]; //prining the solved matrix for(i=0; i<3; i++) { cout<<endl; for(j=0; j<4; j++) { cout<<arr[i][j]<<"\t\t"; } cout<<endl; } float x1 = arr[0][3]; float x2 = arr[1][3]; float x3 = arr[2][3]; delay(700); cout<<endl<<"X1 :"<<x1<<endl; delay(700); cout<<endl<<"X2 :"<<x2<<endl; delay(700); cout<<endl<<"X3 :"<<x3<<endl; getch(); ofstream res("GaussJ.txt"); for(i=0; i<3; i++) { res<<endl; for(j=0; j<4; j++) { res<<arr[i][j]<<"\t\t"; } res<<endl; } res<<endl<<"X1 :"<<x1; res<<endl<<"X2 :"<<x2; res<<endl<<"X3 :"<<x3; }

Use the following system also and verify the Answers given with each
1. 2x1+2x2+4x3=18 x1+3x2+2x3= 13 3x1+x2+3x3=14 And: x1=1, x2= 2 and x3= 3 2. 3x1+x2+x3=3 x1-3x2+x3=5 x1+x2+4x3=4 And: x1=1, x2= -1 and x3= 1 3. 2x1+0x2+x3=4 -3x1+4x2-2x3=-3 x1+7x2-5x3=6 And: x1=1.864865, x2= 0.783784 and x3= 0.27027

Cramers Rule It stated that if AX = C is the system of equations and D is the determinant of A, D1 is the determinant of A after replacing the 1st column of A be C, and D2 is the determinant of A after replacing the 2nd column of A by C and D3 is the determinant of A after replacing the 3rd column of A by C, than x1 = D1/D x2 = D2/D x3 = D3/D Example: Find the solution of
3x1+x2+x3=3 x1-3x2+x3=5 x1+x2+4x3=4

Using Cramers Rule Sol: D = -38 D1= -38 D2= 38 D3= 38 Hence x1 = 1 x2 = -1 and x3 = 1

C++ program for the above example #include<iostream.h> #include<conio.h> #include<stdlib.h> #include<dos.h> #include<fstream.h> void main() { clrscr(); cout<<"Enter the augmented matrix row by row\n\n\n"; cout<<"\nYou should enter (12) numbers.. then press ENTER\n"; float a[3][4]; float D,D1,D2,D3; for(int i=0; i<3 ;i++) //Enering matrix for(int j=0; j<4; j++) cin>>a[i][j]; clrscr(); //Finding D,D1,D2,D3 D=a[0][0]*(a[1][1]*a[2][2]-a[1][2]*a[2][1]) -a[0][1]*(a[1][0]*a[2][2]-a[1][2]*a[2][0]) +a[0][2]*(a[1][0]*a[2][1]-a[1][1]*a[2][0]); D1=a[0][3]*(a[1][1]*a[2][2]-a[1][2]*a[2][1]) -a[0][1]*(a[1][3]*a[2][2]-a[1][2]*a[2][3]) +a[0][2]*(a[1][3]*a[2][1]-a[1][1]*a[2][3]); D2=a[0][0]*(a[1][3]*a[2][2]-a[1][2]*a[2][3]) -a[0][3]*(a[1][0]*a[2][2]-a[1][2]*a[2][0]) +a[0][2]*(a[1][0]*a[2][3]-a[1][3]*a[2][0]); D3=a[0][0]*(a[1][1]*a[2][3]-a[1][3]*a[2][1]) -a[0][1]*(a[1][0]*a[2][3]-a[1][3]*a[2][0]) +a[0][3]*(a[1][0]*a[2][1]-a[1][1]*a[2][0]); //Findgin X1,X2 and X3 float x1 = D1/D; float x2 = D2/D;

float x3 = D3/D; delay(700); cout<<endl<<"X1 :"<<x1<<endl; delay(700); cout<<endl<<"X2 :"<<x2<<endl; delay(700); cout<<endl<<"X3 :"<<x3<<endl; getch(); ofstream res("Cramer.txt"); res<<endl<<"X1 :"<<x1; res<<endl<<"X2 :"<<x2; res<<endl<<"X3 :"<<x3; } Use the following system also and verify the Answers given with each
4. 2x1+2x2+4x3=18 x1+3x2+2x3= 13 3x1+x2+3x3=14 And: x1=1, x2= 2 and x3= 3 5. 3x1+x2+x3=3 x1-3x2+x3=5 x1+x2+4x3=4 And: x1=1, x2= -1 and x3= 1 6. 2x1+0x2+x3=4 -3x1+4x2-2x3=-3 x1+7x2-5x3=6 And: x1=1.864865, x2= 0.783784 and x3= 0.27027

Jacobi Method Given a system of linear equations AX = C Where the diagonal elements of A are none zero. The system can be written as ak+1 = Bxk+b where the elements of B are bij = -aij / aii and bii=0 initial guess is x0 = (0,0,0,0,.)T and continue the process of iteration for different values of k, unless we get the required accuracy. For the system a11x1+a12x2+a13x3 = a14, a21x1+a22x2+a23x3 = a24, a31x1+a32x2+a33x3 = a34 The iterations equations can be written as x1k+1 = 1/a11(a14-a12x2k-a13x3k), x2k+1 = 1/a22(a24-a21x1k-a23x3k), x1k+1 = 1/a33(a34-a31x1ka33x3k) Example: Consider the system, 8x+y-z = 8, x-7y+2z = -4, 2x+y+9z =12 Find sol with x,y and z starting at zero. Sol: Setting the equations as x = 1/8(8-y+z) -1, y = 1/7(4+x+2z) -2, z = 1/9(12-2x-y) -3 putting x,y and z = 0, we get x = 1.095, y = 1.095 and z = 1.048 putting these values in 1,2 and 3 and repeating the process enough time we see that the solution converges to the values. x = 1.000 , y = 1.000 and z = 1.000

C++ program for the above example #include<iostream.h> #include<conio.h> #include<stdlib.h> #include<dos.h> #include<fstream.h> void main() { clrscr(); cout<<"Enter the augmented matrix row by row\n\n\n"; cout<<"\nYou should enter (12) numbers.. then press ENTER\n"; float a[3][4]; for(int i=0; i<3 ;i++) //Enering matrix for(int j=0; j<4; j++) cin>>a[i][j]; clrscr(); //performing itterations float X1=0.0,X2=0.0,X3=0.0,X1_it,X2_it,X3_it; for(int k=0;k<=100;k++) { X1_it=1.0/a[0][0]*(a[0][3]-a[0][1]*X2-a[0][2]*X3); X2_it=1.0/a[1][1]*(a[1][3]-a[1][0]*X1-a[1][2]*X3); X3_it=1.0/a[2][2]*(a[2][3]-a[2][0]*X1-a[2][1]*X2); if((X1_it-X1<0.000000001)&&(X2_it-X2<0.000000001)&&(X3_itX3<0.000000001)) { X1=X1_it; X2=X2_it; X3=X3_it; break; } else { X1=X1_it;

X2=X2_it; X3=X3_it; } } delay(700); cout<<endl<<"X1 :"<<X1<<endl; delay(700); cout<<endl<<"X2 :"<<X2<<endl; delay(700); cout<<endl<<"X3 :"<<X3<<endl; getch(); ofstream res("jacobi.txt"); res<<endl<<"X1 :"<<X1; res<<endl<<"X2 :"<<X2; res<<endl<<"X3 :"<<X3; } Verify the convergence of the system 8x+y-z = 8 x-7y+2z = -4 2x+y+9z =12 to x,y,z = 1.0000

Gauss Seidel iterative Method The Jacoby method is difficult to implement because we need the equations to be in such an order that the system becomes diagonally dominant also the convergence is slow. Another method that is also diagonally dominant (i.e. largest values must come in the diagonal elements of matrix A) but converges faster that Jacoby method is Gauss Seidel method. The iterations equations of this method are slightly different from the Jacoby method as given below. Consider the system a11x1+a12x2+a13x3 = a14, a21x1+a22x2+a23x3 = a24, a31x1+a32x2+a33x3 = a34 The iterations equations for Gauss Seidel method are x1k+1 = 1/a11(a14-a12x2k-a13x3k), x2k+1 = 1/a22(a24-a21x1k+1-a23x3k), x1k+1 = 1/a33(a34-a31x1k+1-a33x3k+1) That is in second and third equations the values already calculated are used.

C++ program for the above example #include<iostream.h> #include<conio.h> #include<stdlib.h> #include<dos.h> #include<fstream.h> void main() { clrscr(); cout<<"Enter the augmented matrix row by row\n\n\n"; cout<<"\nYou should enter (12) numbers.. then press ENTER\n"; float a[3][4]; for(int i=0; i<3 ;i++) //Enering matrix for(int j=0; j<4; j++) cin>>a[i][j]; clrscr(); //performing itterations float X1=0.0,X2=0.0,X3=0.0,X1_it,X2_it,X3_it; for(int k=0;k<=100;k++) { X1_it=1.0/a[0][0]*(a[0][3]-a[0][1]*X2-a[0][2]*X3); X2_it=1.0/a[1][1]*(a[1][3]-a[1][0]*X1_it-a[1][2]*X3); X3_it=1.0/a[2][2]*(a[2][3]-a[2][0]*X1_it-a[2][1]*X2_it); if((X1_it-X1<0.000000001)&&(X2_it-X2<0.000000001)&&(X3_itX3<0.000000001)) { X1=X1_it; X2=X2_it; X3=X3_it; break; } else { X1=X1_it;

X2=X2_it; X3=X3_it; } } delay(700); cout<<endl<<"X1 :"<<X1<<endl; delay(700); cout<<endl<<"X2 :"<<X2<<endl; delay(700); cout<<endl<<"X3 :"<<X3<<endl; getch(); ofstream res("Gauss_sei.txt"); res<<endl<<"X1 :"<<X1; res<<endl<<"X2 :"<<X2; res<<endl<<"X3 :"<<X3; } Use the following system and verify the convergence to the given values using above program
1. 8x1+1x2-1x3=8 x1-7x2+2x3= -4 23x1+x2+9x3=12 And: x1=1, x2= 1 and x3= 1 2. 11x1+2x2+x3=15 x1 +10x2+2x3=16 2x1+3x2-8x3=1 And: x1=1.056, x2= 1.364 and x3= 0.651 3. 4x1-2x2+x3=12 2x1+3x2-x3=7 2x1-2x2+2x3=8 And: x1=3.00, x2= 1.00 and x3= 2.00

Excrcise Consider the network as shown below

Applying Kirchoff s loop rule we obtain

Solve the network #2 for the currents battery:

given the following value for the resistors and

Consider the network

Applying Kirchoff s loop rule we obtain

Solve the network #3 for the currents

given the following value for the resistors and batteries:

Numerical integration
Riemanns Sum Let be continuous over the interval , and let

be a partition, then the definite integral is given by


b n

f(x) dx = lim f(xn-1) hn


a 1

Where xn-1 [x0, xn] and the mesh size of the partition goes to zero in the "limit," i.e. h Example: Compute the integral of the function f(x) = x2 over the interval [1, 2] with n=20. Sol: Here x0 = 1, x1 = 1+0.05, x2 = 1.2(0.05),x20 = 2, h = 2-1/20 = 0.05
2

0 as n

Hence x2 dx = f[x0] h+ f[x1] h+ f[x2] h+ f[xn-1] h+ f[xn] h


1

= 2.25875 Exact answer is = 2.3333333333

C++ program for the above example #include<iostream.h> #include<conio.h> #include<math.h> #include<dos.h> #include<fstream.h> float f(float x) { float value; value=pow(x,2); return value; } void main() { clrscr(); int n; float a,b,h,Fx,sum=0.0; ofstream res("Riemann.txt"); cout<<"\nEnter the values of a, b, and n\n"; cin>>a>>b>>n; h=(b-a)/n; clrscr(); //performing itterations

cout<<"The Riemann Sum is:"; res<<"The Riemann Sum is:"; for(int k=0;k<=n;k++) { Fx=f(a+k*h); cout<<"("<<Fx<<"x"<<h<<")+"; sum=sum+h*Fx; } delay(700); cout<<endl<<"\n\nThe Numerical Value of Integral is :"<<sum<<endl; res<<endl<<"\n\nThe Numerical Value of Integral is :"<<sum<<endl; getch(); } Use Riemann Sum method to find the numerical solution of
1. Let over [1, 2]. with n = 25, 50, and 100 to approximate the value of the integral.
Ans: 2.4336, 2.3834, 2.35835

2. Let over . Taking n =10, 100, 1000 and 10000.


Ans: 2.431266, 2.25325, 2.234182, 2.232257

Trapezoidal Rule Consider y = f(x) continuous over [a=x0, b=xn], where b = a + nh. n = 1,2,3 than by definition trapezoidal rule states that.
b

f(x) dx = h/2 [f(x0)+2{f(x1)+ f(x2)+ f(xn-1)}+f(xn)]


a

where h = b-a/n and x0 = a, x1 = a+h, x2 = a+2h, x3 = a+3h xn-1 = a+(n-1)h+ xn = b

C++ program for Trapezoidal Rule #include<iostream.h> #include<conio.h> #include<math.h> #include<dos.h> #include<fstream.h> float f(float x) { float value; value=2+2*cos(2*sqrt(x)); return value; } void main() { clrscr(); int n; float a,b,h,sum=0.0; ofstream res("Trapezoidal.txt"); cout<<"\nEnter the values of a, b, and n\n"; cin>>a>>b>>n; h=(b-a)/n; sum=f(a)+f(b); clrscr(); //performing itterations

cout<<"The Trapezoidal Rule sum is: "<<h<<"/2("<<f(a)<<"+2("; res<<"The Trapezoidal Rule sum is:"<<h<<"/2("<<f(a)<<"+2("; for(int k=1;k<=n-1;k++) { sum=sum+2*f(a+k*h); cout<<sum<<"+"; } cout<<")"<<f(b)<<")"; res<<")"<<f(b)<<")"; sum=sum*0.5*h; delay(700); cout<<endl<<"\n\nThe Numerical Value of Integral is :"<<sum<<endl; res<<endl<<"\n\nThe Numerical Value of Integral is :"<<sum<<endl; getch(); Use TrapezoidalRule to find the } numerical solution of
1.
Numerically approximate the integral 200, 400 and 800 subintervals. Ans: 2.92047, 2.920115, 2.920024, 2.920005, 2.919996 by

using the trapezoidal rule with n = 50, 100,

2. Numerically approximate the integral


by using the trapezoidal rule with n = 50, 100, 200, 400 and 800 subintervals.
Ans: 2.891074, 2.891149, 2.891168, 2.891173, 2.891173

Simpsons 1/3 Rule Consider y = f(x) continuous over the interval [a, b]. Suppose that the interval is divided into n subintervals (where n must be an even number) with h = b-a/n such that x0 = a, x1 = a+h, x2 = a+2h, x3 = a+3h xn-1 = a+(n-1)h+ xn = b. Than Simpsons Rule states that

f(x) dx = h/3 [f(x0)+2{f(x2)+ f(x4)+ f(xn-2)}+ 4{f(x1)+ f(x3)+ f(xn-1)}+f(xn)]


a

is an approximation of the integral of f(x) over the interval [a, b]

C++ program for Simpsons 1/3 Rule #include<iostream.h> #include<conio.h> #include<math.h> #include<dos.h> #include<fstream.h> float f(float x) { float value; value=2+2*cos(2*sqrt(x)); return value; } void main() { clrscr(); int n; float a,b,h,sum=0.0; ofstream res("Simpsons3.txt"); cout<<"\nEnter the values of a, b, and n\n"; cin>>a>>b>>n; h=(b-a)/n; sum=f(a)+f(b); clrscr(); //performing itterations

for(int k=1;k<=n-1;k++) { if(k%2==0) sum=sum+2*f(a+k*h); else sum=sum+4*f(a+k*h); } sum=(sum*h)/3.0; delay(700); cout<<endl<<"\n\nThe Numerical Value of Integral is :"<<sum<<endl; res<<endl<<"\n\nThe Numerical Value of Integral is :"<<sum<<endl; getch(); Use Simpson 1/3 Rule to find the } numerical solution of
1.
Numerically approximate the integral 200, 400 and 800 subintervals. Ans: 2.919995, 2.919996, 2.919996, 2.919995, 2.919993 by

using the Simpsons rule with n = 50, 100,

2. Numerically approximate the integral


by using the Simpsons1/3 rule with n = 50, 100, 200, 400 and 800 subintervals.
Ans: 2.891175, 2.981174, 2.891168, 2.891175, 2.891175

Simpsons 3/8 Rule Consider y = f(x) continuous over the interval [a, b]. Suppose that the interval is divided into n subintervals (where n must divisible by 3) with h = b-a/n such that x0 = a, x1 = a+h, x2 = a+2h, x3 = a+3h xn-1 = a+(n-1)h+ xn = b. Than Simpsons 3/8 Rule states that

f(x) dx = 3h/8 [f(x0)+3{f(x1)+ f(x2)+ f(xn-1)}+f(xn)]


a

Is an approximation of the integral of f(x) over the interval [a, b]

C++ program for Simpsons 3/8 Rule #include<iostream.h> #include<conio.h> #include<math.h> #include<dos.h> #include<fstream.h> float f(float x) { float value; value=3*(1/exp(x))*sin(pow(x,2))+1; return value; } void main() { clrscr(); int n; float a,b,h,sum=0.0; ofstream res("Simpsons3-8.txt"); cout<<"\nEnter the values of a, b, and n\n"; cin>>a>>b>>n; h=(b-a)/n; sum=(f(a)+f(b)); clrscr(); //performing itterations

for(int k=1;k<=n-1;k++) { sum=sum+3*f(a+k*h); } sum=(sum*h)*0.375; delay(700); cout<<endl<<"\n\nThe Numerical Value of Integral is :"<<sum<<endl; res<<endl<<"\n\nThe Numerical Value of Integral is :"<<sum<<endl; getch(); } Use Simpson 3/8 Rule to find the numerical solution of
1.
Numerically approximate the integral 600 and 900 subintervals. Ans: 3.867379, 3.879891, 3.889969, 3.890808 by

using the Simpsons3/8 rule with n = 60, 120,

2. Numerically approximate the integral


using the Simpsons3/8 rule with n = 60, 120, 600 and 900 subintervals. Ans: 3.241912, 3.247261, 3.251514, 3.251863

Monte Carlo -1 Method Monte Carlo methods can be thought of as statistical simulation methods that utilize sequences of random numbers to perform the simulation. The name "Monte Carlo'' was coined by Nicholas Constantine Metropolis (1915-1999), because of the similarity of statistical simulation to games of chance, and because Monte Carlo is a center for gambling and games of chance. From previous discussion of trapezoidal rule and Simpsons rule we found out that the integral of a function f(x) over the interval [a, b] can be written as according to mid point rule. (Where c is the mid point between a and b)
. Or equivalently

The above equations are for n = 1, i.e we are using the whole interval from a to b and have not made any subintervals. If we do divide the interval into sub interval than f(c) will be the average of all the values of f(ck) in the sub interval. In that case the above relation can be written as
.

using h = (b-a)/n , where .

With error

where

The Algorithm for the Monte Carlo method goes like this. Pick n randomly distributed points in the interval .

Determine the average value of the function

Compute the approximation to the integral

An estimate for the error is

where

Every time a Monte Carlo simulation is made using the same sample size it will come up with a slightly different value. Larger values of the order . will produce more accurate approximations. The values converge very slowly of

Following Excel Sheet demonstrate the procedure for Monte Carlo Method

In column A we have used =RAND()*PI()/2 to generate 100 random numbers between 0 and Pi. In column B we have calculated the values if function f(x) = Cosx from the values of the random numbers generated in column a using the function =COS(A#) where # represent the number of cell just next to the cell of column B. In cell C2 we calculate the average of the values of column B using the function =AVERAGE(B2:B101). In cell D2 we have calculated the numerical answer using (PI()/2)*C2. Which is the required numerical solution. Next four columns contain the calculations of the error in the routine. According to the formula given above.

C++ program for Monte Carlo -1 Method #include<iostream.h> #include<conio.h> #include<math.h> #include<dos.h> #include<fstream.h> #include<stdlib.h> double f(double x) { double value; value=cos(x); return value; } void main() { clrscr(); double a,b; cout<<"Enter the initial and final points of the interval: "; cin>>a>>b; int n=1; cout<<"Enter the number of random numbers you want to use: "; cin>>n; double random[1000];

//generating random numbers for(int j=1;j<=n;j++) { random[j]=(rand()%1000); //Generates random number b/w 0 and 1000 random[j]=(random[j]*b)/1000; //Converts value from 0 to 1000 into a to b } double sum=0.0, avg; ofstream res("Monte1.txt"); //performing itterations for summing up the values of f(x) using the above generated random numbers for(int k=1;k<=n;k++) { sum=sum+f(random[k]); } avg=sum/n; double ans=avg*(b-a);//computes answer of integral delay(700); cout<<endl<<"\n\nThe Numerical Value of Integral is :"<<ans<<endl; res<<endl<<"\n\nThe Numerical Value of Integral is :"<<ans<<endl;

getch(); } Use Monte Carlo Method to find the numerical solution of


1. f(x) = cos x, with a=0 and b = Pi/2 = 1.570796. Ans : 1.002148
2.

Let

. Use the Monte Carlo

method to calculate approximations to the integral number. Ans : 5.2659091 . Use 500 random

3.

Let

. Use the Monte

Carlo method to calculate approximations to the integral Random Number Ans: 1.035687 . Use 500

Monte Carlo -2 Method The Monte Carlo method can be used to numerically approximate the value of a double integral. The algorithm for Monte Carlo method with two variable or double integrals goes like this

Pick n randomly distributed points the rectangle


Determine the average value of the function

in

. Compute the approximation to the integral

.
An estimate for the error is

where

C++ program for Monte Carlo -2 Method #include<iostream.h> #include<conio.h> #include<math.h> #include<dos.h> #include<fstream.h> #include<stdlib.h> double f(double x,double y) { double value; value=4-pow(x,2)-pow(y,2); return value; } void main() { clrscr(); double a,b; cout<<"Enter the initial and final points of the interval of 1st variable: "; cin>>a>>b; double c,d;
cout<<"Enter the initial and final points of the interval of 2nd variable: ";

cin>>c>>d; int n=1; cout<<"Enter the number of random numbers you want to use: "; cin>>n;

double random1[1000];//For the first variable say x //generating random numbers for(int j=1;j<=n;j++) { random1[j]=(rand()%1000); //Generates random number b/w 0 and 1000 random1[j]=(random1[j]*b)/1000; //Converts value from 0 to 1000 into a to b } double random2[1000];//For the second variable say y //generating random numbers for(j=1;j<=n;j++) { random2[j]=(rand()%1000); //Generates random number b/w 0 and 1000 random2[j]=(random2[j]*d)/1000; //Converts value from 0 to 1000 into c to d } double sum=0.0, avg; ofstream res("Monte1.txt"); //performing itterations for summing up the values of f(x,y) using the above generated random numbers for(int k=1;k<=n;k++) { sum=sum+f(random1[k],random2[k]); } avg=sum/n;

double ans=avg*(b-a)*(d-c);//computes answer of integral delay(700); cout<<endl<<"\n\nThe Numerical Value of Integral is :"<<ans<<endl; res<<endl<<"\n\nThe Numerical Value of Integral is :"<<ans<<endl; getch(); }
Use Monte Carlo Method to find the numerical solution of
1.
f(x,y) = x, over the intervals Ix=[0,1] and Iy=[0,2]. Using 500 random numbers Ans : 0.9791

2.

Let double integral

. Use the Monte

Carlo method to calculate approximations to the

. Using 500= random numbers Ans : 4.670791

3.

Let the double integral

. Use the

Monte Carlo method to calculate approximations to

. Using 500 random numbers.


Ans: 2.683277

Ordinary Differential Equations


Euler Method To approximate a solution to the equation y(x) = f(x, y(x)) Where x is independent variable and y is dependent on x. With initial condition y(x0) = y0 On an interval Ix = [x0, x], choose a small value for h > 0 and an integer n such that x0 + nh = x. n =1,2,3, Compute y1, y2, . . . , ym+1 using the difference equation ym+1 = ym + hf(xm, ym). m = 0,1,2,n-1 Then ym+1= yx is an approximation for y(x0 + nh) = y(x).

C++ program for Eulers Method for y(x) = f(x, y(x)) = 0.04x, y(0) =50, Ix = [0, 50] #include<iostream.h> #include<conio.h> #include<math.h> #include<dos.h> #include<fstream.h> double f(double a,double b) { double value; value=(0.04)*b; return value; } void main() { again: clrscr(); int n; double X0,Y0,X,Y,h; cout<<"Enter the value of X0: "; cin>>X0; cout<<"Enter the value of Y0: "; cin>>Y0; cout<<"Enter the value of X: ";

cin>>X; cout<<"Enter the values of desired number of steps: "; cin>>n; if(n<=0) goto again; h=(X-X0)/n; ofstream res("Euler.txt"); //performing itterations for Euler Mehtod Eq Y=Y0; double X_it=X0; for(int i=1;i<=n;i++) { Y=Y+h*f(X_it,Y); cout<<"Y"<<i<<" :"<<Y<<endl; res<<"Y"<<i<<" :"<<Y<<endl; X_it=X_it+h; } delay(700); cout<<"The approximate solution of Y("<<X<<") :"<<Y; res<<"The approximate solution of Y("<<X<<") :"<<Y; getch(); }

Euler Improved Method To approximate a solution to the equation y(x) = f(x, y(x)) Where x is independent variable and y is dependent on x. With initial condition y(x0) = y0 On an interval Ix = [x0, x], choose a small value for h > 0 and an integer n such that x0 + nh = x. n =1,2,3, Compute y1, y2, . . . , ym+1 using the difference equation ym+1 = ym + h/2(f(xm+h, z)+f(xm,ym)). m = 0,1,2,n-1 where z = ym+1 = ym + h f(xm,ym) m = 0,1,2,n-1 Then ym+1= yx is an approximation for y(x0 + nh) = y(x).

C++ program for Eulers Improved Method for y(x) = f(x, y(x)) = 0.04x, y(0) =50,
Ix = [0, 50]

#include<iostream.h> #include<conio.h> #include<math.h> #include<dos.h> #include<fstream.h> double f(double a,double b) { double value; value=(0.04)*b; return value; } void main() { again: clrscr(); int n; double X0,Y0,X,Y,h; cout<<"Enter the value of X0: "; cin>>X0; cout<<"Enter the value of Y0: "; cin>>Y0; cout<<"Enter the value of X: "; cin>>X;

cout<<"Enter the values of desired number of steps: "; cin>>n; if(n<=0) goto again; h=(X-X0)/n; ofstream res("Euler.txt"); //performing itterations for Euler Mehtod Eq Y=Y0; double X_it=X0,Y_it=Y0,Z; for(int i=1;i<=n;i++) { Z=Y_it+h*f(X_it,Y_it); Y=Y_it+0.5*h*(f(X_it,Z)+f(X_it,Y_it)); Y_it=Y; X_it=X_it+h; cout<<"Y"<<i<<" :"<<Y<<endl; res<<"Y"<<i<<" :"<<Y<<endl; } delay(700); cout<<"The approximate solution of Y("<<X<<") :"<<Y; res<<"The approximate solution of Y("<<X<<") :"<<Y; getch(); }

Runge Kutta 4th order Method To approximate a solution to the equation y(x) = f(x, y(x)) Where x is independent variable and y is dependent on x. With initial condition y(x0) = y0 On an interval Ix = [x0, x], choose a small value for h > 0 and an integer n such that x0 + nh = x. n =1,2,3, Compute y1, y2, . . . , ym+1 using the difference equation k1= h* f(xm,ym) k2 = h* f(xm+h/2,ym+k1/2) k3 = h* f(xm+h/2,ym+k2/2) k4 = h* f(xm+h/2,ym+k3) ym+1 = ym + 1/6(k1+2k2+2k3+k4). m = 0,1,2,n-1 Then ym+1= yx is an approximation for y(x0 + nh) = y(x).

C++ program for R-K 4th order Method for y(x) = f(x, y(x)) = 0.04x, y(0) =50, Ix =
[0, 50]

#include<iostream.h> #include<conio.h> #include<math.h> #include<dos.h> #include<fstream.h> double f(double a,double b) { double value; value=(0.04)*b; return value; } void main() { again: clrscr(); int n; double X0,Y0,X,Y,h; cout<<"Enter the value of X0: "; cin>>X0; cout<<"Enter the value of Y0: "; cin>>Y0; cout<<"Enter the value of X: "; cin>>X; cout<<"Enter the values of desired number of steps: ";

cin>>n; if(n<=0) goto again; h=(X-X0)/n; ofstream res("R_K_4.txt"); //performing itterations for Euler Method Eq Y=Y0; double X_it=X0,k1,k2,k3,k4; for(int i=1;i<=n;i++) { k1=h*f(X_it,Y); k2=h*f(X_it+0.5*h,Y+0.5*k1); k3=h*f(X_it+0.5*h,Y+0.5*k2); k4=h*f(X_it+h,Y+k3); Y=Y+1.0/6.0*(k1+2*k2+2*k3+k4); cout<<"Y"<<i<<" :"<<Y<<endl; res<<"Y"<<i<<" :"<<Y<<endl; X_it=X_it+h; } delay(700); cout<<"The approximate solution of Y("<<X<<") :"<<Y; res<<"The approximate solution of Y("<<X<<") :"<<Y; getch(); }

Runge Kutta Fehlberg (RKF45) Method To approximate a solution to the equation y(x) = f(x, y(x)) Where x is independent variable and y is dependent on x. With initial condition y(x0) = y0 On an interval Ix = [x0, x], choose a small value for h > 0 and an integer n such that x0 + nh = x. n =1,2,3, Compute y1, y2, . . . , ym+1 using the difference equation k1= h* f(xm,ym) k2 = h* f(xm+h/4,ym+k1/4) k3 = h* f(xm+3*h/8,ym+3*k2/32+9*k2/32) k4 = h* f(xm+12*h/13,ym+1932*k1/2197-7200*k2/2197+7296*k3/2197) k5 = h* f(xm+12*h/13,ym+439*k1/216-8*k2+3680*k3/513-845*k4/4104) k6 = h* f(xm+h/2,ym-8*k1/27+2*k2-3544*k3/2565+1859*k4/4104-11*k5/40) ym+1 = ym + 16*k1/135+6656*k3/12825+28561*k4/56430-9*k5/50+2*k6/55). m = 0,1,2,n-1 Then ym+1= yx is an approximation for y(x0 + nh) = y(x).

C++ program for RKF45 Method for y(x) = f(x, y(x)) = 0.04x, y(0) =50, Ix = [0, 50] #include<iostream.h> #include<conio.h> #include<math.h> #include<dos.h> #include<fstream.h> double f(double a,double b) { double value; value=(0.04)*b; return value; } void main() { again: clrscr(); int n; double X0,Y0,X,Y,h; cout<<"Enter the value of X0: "; cin>>X0; cout<<"Enter the value of Y0: "; cin>>Y0; cout<<"Enter the value of X: "; cin>>X;

cout<<"Enter the values of desired number of steps: "; cin>>n; if(n<=0) goto again; h=(X-X0)/n; ofstream res("R_K_F_45.txt"); //performing itterations for Euler Mehtod Eq Y=Y0; double X_it=X0,k1,k2,k3,k4,k5,k6; for(int i=1;i<=n;i++) { k1=h*f(X_it,Y); k2=h*f(X_it+0.25*h,Y+0.25*k1); k3=h*f(X_it+3.0*h/8.0,Y+3.0*k2/32.0+9.0*k2/32.0); k4=h*f(X_it+12.0*h/13.0,Y+1932.0*k1/2197.07200.0*k2/2197.0+7296.0*k3/2197.0); k5=h*f(X_it+12.0*h/13.0,Y+439.0*k1/216.0-8*k2+3680.0*k3/513.0845.0*k4/4104.0); k6=h*f(X_it+h/2.0,Y-8.0*k1/27.0+2.0*k2-3544.0*k3/2565.0+1859.0*k4/4104.011.0*k5/40.0); Y=Y+16.0*k1/135.0+6656.0*k3/12825.0+28561.0*k4/56430.09.0*k5/50.0+2.0*k6/55.0; cout<<"Y"<<i<<" :"<<Y<<endl; res<<"Y"<<i<<" :"<<Y<<endl; X_it=X_it+h; }

delay(700); cout<<"The approximate solution of Y("<<X<<") :"<<Y; res<<"The approximate solution of Y("<<X<<") :"<<Y; getch(); }

Vous aimerez peut-être aussi