Runge Kutta Method Formula and C Program

Runge Kutta Method

A Runge Kutta Method of numerically integrating ordinary differential equations by using a trial step at the midpoint of an interval to cancel out lower-order error terms. The second-order formula is

Each of these slope estimates can be described verbally.

  • k1 is the slope at the beginning of the time step.
  • If we use the slope k1 to step halfway through the time step, then k2 is an estimate of the slope at the midpoint. This is the same as the slope, k2, from the second-order midpoint method. This slope proved to be more accurate than k1 for making new approximations for y(t).
  • If we use the slope k2 to step halfway through the time step, then k3 is another estimate of the slope at the midpoint.
  • Finally, we use the slope, k3, to step all the way across the time step (to t₀+h), and k4 is an estimate of the slope at the endpoint.



Runge Kutta Method


C Code For 4th Order Runge Kutta Method


#include<stdio.h>
#include<math.h>
#include<conio.h>
#define f(x,y) 1-2*(x)*(x)*(y)

int main(){
 float x,xp,x0,y0,y,h,m1,m2,m3,m4;
 printf("\n runge-Kutta Forth order\n");
 printf("\nEnter initial values of x and y\n");
 scanf("%f%f",&x0,&y0);
 printf("\nEnter x at which function to be evaluated\n");
 scanf("%f",&xp);
 printf("\nEnter the step size \n");
 scanf("%f",&h);
 y=y0;
 x=x0;
 for(x=x0;x<xp;x=x+h){
  m1=f(x,y);
  printf("\nm1=%f",m1);
  m2=f(x+h/2,y+(h*m1)/2);
  printf("\nm2=%f",m2);
  m3=f(x+h/2,y+(h*m2)/2);
  printf("\nm3=%f",m3);
  m4=f(x+h,y+h*m3);
  printf("\nm4=%f",m4);
  y=y+h/6*(m1+2*m2+2*m3+m4);
  printf("\ny=%f",y);
 }
 
 printf("Function value at x=%f is %f\n",xp,y);
 getch();
 return 0; 
 
}

Comments

Popular posts from this blog

C Program for SCAN Disk Scheduling Algorithm | C Programming

C program to Find Cartesian Product of Two Sets | C programming

C Program To Check The String Is Valid Identifier Or Not | C Programming