Ich bin Anfänger in C++. Also werde ich versuchen, die Gesamtkraft, die Geschwindigkeit, den Ort und die Gesamtzeit in jedem Zeitschritt (dt = 0,00001) herauszufinden und myfile1, myfile2, myfile3, myfile4 aus der einfachen Gleichung zu speichern. Ich stehe vor dem Problem, den genauen Wert herauszufinden. Wenn ich das Programm starte, wird der Segmentierungsfehler gefunden. Wie kann ich das Problem lösen? Ich habe das folgende Programm beigefügt: Jeder kann mir helfen?Wie Gleichung zu lösen und richtig zu speichern?
//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;
}
Könnten Sie weitere Informationen über den Fehler zur Verfügung stellen? Es funktioniert ohne Probleme auf meinem System. Es ist übrigens keine gute Idee, zu viele Werte und Ergebnisse auf die Ausgabekonsole zu drucken. –
Sie möchten wahrscheinlich 'force [i]', 'vy [i]' und 'y [i]' –
ausgeben Was passiert, wenn Sie im Debugger durchgehen? –