Differential Equation Solution

Euler Method /* this is Euler method to find solution of differential equation of type dy/dx = -x*y with initial condition y(0) = 1.0 */ #include <stdio.h> #include <math.h> main() { int n=1000; int i; float s1,s2,x2,y2; float x1=0.0; float xf=10.0; float h; float y1 = 1.0; float func(float, float);/*=============================================*/ h=(xf-x1)/n; /* h is increament along x axis */ FILE *f1=fopen("x2-y2-euler2.dat","w"); for(i=1; i<=n; i++) { s1=func(x1,y1); x2=x1+h; y2=y1+h*s1; s2=func(x2,y2); printf("%f %f \n",x2,y2); fprintf(f1,"%f %f \n",x2,y2); x1=x2; y1=y2; s1=s2; } return(0); }/*== the function f(x,y) is defined as dy/dx = f(x,y)and f(x,y) =-x*y is written as below ===*/ float func(float x, float y) { float fn; fn=-x*y; return(fn); }
Runge-Kutta 4th Order /*this is Runge Kutta 4 method to find solution of differential equation dy/dx = 2*x*y with initial condition y(0) = 0.5 plot y(x) vs x for 0 <= x <= 1 */ #include <stdio.h> #include <math.h> main() { int n=100; int i; float s,s1,s2,s3,s4,x2,y2; float func(float, float);/* Now put the boundary condition */ float x1 = 0.0; float y1 = 0.5; float xf = 1.0; float h; /* h is increament */ h=(xf-x1)/n; /* h is increament along x axis */ FILE *f1=fopen("data-rk4.dat","w"); for(i=1; i<=n; i++) { s1=func(x1,y1); x2=x1+0.5*h; y2=y1+0.5*h*s1; s2=func(x2,y2); y2=y1+0.5*h*s2; s3=func(x2,y2); x2=x1+h; y2=y1+s3*h; s4=func(x2,y2); s=(s1+2.0*s2+2.0*s3+s4); y2=y1+h*s/6.0; fprintf(f1,"%d %f %f \n",i,x2,y2); x1=x2; y1=y2; } return(0); }/*== the function f(x,y) is defined as dy/dx = f(x,y)and f(x,y) =-x*y is written as below ===*/ float func(float x, float y) { float fn; fn = 2.0*x*y; return(fn); }/*=================================================*/
/*=================================================*/2nd order differential equation/* this is Runge-Kutta 2nd order method to find solution of differential equation d^2y/dx^2 + 2 dy/dx + y = 0 with boundary condition y(0) = 0.0, dy(0)/dx = 1.0 this is the program to solve second order differential equation we take dy/dx= z dz/dx=f(x,y,z) x1 given y(x1)=y1 given and y'(x1) = z1 is given as initial condition the smaller the step size better resultthere i am doing runge-kutta of order 2 @@@@@@@@@@@@###########$$$$$$$$$$$$%%%%%%%%%%%%%%%^^^^^^^^^^^^^^&&&&&&&&&&have a nice day made by SOMENATH JALAL date: 11 Dec, 2017@@@@@@@@@@@@###########$$$$$$$$$$$$%%%%%%%%%%%%%%%^^^^^^^^^^^^^^&&&&&&&&&& */ #include <stdio.h> #include <math.h> main() { int n=1000; int i; float s1,s2,p1,p2,x2,y2,z2; float func(float, float,float);/* Now put the boundary condition */ float x1 = 0.0; float y1 = 0.0; float z1 = 1.0; float xf = 10.0; float h; /* h is increament *//*=============================================*/ h=(xf-x1)/n; /* h is increament along x axis */ FILE *f1=fopen("data-2nd-order-diff-eqn-rk2.dat","w"); /*this is the data file to store x,y,z*/ for(i=1; i<=n; i++) { s1=z1; p1=func(x1,y1,z1); s2=z1+h*p1; y2=y1+h*(s1+s2)/2.0; x2=x1+h; p2=func(x2,y1+h*s1,z1+h*p1); z2=z1+h*(p1+p2)/2.0; fprintf(f1,"%d %f %f \n",i,x1,y1); x1=x2; y1=y2; z1=z2; } return(0); }/*== the function f(x,y) is defined as dy/dx = f(x,y,z)and f(x,y,z) =-x-y*y is written as below ===*/ float func(float x, float y,float z) { float fn; fn=-2.0*z -y; return(fn); }/*=================================================*/
/*=================================================*/simultaneous (two first order) differential equation /* this is Runge Kutta 2nd order method to find solution of simultaneous diffrential equation of type: dy/dx = f(x,y,z) dz/dx = g(x,y,z) with the initial conditions y(0) = 0, z(0) = 1.0 See Book Rajaraman Page-181 for algorithm and page 185 for the exercise problem which I solve here.. ================= the code starts from the below line =============*/ #include <stdio.h> #include <math.h> main() { int n=1000; int i; float s1,s2,p1,p2,x2,y2,z2; float func(float, float,float); float gfn(float, float, float);/* Now put the boundary condition */ float x1 = 0.0; float y1 = 0.0; float z1 = 1.0; float xf = 0.5; float h; /* h is increment *//*=============================================*/ h=(xf-x1)/n; /* h is increment along x axis */ FILE *f1=fopen("data--simultaneous-eqn-rk2.dat","w"); /*this is the data file to store x,y,z*/ for(i=1; i<=n; i++) { s1=func(x1,y1,z1); p1=gfn(x1,y1,z1); x2=x1+h; s2=func(x2,y1+h*s1,z1+h*p1); p2=gfn(x2,y1+h*s1,z1+h*p1); y2=y1+h*(s1+s2)/2.0; z2=z1+h*(p1+p2)/2.0; fprintf(f1,"%d %f %f %f \n",i,x2,y2,z2); x1=x2; y1=y2; z1=z2; } return(0); } /*== the function f(x,y) is defined as dy/dx = f(x,y,z)and f(x,y,z) =-x-y*y is written as below ===*/ float func(float x, float y,float z) { float fn; fn=-x-y*z; return(fn); }/*== the function f(x,y) is defined as dz/dx = g(x,y,z)and g(x,y,z) =-y-x*z is written as below ===*/ float gfn(float x, float y,float z) { float gn; gn=-y-x*z; return(gn); }/*=================================================*/