Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Numerical MethodsRSS Feeds

RUNGE-KUTTA 4th ORDER METHOD

Posted By: Lurleen Fischer     Category: C Programming     Views: 43386

Write a program of RUNGE-KUTTA 4th ORDER METHOD.

Code for RUNGE-KUTTA 4th ORDER METHOD in C Programming

#include<stdio.h>
#include <math.h>
#include<conio.h>
//dy/dx = 1 + y^2#define F(x,y)  1 + (y)*(y)
void main()
{
    double y0,x0,y1,n,h,f,k1,k2,k3,k4;
    clrscr();
    printf("\nEnter the value of x0: ");
    scanf("%lf",&x0);
    printf("\nEnter the value of y0: ");
    scanf("%lf",&y0);
    printf("\nEnter the value of h: ");
    scanf("%lf",&h);
    printf("\nEnter the value of last point: ");
    scanf("%lf",&n);
    for(; x0<n; x0=x0+h)
    {
        f=F(x0,y0);
        k1 = h * f;
        f = F(x0+h/2,y0+k1/2);
        k2 = h * f;
        f = F(x0+h/2,y0+k2/2);
        k3 = h * f;
        f = F(x0+h/2,y0+k2/2);
        k4 = h * f;
        y1 = y0 + ( k1 + 2*k2 + 2*k3 + k4)/6;
        printf("\n\n  k1 = %.4lf  ",k1);
        printf("\n\n  k2 = %.4lf ",k2);
        printf("\n\n  k3 = %.4lf ",k3);
        printf("\n\n  k4 = %.4lf ",k4);
        printf("\n\n  y(%.4lf) = %.3lf ",x0+h,y1);
        y0=y1;
    }
    getch();
}

/*
____________________________________

OUT PUT
____________________________________


Enter the value of x0: 0

Enter the value of y0: 0

Enter the value of h: 0.2

Enter the value of last point: 0.4


k1 = 0.2000

k2 = 0.2020

k3 = 0.2020

k4 = 0.2020

y(0.2000) = 0.202

k1 = 0.2081

k2 = 0.2187

k3 = 0.2193

k4 = 0.2193

y(0.4000) = 0.419


*/
  
Share: 

 
 
 

Didn't find what you were looking for? Find more on RUNGE-KUTTA 4th ORDER METHOD Or get search suggestion and latest updates.

Lurleen Fischer
Lurleen Fischer author of RUNGE-KUTTA 4th ORDER METHOD is from Frankfurt, Germany.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
Thay Sooooooo from Japan Comment on: Nov 29
#include<stdio.h>
#include <math.h>
#include<conio.h>

/*Hello, i upgrade your code, now the values are correct :) */
//dy/dx = -2*y
#define F(x,y) -2*y

void main()
{
double y0,x0,y1,n,h,f,k1,k2,k3,k4,t1,t2;

printf("\nEnter the value of x0: ");
scanf("%lf",&x0);
printf("\nEnter the value of y0: ");
scanf("%lf",&y0);
printf("\nEnter the value of h: ");
scanf("%lf",&h);
printf("\nEnter the value of last point: ");
scanf("%lf",&n);
for(; x0 < n; x0 = x0 + h)
{
f = F(x0,y0);
k1 = f;

t1 = x0+(h/2);
t2 = y0+(h*(k1/2));
f = F(t1,t2);
k2 = f;

t1 = x0+(h/2);
t2 = y0+(h*(k2/2));
f = F(t1,t2);
k3 = f;

t1 = x0+h;
t2 = y0+((k3*h));
f = F(t1,t2);
k4 = f;

y1 = y0 + h*( k1 + 2*k2 + 2*k3 + k4)/6;
printf("\n\n k1 = %.5lf ",k1);
printf("\n\n k2 = %.5lf ",k2);
printf("\n\n k3 = %.5lf ",k3);
printf("\n\n k4 = %.5lf ",k4);
printf("\n\n y(%.5lf) = %.5lf ",x0+h,y1);
y0=y1;
}
getch();
}

View All Comments