C-code for Runge-Kutta method
- Partho Basak
- Aug 15, 2018
- 1 min read
Updated: Aug 16, 2018
Follow the link for more information: https://math.okstate.edu/people/yqwang/teaching/math4513_fall11/Notes/rungekutta.pdf
/**************
Name: Partho Basak
Roll: 13MA60041
College: Fakir Chand College
Problem: solve first order Differential equation "dy/dx =x^r+ysin(y)" by Runge kutta method. :initial input: x0=0,, y0=1, the interval is 8 and we have to find y(0.8)
***************/
#include<stdio.h>
#include<math.h>
float function(float x,float y)
{
float f;
int r=8;
f=pow(x,r)+y*sin(y);
return f;
}
void main()
{
float t0,w0,x,y,h,k1,k2,k3,k4;
int n,i;
printf("\t\t\t input \n\n");
printf("Enter the value of x0, y0:\n");
scanf("%f%f",&t0,&w0);
printf("\n Enter the number of intervals:\n");
scanf("%d",&n);
printf("\n Enter the value of x for which y is to be determined:\n");
scanf("%f",&x);
h=(x-t0)/n;
printf("\n\n\n\t\nOUTPUT\n\n");
printf("The value of the step length=%f",h);
printf("\n\n.........................................\n");
printf("IT.No.\t\t\txn\t\t\tyn");
printf("\n\n.........................................\n");
for(i=1;i<=n;i++)
{
k1=h*function(t0,w0);
k2=h*function(t0+h/2,w0+k1/2);
k3=h*function(t0+h/2,w0+k2/2);
k4=h*function(t0+h,w0+k3);
y=w0+(k1+2*(k2+k3)+k4)/6;
w0=y;
t0=t0+h;
printf("\n%5d\t\t\t%f\t\t\t%f",i,t0,w0);
}
printf("\n............................................\n");
printf("\n\nThe required value of y is %.6f",y);
printf("\n correct up to 6 decimal places.\n");
}
/******************************************************************************************************************************************************************/
output is:
Enter the value of x0, y0:
0 1
Enter the number of intervals:
8
Enter the value of x for which y is to be determined:
0.8
The value of the step length=0.100000
.........................................
IT.No. xn yn
.........................................
1 0.100000 1.090261
2 0.200000 1.193962
3 0.300000 1.312832
4 0.400000 1.448167
5 0.500000 1.600328
6 0.600000 1.768254
7 0.700000 1.949481
8 0.800000 2.141429
............................................
The required value of y is 2.141429
correct up to 6 decimal places.





Comments