Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用正确的方法解方程并保存? 我是C++的新学习者。实际上,我将尝试找出每个时间步(dt=0.00001)中的总力、速度、位置和总时间,并从求解简单方程中保存myfile1、myfile2、myfile3、myfile4。我面临的问题是要找出准确的数值。当我运行程序时,发现了分段错误。那么我该如何解决这个问题呢?我附上下面给出的程序:有人能帮我吗 //C++ programming for selection random number #include <cstdlib> #include <iostream> #include <fstream> #include <ctime> #include <sstream> #include <string> #include <iomanip> #include <cmath> //%stifness double kp=1000; //%gravitational double g=-9.81; double x[10000], y[10000], z[10000]; double Fx[10000],Fy[10000], Fz[10000]; double vx[10000],vy[10000], vz[10000] ; double ax[10000],ay[10000], az[10000] ; double force[10000]; const double PI=4.0*atan(1.0); using namespace std; int main() { srand((unsigned) time(NULL)); //open output file ofstream myfile1; myfile1.open("1.dat"); ofstream myfile2; myfile2.open("2.dat"); ofstream myfile3; myfile3.open("3.dat"); ofstream myfile4; myfile4.open("4.dat"); // %particle parameter double dt=1e-5; double Ntimestep=1000; //particle properties double R=0.003; double Dens=2600; int npart=1; //Particle Creation double Mass=(Dens*4*PI*(pow(R,3))/3); //m=(4/3)* Dens*Pi*R^3 cout<<"Mass="<< Mass<<endl; //initial position of the particle x[0]=0; y[0] =0.01; z[0] =0; // movement of the particle // Particle initial velocity and forces Fx[0]=0; Fy[0]=0; Fz[0]=0; vx[0]=0; vy[0]=-0.5; vz[0]=0; force[0]=0; // Relation between dashport coefficient and coefficient of restitution double exp=0.9; double lnexp=0.10536; double Eta= ((2*sqrt(Mass*kp)*lnexp)/(sqrt(pow(PI,2)+pow(lnexp,2)))); //Time step int t=0; for (int i=0;i<Ntimestep;i++) { // calculate particle to wall contact force if (y[i]<R) { Fy[i]=(-kp*(y[i]-R))-Eta*vy[i]; } // Calculate initial acceleration ay[i]=(Fy[i]/Mass)+g; //force[i]+=force[1]; force[i+1]=ay[i]*Mass; //cout<<"Total_Force="<<force<<endl; // update valocity and displacement/location vy[i+1]=(vy[i]+(ay[i]*dt)); y[i+1]=y[i]+vy[i]*dt+0.5*ay[i]*pow(dt,2); Fy[i]=0; t=t+1; double time=t*dt; //...............output/save file.............................. cout<<"Total force="<<force<<endl; myfile1<<"Total force="<<force<<endl; cout<<"velocity="<<vy<<endl; myfile2<<"velocity="<<vy<<endl; cout<<"location="<<y<<endl; myfile3<<"location="<<y<<endl; cout<<"Total time"<<time<<endl; myfile4<<"Total time="<<time<<endl; } //system ("PAUSE"); cin.ignore(); cin.get(); myfile1.close(); myfile2.close(); myfile3.close(); myfile4.close(); return 0; } //选择随机数的C++编程 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 //%僵硬 双倍kp=1000; //%引力的 双g=-9.81; 双x[10000],y[10000],z[10000]; 双外汇[10000],财年[10000],Fz[10000]; 双vx[10000],vy[10000],vz[10000]; 双轴[10000],ay[10000],az[10000]; 双重力量[10000]; 常数双π=4.0*atan(1.0); 使用名称空间std; int main() { srand((无符号)时间(NULL)); //打开输出文件 流myfile1; myfile1.open(“1.dat”); 流myfile2; myfile2.open(“2.dat”); 流myfile3; myfile3.open(“3.dat”); 流myfile4; myfile4.open(“4.dat”); //%粒子参数 双dt=1e-5; 双Ntimestep=1000; //粒子特性 双R=0.003; 双穴=2600; int npart=1; //粒子生成 双质量=(密度*4*PI*(功率(R,3))/3);/m=(4/3)*密度*PI*R^3 我假设你的Ntimestep的原始值是10000(你的C型数组的尺寸x,y,z,Fx,Fy,Fz,vx,vy,vz,和力),而不是实际的1000_C++ - Fatal编程技术网

