Chapter:

rk-4-method

1. FIRST ORDER DIFFERENTIAL EQUATION USING RK-4 METHOD

TITLE: 

TO SOLVE FIRST ORDER DIFFERENTIAL EQUATION USING RK-4 METHOD

OBJECTIVES:

  • To be able to follow the algorithm of RK-4 method to solve first order differential equation.

  • To build some programming concepts by solving practical problems.

 TOOLS REQUIRED:

  • Computer workstation

  • Program software (as necessary)

 THEORY:

RUNGE-KUTTA method is a method for solving differential equation without requiring the calculations of higher derivatives and hence gives greater accuracy.  The runge kutta method possess the advantage of requiring only the function values at some selected points.

The fourth order RK method is most commonly used. The working rule for finding the increment of K of y corresponding to an increment h of x by RK-4 method from

`(dy)/(dx)=f (x,y)`, is as follows:

Calculate successively the following:

  • `K_1=h*f (x_0,y_0)`

  • `K_2=h*f (x_0+1/2*h,y_0+1/2*K_1`

  • `K_3=h*f (x_0+1/2*h,y_0+1/2*K_2`

  • `K_4=h*f (x_0+h,y_0+K_3`

  • `K=1/6*(K_1+2K_2+2K_3+K_4)`

which gives the required approximate value as `y_1=y_0+K`

 ALGORITHM:

  1. Start

  2. Define function f(x,y)

  3. Read values of initial condition(x0 and y0), number of steps (n) and calculation point (xn)

  4. Calculate step size (h) = (xn - x0)/n

  5.  Set i=0

  6. Find the following while i

  • `K_1=h*f (x_0,y_0)`

  • `K_2=h*f (x_0+1/2*h,y_0+1/2*K_1`

  • `K_3=h*f (x_0+1/2*h,y_0+1/2*K_2`

  • `K_4=h*f (x_0+h,y_0+K_3`

  • `K=1/6*(K_1+2K_2+2K_3+K_4)`

  • `y_n=y_0+K`

  • `i=i+1`

  • `x_0=x_0+h`

  • `y_0=y_n`

  1. Display yn as result

  2. Stop

C-PROGRAM:   

#include
#include
#define f(x,y) (y*y-x*x)/(y*y+x*x)
void main()
{
 float x0, y0, xn, h, yn, k1, k2, k3, k4, k;
 int i, n;
 clrscr();
 printf("Enter Initial Condition\n");
 printf("x0 = ");
 scanf("%f", &x0);
 printf("y0 = ");
 scanf("%f", &y0);
 printf("Enter calculation point xn = ");
 scanf("%f", &xn);
 printf("Enter number of steps: ");
 scanf("%d", &n);
 h = (xn-x0)/n;
 printf("\nx0\ty0\tyn\n");
 for(i=0; i < n; i++)
 {
  k1 = h * (f(x0, y0));
  k2 = h * (f((x0+h/2), (y0+k1/2)));
  k3 = h * (f((x0+h/2), (y0+k2/2)));
  k4 = h * (f((x0+h), (y0+k3)));
  k = (k1+2*k2+2*k3+k4)/6;
  yn = y0 + k;
  printf("%0.4f\t%0.4f\t%0.4f\n",x0,y0,yn);
  x0 = x0+h;
  y0 = yn;
 }
 /* Displaying result */
 printf("\nValue of y at x = %0.2f is %0.3f",xn, yn);
 getch();
 }

OUTPUT:

Enter Initial Condition
x0 = 0
y0 = 1
Enter calculation point xn = 0.4
Enter number of steps: 2

x0      y0      yn
0.0000  1.0000  1.1960
0.2000  1.1960  1.3753

Value of y at x = 0.40 is 1.375

CONCLUSION:

Thus the first order partial differential equation was solved using RK-4 method using C Programming language.


Show More