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*/

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

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

   fclose(output);
}                   /* 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));
    return(y+=(y+(k1+2*k2+2*k3+k4)*1/6));
}
   

double F(double x, double y)                /*Called on derivative*/
{
    return (y);
}

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 ...

[/code]
2. Reread Runge-Kutta RK4 method description, for example:
http://en.wikipedia.org/wiki/Runge-Kutta
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 ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.