如何用正确的方法解方程并保存? 我是C++的新学习者。实际上,我将尝试找出每个时间步(dt=0.00001)中的总力、速度、位置和总时间,并从求解简单方程中保存myfile1、myfile2、myfile3、myfile4。我面临的问题是要找出准确的数值。当我运行程序时,发现了分段错误。那么我该如何解决这个问题呢?我附上下面给出的程序:有人能帮我吗 //C++ programming for selection random number #include <cstdlib> #include <iostream> #include <fstream> #include <ctime> #include <sstream> #include <string> #include <iomanip> #include <cmath> //%stifness double kp=1000; //%gravitational double g=-9.81; double x[10000], y[10000], z[10000]; double Fx[10000],Fy[10000], Fz[10000]; double vx[10000],vy[10000], vz[10000] ; double ax[10000],ay[10000], az[10000] ; double force[10000]; const double PI=4.0*atan(1.0); using namespace std; int main() { srand((unsigned) time(NULL)); //open output file ofstream myfile1; myfile1.open("1.dat"); ofstream myfile2; myfile2.open("2.dat"); ofstream myfile3; myfile3.open("3.dat"); ofstream myfile4; myfile4.open("4.dat"); // %particle parameter double dt=1e-5; double Ntimestep=1000; //particle properties double R=0.003; double Dens=2600; int npart=1; //Particle Creation double Mass=(Dens*4*PI*(pow(R,3))/3); //m=(4/3)* Dens*Pi*R^3 cout<<"Mass="<< Mass<<endl; //initial position of the particle x[0]=0; y[0] =0.01; z[0] =0; // movement of the particle // Particle initial velocity and forces Fx[0]=0; Fy[0]=0; Fz[0]=0; vx[0]=0; vy[0]=-0.5; vz[0]=0; force[0]=0; // Relation between dashport coefficient and coefficient of restitution double exp=0.9; double lnexp=0.10536; double Eta= ((2*sqrt(Mass*kp)*lnexp)/(sqrt(pow(PI,2)+pow(lnexp,2)))); //Time step int t=0; for (int i=0;i<Ntimestep;i++) { // calculate particle to wall contact force if (y[i]<R) { Fy[i]=(-kp*(y[i]-R))-Eta*vy[i]; } // Calculate initial acceleration ay[i]=(Fy[i]/Mass)+g; //force[i]+=force[1]; force[i+1]=ay[i]*Mass; //cout<<"Total_Force="<<force<<endl; // update valocity and displacement/location vy[i+1]=(vy[i]+(ay[i]*dt)); y[i+1]=y[i]+vy[i]*dt+0.5*ay[i]*pow(dt,2); Fy[i]=0; t=t+1; double time=t*dt; //...............output/save file.............................. cout<<"Total force="<<force<<endl; myfile1<<"Total force="<<force<<endl; cout<<"velocity="<<vy<<endl; myfile2<<"velocity="<<vy<<endl; cout<<"location="<<y<<endl; myfile3<<"location="<<y<<endl; cout<<"Total time"<<time<<endl; myfile4<<"Total time="<<time<<endl; } //system ("PAUSE"); cin.ignore(); cin.get(); myfile1.close(); myfile2.close(); myfile3.close(); myfile4.close(); return 0; } //选择随机数的C++编程 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 //%僵硬 双倍kp=1000; //%引力的 双g=-9.81; 双x[10000],y[10000],z[10000]; 双外汇[10000],财年[10000],Fz[10000]; 双vx[10000],vy[10000],vz[10000]; 双轴[10000],ay[10000],az[10000]; 双重力量[10000]; 常数双π=4.0*atan(1.0); 使用名称空间std; int main() { srand((无符号)时间(NULL)); //打开输出文件 流myfile1; myfile1.open(“1.dat”); 流myfile2; myfile2.open(“2.dat”); 流myfile3; myfile3.open(“3.dat”); 流myfile4; myfile4.open(“4.dat”); //%粒子参数 双dt=1e-5; 双Ntimestep=1000; //粒子特性 双R=0.003; 双穴=2600; int npart=1; //粒子生成 双质量=(密度*4*PI*(功率(R,3))/3);/m=(4/3)*密度*PI*R^3 我假设你的Ntimestep的原始值是10000(你的C型数组的尺寸x,y,z,Fx,Fy,Fz,vx,vy,vz,和力),而不是实际的1000

