Project
Use the fourth order Runge-Kutta algorithm to solve the differential equation.
dy/dx = -y, y(0) = 1
thats the problem baiscally, below is the code I have got so far and so far as I am a complete beginner to c/c++ I'm having great difficulty getting this to work.
Please any help on this would be greatly appreciated as i currently feeling like im banging my head against a large c shape wall
Code:#include<stdio.h> #define xs 0.1 #define xf 5 FILE *output; main() { double t, y, h, y0 , yn; double f(double x, double y); double runge(double x, double y) int j; output=fopen("data3.dat", "w"); /* external filename */ y0 = 1; h = 0.1; yn = y0; fprintf(output, "0\t%f\n", y); for (j=0; j*xs<=xf; j++) /* the time loop */ { t=j*xs; y-=runge(t, y,); fprintf(output, "%f\t%f\n", t, y); } fclose(output); } double runge(double x, double y) { double K1 = (H * f(x,y)); double K2 = (H * f((x + 1 / 2 * H), (y + 1 / 2 * K1))); double K3 = (H * f((x + 1 / 2 * H), (y + 1 / 2 * K2))); double K4 = (H * f((x + H), (y + K3))); double runge = (y + (1 / 6) * (K1 + 2 * K2 + 2 * K3 + K4)); return runge; } double f(double x, double y) { double f = (-y); return f; }



LinkBack URL
About LinkBacks


