// Runge-Kutta 4 solver of 1st order ODE #include #include double f(double t, double y); double analyticalsol(double x); void RK4(double dt, double t0, double y0, double tmax); void plotfunction(double x0, double x1, int N); // Driver program int main() { // Initial values RK4(0.5,0.0,1.0,50); plotfunction(0.0, 50,1000); return 0; } void RK4(double dt, double t0, double y0, double tmax) { double y,t,k1,k2,k3,k4; FILE *output; output = fopen("RK4.dat", "w");//opening file. t=t0; y=y0; fprintf(output,"%.15f\t%.15f\n",t0,y0); while (t <= tmax) { k1=dt*f(t,y); k2=dt*f(t+0.5*dt,y+0.5*k1); k3=dt*f(t+0.5*dt,y+0.5*k2); k4=dt*f(t+dt,y+k3); y=y+(k1+2.0*k2+2.0*k3+k4)/6.0; t=t+dt; fprintf(output,"%.15f\t%.15f\n",t,y); } fclose(output);//closing file. } void plotfunction(double x0, double x1,int N) { int i; double x,dx; FILE *output; dx=(x1-x0)/N; output = fopen("function.dat", "w");//opening file. for(i=0;i