如何用正确的方法解方程并保存? 我是C++的新学习者。实际上,我将尝试找出每个时间步(dt=0.00001)中的总力、速度、位置和总时间,并从求解简单方程中保存myfile1、myfile2、myfile3、myfile4。我面临的问题是要找出准确的数值。当我运行程序时,发现了分段错误。那么我该如何解决这个问题呢?我附上下面给出的程序:有人能帮我吗 //C++ programming for selection random number #include <cstdlib> #include <iostream> #include <fstream> #include <ctime> #include <sstream> #include <string> #include <iomanip> #include <cmath> //%stifness double kp=1000; //%gravitational double g=-9.81; double x[10000], y[10000], z[10000]; double Fx[10000],Fy[10000], Fz[10000]; double vx[10000],vy[10000], vz[10000] ; double ax[10000],ay[10000], az[10000] ; double force[10000]; const double PI=4.0*atan(1.0); using namespace std; int main() { srand((unsigned) time(NULL)); //open output file ofstream myfile1; myfile1.open("1.dat"); ofstream myfile2; myfile2.open("2.dat"); ofstream myfile3; myfile3.open("3.dat"); ofstream myfile4; myfile4.open("4.dat"); // %particle parameter double dt=1e-5; double Ntimestep=1000; //particle properties double R=0.003; double Dens=2600; int npart=1; //Particle Creation double Mass=(Dens*4*PI*(pow(R,3))/3); //m=(4/3)* Dens*Pi*R^3 cout<<"Mass="<< Mass<<endl; //initial position of the particle x[0]=0; y[0] =0.01; z[0] =0; // movement of the particle // Particle initial velocity and forces Fx[0]=0; Fy[0]=0; Fz[0]=0; vx[0]=0; vy[0]=-0.5; vz[0]=0; force[0]=0; // Relation between dashport coefficient and coefficient of restitution double exp=0.9; double lnexp=0.10536; double Eta= ((2*sqrt(Mass*kp)*lnexp)/(sqrt(pow(PI,2)+pow(lnexp,2)))); //Time step int t=0; for (int i=0;i<Ntimestep;i++) { // calculate particle to wall contact force if (y[i]<R) { Fy[i]=(-kp*(y[i]-R))-Eta*vy[i]; } // Calculate initial acceleration ay[i]=(Fy[i]/Mass)+g; //force[i]+=force[1]; force[i+1]=ay[i]*Mass; //cout<<"Total_Force="<<force<<endl; // update valocity and displacement/location vy[i+1]=(vy[i]+(ay[i]*dt)); y[i+1]=y[i]+vy[i]*dt+0.5*ay[i]*pow(dt,2); Fy[i]=0; t=t+1; double time=t*dt; //...............output/save file.............................. cout<<"Total force="<<force<<endl; myfile1<<"Total force="<<force<<endl; cout<<"velocity="<<vy<<endl; myfile2<<"velocity="<<vy<<endl; cout<<"location="<<y<<endl; myfile3<<"location="<<y<<endl; cout<<"Total time"<<time<<endl; myfile4<<"Total time="<<time<<endl; } //system ("PAUSE"); cin.ignore(); cin.get(); myfile1.close(); myfile2.close(); myfile3.close(); myfile4.close(); return 0; } //选择随机数的C++编程 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 //%僵硬 双倍kp=1000; //%引力的 双g=-9.81; 双x[10000],y[10000],z[10000]; 双外汇[10000],财年[10000],Fz[10000]; 双vx[10000],vy[10000],vz[10000]; 双轴[10000],ay[10000],az[10000]; 双重力量[10000]; 常数双π=4.0*atan(1.0); 使用名称空间std; int main() { srand((无符号)时间(NULL)); //打开输出文件 流myfile1; myfile1.open(“1.dat”); 流myfile2; myfile2.open(“2.dat”); 流myfile3; myfile3.open(“3.dat”); 流myfile4; myfile4.open(“4.dat”); //%粒子参数 双dt=1e-5; 双Ntimestep=1000; //粒子特性 双R=0.003; 双穴=2600; int npart=1; //粒子生成 双质量=(密度*4*PI*(功率(R,3))/3);/m=(4/3)*密度*PI*R^3 我假设你的Ntimestep的原始值是10000(你的C型数组的尺寸x,y,z,Fx,Fy,Fz,vx,vy,vz,和力),而不是实际的1000,c++,C++,对于实际的Ntimestep=1000,我看不到分段错误的情况;但是对于Ntimestep=10000,当for循环中的I为9999(最后一次迭代)时,在以下说明中 force[i+1]=ay[i]*Mass; vy[i+1]=(vy[i]+(ay[i]*dt)); y[i+1]=y[i]+vy[i]*dt+0.5*ay[i]*pow(dt,2); >在代码> > >代码>中,在10000位中,在 y>代码>中,这是坏的(你是C++学习者,但我想你知道你可以读/写一个 x x[n] <

对于实际的
Ntimestep=1000
,我看不到分段错误的情况;但是对于
Ntimestep=10000
,当
for
循环中的
I
为9999(最后一次迭代)时,在以下说明中

force[i+1]=ay[i]*Mass;
vy[i+1]=(vy[i]+(ay[i]*dt));
y[i+1]=y[i]+vy[i]*dt+0.5*ay[i]*pow(dt,2); 

>在代码> > <代码> >代码>中,在10000位中,在<代码> y>代码>中,这是坏的(你是C++学习者,但我想你知道你可以读/写一个<代码> x x[n] < /COD>数组,从<代码> 0代码>到<代码> n-1 < /代码>,并且在>x[n] < /Cube中的访问是一个越界访问)。 一些建议,没有分词顺序

1) 注意缩进;您的代码很难阅读

2) 尽可能避免使用全局变量

3) 如果可以,请使用常量(
constexpr
,如果可能,请使用C++11/C++14);全局常量很好,您可以使用它们定义数组大小;例如,您可以定义全局常量

 const int  numSteps = 10000;
数组定义(我建议在
main()
中使用local)可以

+1
为阵列提供另一个位置并避免上述问题)并且循环的
可以

 for (int i=0;i<numSteps;i++)

for(int i=0;i您能提供有关错误的更多详细信息吗?它在我的系统上运行没有任何问题。顺便说一下,在输出控制台上打印太多的值和结果不是一个好主意。您可能希望输出
force[i]
vy[i]
y[i]
在调试器中单步执行时会发生什么?谢谢Anton Savin
 for (int i=0;i<numSteps;i++)