Вы находитесь на странице: 1из 19

Numerical solution of ordinary differential equations

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

2 2 solution of different equation dy y x

dx

y2 x2

Analytically it is very difficult but by numerical method it is very simple and easy.

Range Kutta methods


More efficient methods in terms of accuracy were developed by two Germen Mathematicians Carl runge(1856-1927) and Wilhem kutta (1867-1944). These methods are well known as RUNGE-KUTTA methods. We can solve differential equation numerically using TAYLORS series method to determine higher order derivatives is a lengthy process . To overcome this there is a class of methods known as Runge Kutta methods ,which do not require the calculations of higher order derivatives and give greater accuracy. These methods donot demand prior computation of higher derivatives of y(x) as in Taylors methods.In place of these derivatives ,extra values of the given function are used.

Fourth order-Range-Kutta Method


Is widely used for finding the numerical solution of linear or non-linear ordinary differential equations.

First order Range Kutta Method:-

(1) Eulers method gives


Page | 3

. .(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 .

Second order Range-Kutta Method:Consider the differential equation:

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

Taken in the given order: Then, x1=x0+h

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)

This method is also known as RUNGES method

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,

x1=x0+h and y1=y0+ y

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.

Examples of numerical solution by Runge Kutta method: 1.


Page | 6

/* runge kutta 3rd order func y 2 * x y */

#include<stdio.h> #include<conio.h> #include<math.h>

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);

printf("Enter the no of iteration"); scanf("%d",&n);


Page | 7

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(); }

float f(float x,float y )


{
Page | 8

float sum; sum=-(x*pow(y,2)+y); return sum; }

Exact solution of the above problem:


/* exact sol of func x*pow(y,2)+y*/

#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

/*Range kutta 4th order of function x*x-y */

#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

x=x0; y=y0; f(x,y);

for(i=0;i<=n;i++) { k1=h*f(x,y); k2=h*f(x+h/2.0,y+k/2.0); k3=h*f(x+h/2.0,y+k2/2.0) ; k4=h*f(x+h,y+k3);

k=(k1+(k2+k3)*2.0+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;

Page | 12

fclose(fpx); fclose(fpy); getch(); } float f(float x,float y ) { float sum; sum=x*x-y; return sum; }

2.Exact solution of this problem: /* exact sol of func x*x-y*/

#include<stdio.h> #include<conio.h> #include<math.h> #define f(x) x*x-2*x+2+c*exp(-x) void main()

{ float x,y,x0,y0,c,h; int n,i;


Page | 13

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.

/*Range kutta 4th order of function x-y/y+x */


Page | 15

#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

for(i=0;i<=n;i++) { k1=h*f(x,y); k2=h*f(x+h/2.0,y+k/2.0); k3=h*f(x+h/2.0,y+k2/2.0) ; k4=h*f(x+h,y+k3);

k=(k1+(k2+k3)*2.0+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(); }
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

Вам также может понравиться