RUNGE KUTTA METHOD 4TH ORDER
July 22, 2022
Solve The Given Differential Equation By Using 4th Order Runge Kutta Method
Source code :-
//Solve by Runge Kutta 4th order method dy/dx=(1+x^2)y where y(0)=1 and x=0(0.2)0.6
#include<stdio.h>#include<math.h>float f(float x,float y){ return ((1+pow(x,2))*y); // Replace With given Equation}int main(){ float x,y,p,q,xf,h,d1,d2,d3,d4; int i=0; printf("Enter the initial value of x : "); scanf("%f",&x); printf("\nEnter the initial value of y : "); scanf("%f",&y); printf("\nEnter the final value of x : "); scanf("%f",&xf); printf("\nEnter the value of step length (h) : "); scanf("%f",&h); printf("\n\n Ite\t Xi\t\t Yi\t\t D1\t\t D2\t\t D3\t\t D4\t\t Yi+1\n"); printf("--------------------------------------------------------"); printf("--------------------------------------------------------"); while(x<xf) { d1=h*f(x,y); d2=h*f(x+h/2,y+d1/2); d3=h*f(x+h/2,y+d2/2); d4=h*f(x+h,y+d3); p=x; q=y; x=x+h; y=y+(1.0/6.0)*(d1+2*d2+2*d3+d4); printf("\n%3d\t%f\t%f\t%f\t%f\t%f\t%f\t%f",i++,p,q,d1,d2,d3,d4,y); } printf("\n\n\n\t\t\t The result of Y(%.3f) = %f\n",x,y); return 1;}
Input-Output :-
abhi@hp-15q-laptop:~$ gcc rk4.cabhi@hp-15q-laptop:~$ ./a.out
Enter the initial value of x : 0
Enter the initial value of y : 1
Enter the final value of x : 0.6
Enter the value of step length (h) : 0.2
Ite Xi Yi D1 D2 D3 D4 Yi+1 ---------------------------------------------------------------------------------------------------------------- 0 0.000000 1.000000 0.200000 0.222200 0.224442 0.254684 1.224661 1 0.200000 1.224661 0.254730 0.294742 0.299103 0.353513 1.523983 2 0.400000 1.523983 0.353564 0.425191 0.434145 0.532611 1.958125
The result of Y(0.600) = 1.958125