试图在1个代码中组合三个代码。错误出来了 [英] Trying to combine three codes in 1 code. Error came out

查看:51
本文介绍了试图在1个代码中组合三个代码。错误出来了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力解决数字问题。常微分方程和这个问题有3种方法可以解决它。我输入了3个单独的方法并尝试将它们组合成1个代码。我正在使用Visio studio 2005.


#include" stdafx.h"

#include" math.h"



浮点数f1(浮点数t,浮点数x,浮点数v);

浮点数f2(浮点数t,浮点数x,浮点数v);



int _tmain(int argc,_TCHAR * argv [])//欧拉方法

{

FILE * wPtr;

wPtr = fopen(" results.xls"," w");


float t0,x0,v0,tn,n,h;

浮点k1,k2,t,x,v;


printf("请插入初始时间,t0 =");

scanf("%f"& t0);

printf(" Please Insert initial displacement,x0 =");

scanf(" %f"& x0);

printf(" Please insert initial velocity,v0 =");

scanf("%f"&)& v0);

printf(请插入最终时间,tn =");

scanf("%f",& tn);

printf("请插入-1以结束程序。&quo t;);

printf(" \ n");

printf(" \ nPlease insert n value =");

scanf("%f"& n);


while(n!= - 1)

{

printf(" \\\
t\t\tx\t\tv \ n");

h =(tn-t0)/ n;

t = t0;

x = x0;

v = v0;


而(t
{

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 \ nn,t,x,v);

fprintf(wPtr,"%f%f%f \ n,t ,x,v);


}


printf(" \\\
Please insert n value =");

scanf("%f",& n);

}


fclose(wPtr);

printf(Thankyou for using.\\\
\ n) ;


返回0;

}


float f1(float t,float x,浮动v)

{

返回v;

}


浮动f2(浮动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方法

{

FILE * wPtr;

wPtr = fopen(" results.xls"," w");


浮点t0,x0,v 0,tn,n,h;

浮点dydx11,dydx12,dydx21,dydx22,slope1,slope2,t,x,xe,ve,v;


printf(请插入初始时间,t0 =);

scanf("%f"& t0);

printf(" Please插入初始位移,x0 =");

scanf("%f"& x0);

printf("请插入初始速度,v0 = ");

scanf("%f"& v0);

printf(" Please insert final time,tn =");

scanf("%f",& tn);

printf(请插入-1以结束程序。);

printf(" \ n");

printf(" \ nPlease insert n value =");

scanf("%f" ,& n);


while(n!= - 1)

{


printf(" ; \\\\\\\\\\\\\\\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\

x = x0;

v = v0;


whil e(t
{

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) ;

返回0;

}


float f1(float t,float x,float v)

{

返回v;

}


浮点数f2(浮点数t,浮点数x,浮点数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方法

{

FILE * wPtr;

wPtr = fopen(" results.xls"," w");


浮点t0,x0,v0,t n,n,h;

浮点k11,k12,k21,k22,k31,k32,k41,k42,slope1,slope2,t,x,v;


printf(" Please Inset initial time,t0 =");

scanf("%f"& t0);

printf(" ;请插入初始位移,x0 =");

scanf("%f"& x0);

printf("请插入初始速度, v0 =");

scanf("%f"& v0);

printf(" Please insert final time,tn =");

scanf("%f"& tn);

printf("请插入EOF以结束程序。);

printf(" \ n");

printf(" \ nPlease insert n value =");

scanf("%f" ;,& n);


while(n!= - 1)

{


printf( \\\\\\\\\\ ;

x = x0;

v = v0;


while(t& lt; 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) ;

返回0;

}


float f1(float t,float x,float v)

{

返回v;

}


浮点数f2(浮点数t,浮点数x,浮点数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 dxdt(float t,float x ,浮动v);

浮动dvdt(浮动t,浮动x,浮动v);


文件* wEuler;

FILE * wHuen;

FILE * wRK;


浮点数Euler(浮点数t0,浮点数x0,浮点数v0,浮点数tn,int n)

{


浮动t,x,v,h;

浮动k1,k2;


printf(" \\\
t\t\tx \t\tv\\\
");

fprintf(wEuler," \ antt \ 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);


}


返回0 ;

}


浮Huen(浮点数t0,浮点数x0,浮点数v0,浮点数tn,int n)

{


浮动t,x,v,h;

浮动dydx11,dydx12,dydx21,dydx22,slope1,slope2,xe,ve;


printf(" \\\
t\t\tx\t\tv \ n");

fprintf(wHuen," \ nn \\ tx \ tv \ n");


h =(tn-t0)/ n;

t = t0;

X = X0 ;

v = v0;


而(t
{

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 \ nn,t,x,v);

fprintf(wHuen,"%f %f%f\ n,t,x,v);


}


返回0;

}


浮点数RK(浮点数t0,浮点数x0,浮点数v0,浮点数tn,int n)

{


浮动t,x,v,h;

浮动k11,k12,k21,k22,k31,k32,k41,k42,s1,s2;


printf(" \\\
t\t\tx\t\tv \ n");

fprintf(wRK," \\\\ \\ tv \ n");


h =(tn-t0)/ n;

t = t0;

x = x0;

v = v0;


而(t
{

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);



}


返回0;

}


float dxdt(float t,float x,float v)

{

return v;

}

浮动dvdt(浮动t,浮动x,浮动v)

{

返回(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选择;

float t0,x0,v0,tn,h,t,x,v;

int n;

//浮动k1,k2;

//浮动dydx11,dydx12,dydx21,dydx22,slope1,slope2,xe,ve;

//浮动k11,k12,k21,k22, k31,k32,k41,k42,s1,s2;


//主要:


printf(" \ n请选择选项) :\ n \ n");

printf(" ==================主菜单======== ======= \ n");

printf(" **选项1:不同的时间步长** \ n);

printf (**选项2:同一时间步骤** \ n);

printf(**输入EOF结束程序** \ n);

printf(" ================== ======================== \ n \ nn");


printf (请插入初始时间,t0 =);

scanf_s("%f",& t0);

printf(" Please Insert initial displa,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");

: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);


转到再次;

}


其他

{

printf(" Thankyou for use.\\\
\\\
");

}


fclose(wEuler);

fclose(wHuen) ;

fclose(wRK);

返回0;

}


该计划给我错误的答案和循环。有什么建议吗?

解决方案

你确定它在哪个循环中无限循环,或者你想让我们为你做这个?


ERM。它不应该循环程序。从t0到tn的oni循环(假设0到100,时间步长为h)并要求用户输入另一个n值或-1来结束程序


i don''我知道我的组合代码有什么问题,因为当我尝试使用1方法时它似乎是正确的。


erm。它不应该循环程序。从t0到tn的oni循环(假设0到100,时间步长为h)并要求用户输入另一个n值或-1来结束程序


i don''我知道我的组合代码有什么问题,因为当我尝试使用1方法时它似乎是正确的。


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?

解决方案

Did you determine in which loop exactly it loops infinitely, or you''d rather want us do it for you?


erm. it should not loop the program. it oni loop from t0 till tn (let say 0 to 100 with time steps of h) and ask the user to input another n value or -1 to end the program

i don''t know what''s wrong with my combined code because it seems to be correct when i try to do with 1 method.


erm. it should not loop the program. it oni loop from t0 till tn (let say 0 to 100 with time steps of h) and ask the user to input another n value or -1 to end the program

i don''t know what''s wrong with my combined code because it seems to be correct when i try to do with 1 method.


这篇关于试图在1个代码中组合三个代码。错误出来了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