Could someone please help me im trying to
Use the fourth order Runge-Kutta algorithm to solve the differential equation.

dy/dx = -y, y(0) = 1

nelow is the program I currently have, and do not know how to corect it, I shouls get altering values of y as x goes from 0 to 5 at increments of 0.1, and then when plotted in a graph should produce a curve... Currently my program does the x bit right but falls apart with y in that i have problably missed a step required to get sensible values.... im very new to this programming so it may even be a obvious mistake..... any help would be awesome

/* A Runge-Kutta Method for solving Differential Equations*/
/* dy/dt = -y(t), y(0)=1, 0<=x<=5, start h=0.1*/

#include <stdio.h>

#define dist 0.1		/* stepsize */
#define xf 5			/* max for x */ 

FILE *output;			/* internal filename */

double rkutta( double x, double y, double h);   /*Runge Kutta Function */
double F(double x, double y);                   /*Function derivative*/

    double x, y, h;
    int n;
    output=fopen("xydata.dat", "w");	/* External filename */
    y=1;				            /* Initial condition */
    fprintf(output, "0\t%f\n", y);

    for (n=0;dist*n<=xf;n++)	    /* The time loop */
          y-=rkutta(x, y, dist);
   fprintf (output, "%f\t%f\n", x, y);

}                   /* End of main function*/

double rkutta(double x, double y, double h)  /*Called on RK function*/
    double yn, k1, k2, k3, k4;
    double H = h/2.0;
    k1 = (h*F(x, y));
    k2 = (h*F(x+H, y+(k1/2)));
    k3 = (h*F(x+H, y+(k2/2)));
    k4 = (h*F(x+h, y+k3));

double F(double x, double y)                /*Called on derivative*/
    return (y);
8 Years
Discussion Span
Last Post by ArkM

Too many errors... That's your homework, not mine, so I can't write the right code for you. Some (free ;) ) advices:
1. Next time use code tag with language specifier:
[code=c] ... sources ...

2. Reread Runge-Kutta RK4 method description, for example:
3. Declare all variables as local as possible. For example, no need in a global variable output in your code at all.
4. Never calculate grid values of x by addition to initial value: rounding errors might distort end of grid test. One of possible (but not the best) patters (adopted for your case):

double h = 0.1;
double h2 = h*0.5; /* optimization for RK4 calcs ;) */
double h6 = h/6.0; /* ... */
double x0 = 0.0;
double xn = 5.0;
double range = xn - x0;
int N = (int)(range/h+0.5); /* number of intervals needed; round it */
double x = x0;
double y = 1.0;
int i;
h = range / N; /* correct h value */
/* print the left boundary values */
for (i = 0; i < N; ++i) { 
   ... /* calc next RK4 step */
   x = x0 + h*i;           /* Correct x-value: no adds, multiply! */
   /* print the next value */

5. Avoid int constants using in double expressions. Remember int division effects.
6. Floating point values are approximations of real numbers. Remember: the only relatively safe direct comparison of doubles are x == 0.0 and x != 0.0 ...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.