I'm trying to solve a numerical problem. Ordinary Differential Equation and this problem have 3 methods to solve it. I've type out 3 individual method and try to combine them into 1 code. I'm using Visio studio 2005.
#include "stdafx.h"
#include "math.h"
float f1(float t,float x, float v);
float f2(float t,float x, float v);
int _tmain(int argc, _TCHAR* argv[])//Euler method
{
FILE *wPtr;
wPtr=fopen("results.xls","w");
float t0,x0,v0,tn,n,h;
float k1,k2,t,x,v;
printf("Please Inset initial time, t0 = ");
scanf ("%f",&t0);
printf("Please Insert initial displacement, x0 = ");
scanf ("%f",&x0);
printf("Please insert initial velocity, v0= ");
scanf ("%f",&v0);
printf("Please insert final time,tn= ");
scanf ("%f",&tn);
printf("Please insert -1 to end the program.");
printf("\n");
printf("\nPlease insert n value= ");
scanf ("%f",&n);
while (n!=-1)
{
printf("\nt\t\tx\t\tv\n");
h=(tn-t0)/n;
t=t0;
x=x0;
v=v0;
while(t<tn)
{
k1=h*f1(t,x,v);
k2=h*f2(t,x,v);
x=x+k1;
v=v+k2;
t=t+h;
printf("%f %f %f\n",t,x,v);
fprintf(wPtr,"%f %f %f\n",t,x,v);
}
printf("\nPlease insert n value= ");
scanf ("%f",&n);
}
fclose(wPtr);
printf ("Thankyou for using.\n\n");
return 0;
}
float f1(float t,float x, float v)
{
return v;
}
float f2(float t, float x, float v)
{
return (0.5*cos(0.5*t)-1*x-0.5*pow(x,3)-0.4*v)/1;
}
================================================== ================================================== ================================================== =====
#include "stdafx.h"
#include "math.h"
float f1(float t,float x, float v);
float f2(float t,float x, float v);
int _tmain(int argc, _TCHAR* argv[])//Huen's method
{
FILE *wPtr;
wPtr=fopen("results.xls","w");
float t0,x0,v0,tn,n,h;
float dydx11,dydx12,dydx21,dydx22,slope1,slope2,t,x,xe,v e,v;
printf("Please Inset initial time, t0 = ");
scanf ("%f",&t0);
printf("Please Insert initial displacement, x0 = ");
scanf ("%f",&x0);
printf("Please insert initial velocity, v0= ");
scanf ("%f",&v0);
printf("Please insert final time,tn= ");
scanf ("%f",&tn);
printf("Please insert -1 to end the program.");
printf("\n");
printf("\nPlease insert n value= ");
scanf ("%f",&n);
while(n!=-1)
{
printf("\nt\t\tx\t\tv\n");
h=(tn-t0)/n;
t=t0;
x=x0;
v=v0;
while(t<tn)
{
dydx11=f1(t,x,v);
dydx12=f2(t,x,v);
xe=x+h*dydx11;
ve=v+h*dydx12;
dydx21=f1(t+h,xe,ve);
dydx22=f2(t+h,xe,ve);
slope1=(dydx11+dydx21)/2;
slope2=(dydx12+dydx22)/2;
x=x+h*slope1;
v=v+h*slope2;
t=t+h;
printf("%f %f %f\n",t,x,v);
fprintf(wPtr,"%f %f %f\n",t,x,v);
}
printf("\nPlease insert n value= ");
scanf ("%f",&n);
}
fclose(wPtr);
printf ("Thankyou for using.\n\n");
return 0;
}
float f1(float t,float x, float v)
{
return v;
}
float f2(float t, float x, float v)
{
return (0.5*cos(0.5*t)-1*x-0.5*pow(x,3)-0.4*v)/1;
}
================================================== ================================================== ================================================== =====
#include "stdafx.h"
#include "math.h"
float f1(float t,float x, float v);
float f2(float t,float x, float v);
int _tmain(int argc, _TCHAR* argv[])//Range-Kutta method
{
FILE *wPtr;
wPtr=fopen("results.xls","w");
float t0,x0,v0,tn,n,h;
float k11,k12,k21,k22,k31,k32,k41,k42,slope1,slope2,t,x, v;
printf("Please Inset initial time, t0 = ");
scanf ("%f",&t0);
printf("Please Insert initial displacement, x0 = ");
scanf ("%f",&x0);
printf("Please insert initial velocity, v0= ");
scanf ("%f",&v0);
printf("Please insert final time,tn= ");
scanf ("%f",&tn);
printf("Please insert EOF to end the program.");
printf("\n");
printf("\nPlease insert n value= ");
scanf ("%f",&n);
while(n!=-1)
{
printf("\nt\t\tx\t\tv\n");
h=(tn-t0)/n;
t=t0;
x=x0;
v=v0;
while(t<tn)
{
k11=f1(t,x,v);
k12=f2(t,x,v);
k21=f1(t+h/2,x+k11*h/2, v+k12*h/2);
k22=f2(t+h/2,x+k11*h/2, v+k12*h/2);
k31=f1(t+h/2,x+k21*h/2, v+k22*h/2);
k32=f2(t+h/2,x+k21*h/2, v+k22*h/2);
k41=f1(t+h, x+k31*h, v+k32*h);
k42=f2(t+h, x+k31*h, v+k32*h);
slope1=(k11+2*k21+2*k31+k41)/6;
slope2=(k12+2*k22+2*k32+k42)/6;
x=x+h*slope1;
v=v+h*slope2;
t=t+h;
printf("%f %f %f\n",t,x,v);
fprintf(wPtr,"%f %f %f\n",t,x,v);
}
printf("\nPlease insert n value= ");
scanf ("%f",&n);
}
fclose(wPtr);
printf ("Thankyou for using.\n\n");
return 0;
}
float f1(float t,float x, float v)
{
return v;
}
float f2(float t, float x, float v)
{
return (0.5*cos(0.5*t)-1*x-0.5*pow(x,3)-0.4*v)/1;
}
================================================== ======
and here's my combined code:
#include "stdafx.h"
#include "math.h"
float dxdt(float t, float x, float v);
float dvdt(float t, float x, float v);
FILE *wEuler;
FILE *wHuen;
FILE *wRK;
float Euler(float t0, float x0, float v0, float tn,int n )
{
float t,x,v,h;
float k1,k2;
printf("\nt\t\tx\t\tv\n");
fprintf(wEuler,"\nt\tx\tv\n");
h=(tn-t0)/n;
t=t0;
x=x0;
v=v0;
while(t<tn)
{
k1=h*dxdt(t,x,v);
k2=h*dvdt(t,x,v);
x=x+k1;
v=v+k2;
t=t+h;
printf("%f %f %f\n",t,x,v);
fprintf(wEuler,"%f %f %f\n",t,x,v);
}
return 0;
}
float Huen(float t0, float x0, float v0, float tn,int n )
{
float t,x,v,h;
float dydx11,dydx12,dydx21,dydx22,slope1,slope2,xe,ve;
printf("\nt\t\tx\t\tv\n");
fprintf(wHuen,"\n\nt\tx\tv\n");
h=(tn-t0)/n;
t=t0;
x=x0;
v=v0;
while(t<tn)
{
dydx11=dxdt(t,x,v);
dydx12=dvdt(t,x,v);
xe=x+h*dydx11;
ve=v+h*dydx12;
dydx21=dxdt(t+h,xe,ve);
dydx22=dvdt(t+h,xe,ve);
slope1=(dydx11+dydx21)/2;
slope2=(dydx12+dydx22)/2;
x=x+h*slope1;
v=v+h*slope2;
t=t+h;
printf("%f %f %f\n",t,x,v);
fprintf(wHuen,"%f %f %f\n",t,x,v);
}
return 0;
}
float RK(float t0, float x0, float v0, float tn,int n )
{
float t,x,v,h;
float k11,k12,k21,k22,k31,k32,k41,k42,s1,s2;
printf("\nt\t\tx\t\tv\n");
fprintf(wRK,"\nt\tx\tv\n");
h=(tn-t0)/n;
t=t0;
x=x0;
v=v0;
while(t<tn)
{
k11=dxdt(t,x,v);
k12=dvdt(t,x,v);
k21=dxdt(t+h/2,x+k11*h/2, v+k12*h/2);
k22=dvdt(t+h/2,x+k11*h/2, v+k12*h/2);
k31=dxdt(t+h/2,x+k21*h/2, v+k22*h/2);
k32=dvdt(t+h/2,x+k21*h/2, v+k22*h/2);
k41=dxdt(t+h, x+k31*h, v+k32*h);
k42=dvdt(t+h, x+k31*h, v+k32*h);
s1=(k11+2*k21+2*k31+k41)/6;
s2=(k12+2*k22+2*k32+k42)/6;
x=x+h*s1;
v=v+h*s2;
t=t+h;
printf("%f %f %f\n",t,x,v);
fprintf(wRK,"%f %f %f\n",t,x,v);
}
return 0;
}
float dxdt(float t,float x, float v)
{
return v;
}
float dvdt(float t, float x, float v)
{
return (0.5*cos(0.5*t)-1*x-0.5*pow(x,3)-0.4*v)/1;
}
int _tmain(int argc, _TCHAR* argv[])
{
wEuler=fopen("Euler.xls","w");
wHuen=fopen("Heun.xls","w");
wRK=fopen("Range-Kutta.xls","w");
// char selection;
float t0,x0,v0,tn,h,t,x,v;
int n;
// float k1,k2;
// float dydx11,dydx12,dydx21,dydx22,slope1,slope2,xe,ve;
// float k11,k12,k21,k22,k31,k32,k41,k42,s1,s2;
//main:
printf("\nPlease select the option:\n\n");
printf("==================Main Menu===============\n");
printf("** Option 1: Different time steps **\n");
printf("** Option 2: Same time steps **\n");
printf("** Enter EOF to end the program **\n");
printf("========================================== \n\n");
printf("Please Inset initial time, t0 = ");
scanf_s ("%f",&t0);
printf("Please Insert initial displacement, x0 = ");
scanf_s ("%f",&x0);
printf("Please insert initial velocity, v0= ");
scanf_s ("%f",&v0);
printf("Please insert final time,tn= ");
scanf_s ("%f",&tn);
printf("Please insert -1 to end the program.");
printf("\n");
again: printf("\nPlease insert n value= ");
scanf_s ("%f",&n);
if(n!=-1)
{
Euler(t0,x0,v0,tn,n);
Huen(t0,x0,v0,tn,n );
RK(t0,x0,v0,tn,n);
goto again;
}
else
{
printf ("Thankyou for using.\n\n");
}
fclose(wEuler);
fclose(wHuen);
fclose(wRK);
return 0;
}
The program give me wrong answers and looping. Any advise?