Академический Документы
Профессиональный Документы
Культура Документы
Page | 1
A physical situation that concerns with the rate of change of one quantity with respect to another gives rise to a differential equation. Consider the first order ordinary differential equation , with the initial condition
Many analytical techniques exist for solving such equations. But these methods can be applied to solve only a selected class of differential equations. However, a majority of differential equations appearing in physical problems cannot be solved analytically .Thus it becomes imperative to discuss their solution by numerical methods. In numerical methods, we do not proceed in hope of finding a relation between variables but we find the numerical values of the dependent variable for certain values of independent variable. Example: solution of following equation can be easily determined by numerical method rather than analytical methods.
dy y 2 x 2 dx y 2 x 2
Page | 2
dx
y2 x2
Analytically it is very difficult but by numerical method it is very simple and easy.
. .(2) Expanding by Taylors series, we get .(3) Comparing (2) and (3),it follows that Eulers method agrees with Taylors series solution upto the term in h.Hence is the .
Let h be the interval between equidistant values of x then in 2nd order Range-Kutta Method, the first increment in y is computed from the formulae
In similar manner, the increment in y for the second interval is computed by means of the formulae,
The inherent error in the second order Runge Kutta method is of order h 3
Page | 4
Third order Runge-Kutta method:This method gives the approximate solution of the initial value problem
f(x0,y0)
Fourth order Runge Kutta method:It is the most widely used method and is particularly suitable cases when the computation of higher derivatives is complicated.
Then,
This method is also termed as RUNGE-KUTTAs method simply. the inherent error in the forth order Runge-Kutta method is of order h 3 .
Page | 5
Condition for using numerical methods:Let the following function y=f(x,y), y(x0)=y0 (1)
1.Existence theorem:
This theorem says that if f(x,y) in (1) is continuous in some region of the xy-plane containing the point (x0,y0) (corresponding to the given initial condition),then the problem (1) has at least one solution.
2.Uniqueness theorem :
The second theorem says that if ,moreover ,the partial derivative
f exists and is continuous in y
that region ,then the problem (1) can have at most one solution ;hence by theorem 1,it has precisely one solution.
The condition in the two theorems are sufficient conditions rather than necessary ones ,and can be lessened .For example ,by the mean value theorem of differential calculus we have f(x,y2)-f(x,y1)=(y2-y1)
f |y= y y
where (x,y1) and (x,y2) are assumed to be in R and y is a suitable value between y1 and y2. From this f ( x, y2) f ( x, y1) M y2 y1 This is known as Lipschitz condition.
float f(float,float); void main() { int i,n; float x0,y0,h; float k1,k2,k3,k4,x,y,k; clrscr(); FILE *fpx,*fpy; fpx=fopen("3rd1x","w"); fpy=fopen("3rd1y","w"); printf("\nEnter the value of x0,y0,h\n"); scanf("%f%f%f",&x0,&y0,&h);
x=x0; y=y0; f(x,y); for(i=0;i<=n;i++) { k1=h*f(x,y); k2=h*f(x+h,y+k1); k3=h*f(x+h,y+k2) ; k4=h*f(x+h/2,y+k3/2); k=(k1+k3+4*k4)/6.0; fprintf(fpx,"%f\n",x); fprintf(fpy,"%f\n",y); printf("\n X=%f Y=%f",x,y); x=x+h; y=y+k; } fclose(fpx); fclose(fpy); getch(); }
#include<stdio.h> #include<conio.h> #include<math.h> #define f(x) c*exp(x)-x-1 void main() { float x,p,y,x0,y0,c,h; int n,i; clrscr(); FILE *fpx,*fpy; fpx=fopen("exactx","w"); fpy=fopen("exacty","w"); printf("Enter the value of x0,y0,h \n"); scanf("%f%f%f",&x0,&y0,&h);
c=(exp(-x0)*(1+x0+1/y0)); printf("c=%f",c);
Page | 9
printf("\nEnter the no of iteration"); scanf("%d",&n); for(i=0;i<=n;i++) { p=f(x); y=1/p; fprintf(fpx,"%f\n",x); fprintf(fpy,"%f\n",y); printf("\nX=%f Y=%f",x,y); x=x+h; } fclose(fpy); getch(); }Graphical representation
x-axis
Example 2.
Page | 10
#include<stdio.h> #include<conio.h> float f(float,float); void main() { int i,n; float x0,y0,h,xn; float k1,k2,k3,k4,x,y,k; clrscr(); FILE *fpx,*fpy; fpx=fopen("4thx","w"); fpy=fopen("4thy","w"); printf("\nEnter the value of x0,y0,h\n"); scanf("%f%f%f",&x0,&y0,&h); printf("Enter the no of iteration\n");
scanf("%d",&n);
Page | 11
Page | 12
fclose(fpx); fclose(fpy); getch(); } float f(float x,float y ) { float sum; sum=x*x-y; return sum; }
clrscr(); FILE *fpx,*fpy; fpx=fopen("exact2x","w"); fpy=fopen("exact2y","w"); printf("Enter the value of x0,y0,h \n"); scanf("%f%f%f",&x0,&y0,&h);
c=(y0-x0*x0+2*x0-2)*exp(x0); printf("c=%f",c);
printf("\nEnter the no of iteration"); scanf("%d",&n); for(i=0;i<=n;i++) { y=f(x); fprintf(fpx,"%f\n",x); fprintf(fpy,"%f\n",y); // printf("\nX=%f\n",x); printf("\nX=%f Y=%f",x,y); x=x+h;
} fclose(fpx);
Page | 14
fclose(fpy); getch(); }
Graph:-
Example 3.
#include<stdio.h> #include<conio.h> float f(float,float); void main() { int i,n; float x0,y0,h,xn; float k1,k2,k3,k4,x,y,k; clrscr(); FILE *fpx,*fpy; fpx=fopen("4th2x","w"); fpy=fopen("4th2y","w"); printf("\nEnter the value of x0,y0,h\n"); scanf("%f%f%f",&x0,&y0,&h); printf("Enter the no of iteration\n"); scanf("%d",&n);
x=x0; y=y0;
f(x,y);
Page | 16
fclose(fpx); fclose(fpy);
getch(); }
Page | 17
float f(float x,float y ) { float sum; sum=(x-y)/(x+y); return sum; } Exact solution of above problem .
/* exact sol of func x-y/x+y*/
#include<stdio.h> #include<conio.h> #include<math.h> #define f(x) pow(2*x*x+c,0.5)-x void main() { float x,y,x0,y0,c,h; int n,i; clrscr(); FILE *fpx,*fpy; fpx=fopen("exact3x","w"); fpy=fopen("exact3y","w"); printf("Enter the value of x0,y0,h where x0 and y0 are not zero together\n");
scanf("%f%f%f",&x0,&y0,&h);
c= pow(x0+y0,2)-2*x0*x0;
Page | 18
printf("c=%f",c);
printf("\nEnter the no of iteration"); scanf("%d",&n); for(i=0;i<=n;i++) { y=f(x); fprintf(fpx,"%f\n",x); fprintf(fpy,"%f\n",y); printf("\nX=%f Y=%f",x,y); x=x+h; } fclose(fpx); fclose(fpy); getch(); }
Graph:-
Page | 19