私は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;
}
のため申し訳ありません:
std::vector<double>
PSより良い解決策になることができますか?私のシステムで問題なく動作します。ところで、あまりにも多くの値と結果を出力コンソールに出力するのは良い考えではありません。 –
'force [i]'、 'vy [i]'と 'y [i]'を出力したいと思うかもしれません。 –
デバッガをステップ実行するとどうなりますか? –