gpt4 book ai didi

c++ - vector 导致的段错误

转载 作者:搜寻专家 更新时间:2023-10-31 01:47:10 25 4
gpt4 key购买 nike

我有一个程序应该用四阶龙格库塔方法和欧拉克罗默方法计算钟摆的轨迹,不幸的是......我有一个段错误。

我不知道如何解决这个问题,正如您所看到的,我有几个 std::cout 来尝试调试,但它们甚至没有输出。这让我相信该函数永远不会执行 main();

我目前的直觉是它在函数声明期间出错,但如果我错了我想知道。如果可能的话,您能否描述一下您用于调试的技术?

#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <stdlib.h>
#include <fstream>

#define N 1

// Pendulum Variables
double d_pendLength = 1.0, d_pendMass = 1.0; // Pendulum Properties
double d_viscParam = 1.0, d_gravity = 1.0; //9.80665; // Enviromental Factors
double d_dAmp = 0.0, d_dFreq = 0.0; // Driving Force

double d_initTheta = 0.0, d_initAVel = 0.0; //Initial Conditions

void v_rungekutta(double d_time, std::vector<double> d_pendulum, double d_step);
void v_eulercromer(double d_time, std::vector<double> d_pendulum, double d_step);
double d_derivative(double d_time, std::vector<double> d_pendulum, int i_de);

int main(void)
{
// Numerical Variables
double d_step = 0.01, d_mTime = -1, d_mPeriod = 300;

// Global Variables
double d_time = 0.0, d_period = 0.0, d_limit;

// Function Vectors
std::vector<double> d_pendulum (2);

// Settings
std::string plot = "theta", algo = "rk4";


std::cout << "START!";
std::ofstream file_output;
std::cout << "HAIDATA!";
file_output.open("pendulum.data");

std::cout << "HAI!";
d_pendulum.at(0)= d_initTheta; //Initial Theta
d_pendulum.at(1)= d_initAVel; //Initial Omega
std::cout <<"BAI!";

if (d_mPeriod > 0)
{
d_limit = d_mPeriod;
}
else if (d_mTime > 0)
{
d_limit = d_mTime;
}
else
{
std::cout << "No Limit Specified";
exit(1);
}

for (int i_currentStep=1; i_currentStep*d_step<=d_limit;i_currentStep++)
{
d_time = i_currentStep*d_step;

if (algo == "rk4")
{
v_rungekutta(d_time, d_pendulum, d_step);
}
else if (algo == "ec")
{
v_eulercromer(d_time, d_pendulum, d_step);
}
file_output << d_time << d_pendulum.at(0) << d_pendulum.at(1) << std::endl;
}


file_output.close();
d_pendulum.clear();

return 0;
}


void v_rungekutta(double d_time, std::vector<double> d_pendulum, double d_step)
{
double h=d_step/2.0;
std::vector<double> t1, t2, t3, k1, k2, k3, k4;
int i;

for (i=N;i>0;i--) t1[i]=d_pendulum.at(i)+0.5*(k1[i]=d_step*d_derivative(d_time, d_pendulum, i));
for (i=N;i>0;i--) t2[i]=d_pendulum.at(i)+0.5*(k2[i]=d_step*d_derivative(d_time+h, t1, i));
for (i=N;i>0;i--) t3[i]=d_pendulum.at(i)+ (k3[i]=d_step*d_derivative(d_time+h, t2, i));
for (i=N;i>0;i--) k4[i]=d_step*d_derivative(d_time+d_step, t3, i);

for (i=N;i>0;i--) d_pendulum.at(i) += (k1[i]+2*k2[i]+2*k3[i]+k4[i])/6.0;
}

void v_eulercromer(double d_time, std::vector<double> d_pendulum, double d_step)
{
int i;
for (i=N;i>0;i--) d_pendulum.at(i) += d_derivative(d_time, d_pendulum, i)*d_step;
}

double d_derivative(double d_time, std::vector<double> d_pendulum, int i_de)
{
double dtheta = d_pendulum.at(1);
if (i_de==0) return dtheta;
double domega = d_pendulum.at(1)+((-d_gravity/d_pendLength)*sin(d_pendulum.at(0)))+(-d_viscParam*(d_pendulum.at(1)))+(d_dAmp*sin(d_dFreq*d_time));
if (i_de==1) return domega;
if (i_de < 0) return 0; // 0 is a possible value, exit with exit func.
}

解决方案

我的问题是我没有正确访问 rungekutta 中的 vector ,也没有给它们一个起始大小。

之前

    void v_rungekutta(double d_time, std::vector<double> d_pendulum, double d_step)
{
double h=d_step/2.0;
std::vector<double> t1, t2, t3, k1, k2, k3, k4;
int i;

for (i=N;i>0;i--) t1[i]=d_pendulum.at(i)+0.5*(k1[i]=d_step*d_derivative(d_time, d_pendulum, i));
for (i=N;i>0;i--) t2[i]=d_pendulum.at(i)+0.5*(k2[i]=d_step*d_derivative(d_time+h, t1, i));
for (i=N;i>0;i--) t3[i]=d_pendulum.at(i)+ (k3[i]=d_step*d_derivative(d_time+h, t2, i));
for (i=N;i>0;i--) k4[i]=d_step*d_derivative(d_time+d_step, t3, i);

for (i=N;i>0;i--) d_pendulum.at(i) += (k1[i]+2*k2[i]+2*k3[i]+k4[i])/6.0;
}

之后:

    void v_rungekutta(double d_time, std::vector<double> d_pendulum, double d_step)
{
double h=d_step/2.0;
std::vector<double> t1 (2), t2 (2), t3 (2), k1 (2), k2 (2), k3 (2), k4 (2);
int i;

for (i=N;i>0;i--) t1.at(i)=d_pendulum.at(i)+0.5*(k1.at(i)=d_step*d_derivative(d_time, d_pendulum, i));
for (i=N;i>0;i--) t2.at(i)=d_pendulum.at(i)+0.5*(k2.at(i)=d_step*d_derivative(d_time+h, t1, i));
for (i=N;i>0;i--) t3.at(i)=d_pendulum.at(i)+ (k3.at(i)=d_step*d_derivative(d_time+h, t2, i));
for (i=N;i>0;i--) k4.at(i)=d_step*d_derivative(d_time+d_step, t3, i);

for (i=N;i>0;i--) d_pendulum.at(i) += (k1[i]+2*k2[i]+2*k3[i]+k4[i])/6.0;
}

最佳答案

我第一眼看到的是您正在尝试访问空 vector 中的元素。尝试用 at() 方法替换 [] 运算符。不同之处在于前者不检查可能会产生段错误的边界,而后者会检查,如果索引越界则抛出异常。

关于c++ - vector 导致的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19615371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com