Logo 
Search:

C Programming Articles

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

Runge-Kutta Second Order

Posted By: Luiz Silva     Category: C Programming     Views: 7801

Program of Runge-Kutta Second Order.

Code for Runge-Kutta Second Order in C Programming

#include<stdio.h>
#include<conio.h>
#define MAX 20

float equation(float x,float y)
{
    return(x*y);
}

void main()
{
    FILE *fp;
    int i=1,count=1;
    float lower,upper,y1,h,xvalue[MAX],yvalue[MAX];
    float s,s1,s2;
    fp=fopen("rk_2.dat","w");
    clrscr();
    printf("\n\n");
    fprintf(fp,"\n\n");
    printf("Runge-Kutta Second Order ");
    fprintf(fp,"Runge-Kutta Second Order ");
    printf("\n\n");
    fprintf(fp,"\n\n");
    printf("\nEnter the lower bound of x :  ");
    fprintf(fp,"\nEnter the lower bound of x :  ");
    scanf("%f",&lower);
    fprintf(fp,"%f",lower);
    printf("\nEnter the upper bound of x :  ");
    fprintf(fp,"\nEnter the upper bound of x :  ");
    scanf("%f",&upper);
    fprintf(fp,"%f",upper);
    printf("\nEnter the value of y(lower):  ");
    fprintf(fp,"\nEnter the value of y(lower):  ");
    scanf("%f",&y1);
    fprintf(fp,"%f",y1);
    printf("\nEnter the value of h       :  ");
    fprintf(fp,"\nEnter the value of h       :  ");
    scanf("%f",&h);
    fprintf(fp,"%f",h);
    xvalue[i]=lower;
    yvalue[i]=y1;
    for(i=1;xvalue[i]<=upper;i++)
    {
        xvalue[i+1]=xvalue[i]+h;
        count=count+1;
    }
    for(i=1;i<=count;i++)
    {
        s1=equation(xvalue[i],yvalue[i]);
        s2=equation(xvalue[i]+h,yvalue[i]+(h*s1));
        s=(s1+s2)/2;
        yvalue[i+1]=yvalue[i]+(h*s);
    }
    printf("\n\n");
    fprintf(fp,"\n\n");
    printf("The complete solution of the differential equation is ");
    fprintf(fp,"The complete solution of the differential equation is ");
    printf("\n\n");
    fprintf(fp,"\n\n");
    for(i=1;i<=count;i++)
    {
        printf(" %d  %.4f   %.4f  ",i,xvalue[i],yvalue[i]);
        fprintf(fp," %d  %.4f   %.4f  ",i,xvalue[i],yvalue[i]);
        printf("\n");
        fprintf(fp,"\n");
    }
    fclose(fp);
    getch();
}
  
Share: 


Didn't find what you were looking for? Find more on Runge-Kutta Second Order Or get search suggestion and latest updates.

Luiz   Silva
Luiz Silva author of Runge-Kutta Second Order is from Salvador, Brazil.
 
View All Articles

 
Please enter your Comment

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

 
No Comment Found, Be the First to post comment!