Solve the initial value problems using Runge-Kutta Method.

 Runge-Kutta Method

A number of ordinary differential equations come in engineering and all of them may not be solved by analytical method. In order to solve or get numerical solution of such ordinary differential equations, Runge Kutta method is one of the widely used methods. Unlike like Taylor’s series, in which much labor is involved in finding the higher order derivatives, in RK4 method, calculation of such higher order derivatives is not required. (Derivation) This C program for Runge Kutta 4 method is designed to find out the numerical solution of a first order differential equation. It is a kind of initial value problem in which initial conditions are known, i.e the values of x0 and y0 are known, and the values of y at different values x is to be found out. The source code below to solve ordinary differential equations of first order by RK4 method first asks for the values of initial condition i.e. user needs to input x0 and y0 . Then, the user has to define increment ‘h’ and the final value of x at which y is to be determined. In this program for Runge Kutta method in C, a function f(x,y) is defined to calculate slope whenever it is called. f(x,y) = (x-y)/(x+y).

Solve the initial value problems using Runge-Kutta Method.


Ordinary Differential Equation Using Runge Kutta (RK) Method 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. Loop

                                                         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

 i = i + 1

x0 = x0 + h

y0 = yn 

While i < n

7. Display yn as result

8. Stop

 

Ordinary Differential Equation Using Fourth Order Runge Kutta (RK) Method

 This program is implementation of Runge Kutta Fourth Order method for solving ordinary differential equation using C programming language with output.

 Output of this is program is solution for dy/dx = (y2 - x2)/(y2+x2) with initial condition y = 1 for x = 0 i.e. y(0) = 1 and we are trying to evaluate this differential equation at y = 0.4 in two steps i.e. n = 2. ( Here y = 0.4 i.e. y(0.4) = ? is our calculation point).

 

C Program for RK-4 Method

 

#include<stdio.h>

#include<conio.h>

 #define f(x,y) (y*y-x*x)/(y*y+x*x)

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

  /* Calculating step size (h) */

 h = (xn-x0)/n;

  /* Runge Kutta Method */

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

 return 0;

}

 

RK Method C Program 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


Regression Method Algorithm Using Least Square Method

Solve the initial value problems using Modified Euler's Method.

Integratea function using numerically using trapezoidal Method.

Post a Comment

0 Comments