Practical no: 4
Name- saakshi champaneria
Enrollment no. –ET23BTIC004
Aim- Solving the ordinary differential equation using Fourth Order Runga-Kutta
Mehtod
dy
dx
=f(x,y) y(0)=1
dy
dx
2
=x + y
2
[ x=0, y0=1, h=0.2]
CODE:
#include<stdio.h>
#include<conio.h>
#define f(x,y) (x*x+y*y)
int main()
float x0,y0,xn,h,yn,m1,m2,m3,m4,m;
int i,n;
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 step size:");
scanf("%f",&h);
n=(xn-x0)/h;
printf("%d",n);
printf("\nx0\t\ty0\t\tm\t\tyn\n");
for(i=0;i<n;i++)
m1=f(x0,y0);
m2=f((x0+h/2),(y0+(m1*h/2)));
m3=f((x0+h/2),(y0+(m2*h/2)));
m4=f((x0+h),(y0+(m3*h)));
m=((m1+(2*m2)+(2*m3)+m4)/6);
yn=y0+(m*h);
printf("%f\t%f\t%f\t%f\n",x0,y0,m,yn);
x0=x0+h;
y0=yn;
printf("\nValue of y at x =%f is %f",xn,yn);
return 0;
OUTPUT:
CONCLUSION:
The Runge-Kutta 4th order method was successfully implemented using
C++. The program accurately computed the solution to the first-order
ODE with given initial conditions. This method provides a stable and
accurate numerical solution, and is particularly useful when analytical
methods are difficult or impossible to apply.