spring 运动模型代码中的分段错误
Segmentation fault in code to model spring motion
我正在编写一些代码来模拟 spring 的运动。给定系统的初始位置和速度,我正在尝试计算后面某个时刻的位置速度。
为此,我正在对 x(t+e) = x(t) + ev(t) 进行近似。短时间 'e' 秒后的位置等于当前位置加上 e 乘以速度。
我们可以对速度做同样的事情。 v(t+e) = v(t) + ea(t)。新速度等于旧速度加上加速度和时间的乘积。对于 a spring,a(t) = -x(t),因此我们有 v(t+e) = v(t) - ex(t)。
我的程序的思路是在一定的'steps'(步长等于e)打印位置和速度。
我根据上述方程定义了位置和速度的函数。
#include <iostream>
using namespace std;
//Defining the initial state of the system
float initialPosition = 1;
float initialVelocity = 0;
float step = 0.1;
float duration = 5;
int numberSteps = duration / step;
//Declaring the functions
float position(float t);
float velocity(float t);
//Outputting the position and velocity at time steps
int main()
{
cout << "Time \t" << "Position \t" << "Velocity \t" << endl;
for(int i = 0; i < numberSteps; ++i)
{
cout << step * i << "\t" << position(step * i) << "\t \t" << velocity(step * i) << endl;
}
return 0;
}
float position(float t)
{
if(t == 0)
{
return initialPosition;
}
else
{
return position(t - step) + step * velocity(t - step);
}
}
float velocity(float t)
{
if(t == 0)
{
return initialVelocity;
}
else
{
return velocity(t - step) - step * position(t - step);
}
}
我得到的输出(在 textmate 上)如下
Time Position Velocity
0 1 0
0.1 1 -0.1
0.2 0.99 -0.2
bootstrap.sh: line 10: 595 Segmentation fault: 11 "$A_OUT"
我试图弄清楚分段错误是什么,但许多消息来源似乎说它与指针有关(我不知道,也没有在这个程序中使用过)。我花了一个下午试图在我的 mac 上配置 gdb,以找出确切的错误,但无济于事。
编辑
我理解花车的问题。
正在替换
if(t == 0){...}
和
if(t < 0.1 * step){...}
似乎成功了。
永远不要使用 ==
与浮点数的比较,3 * 0.1
是 0.30000000000000004
所以看起来你的程序进入了无限循环。
而是尝试在离散域中对时间建模(换句话说,使用 int
)并像这样计算实时 float t = nQuants * quantDuration
,其中 quantDuration
可能等于 0.1f
或其他小值。
你有一个 velocity
的永久递归,当堆栈结束时程序崩溃。错误的比较在第 45 行:if (f == 0) where f is float.
问题是递归,递归不会停止(因为永远不会准确达到 0)
将比较 "if (t == 0)" 更改为 "if (t <= 0)",并停止依赖浮点数,它们非常不安全(由于 binary->float 对话)
我正在编写一些代码来模拟 spring 的运动。给定系统的初始位置和速度,我正在尝试计算后面某个时刻的位置速度。
为此,我正在对 x(t+e) = x(t) + ev(t) 进行近似。短时间 'e' 秒后的位置等于当前位置加上 e 乘以速度。
我们可以对速度做同样的事情。 v(t+e) = v(t) + ea(t)。新速度等于旧速度加上加速度和时间的乘积。对于 a spring,a(t) = -x(t),因此我们有 v(t+e) = v(t) - ex(t)。
我的程序的思路是在一定的'steps'(步长等于e)打印位置和速度。
我根据上述方程定义了位置和速度的函数。
#include <iostream>
using namespace std;
//Defining the initial state of the system
float initialPosition = 1;
float initialVelocity = 0;
float step = 0.1;
float duration = 5;
int numberSteps = duration / step;
//Declaring the functions
float position(float t);
float velocity(float t);
//Outputting the position and velocity at time steps
int main()
{
cout << "Time \t" << "Position \t" << "Velocity \t" << endl;
for(int i = 0; i < numberSteps; ++i)
{
cout << step * i << "\t" << position(step * i) << "\t \t" << velocity(step * i) << endl;
}
return 0;
}
float position(float t)
{
if(t == 0)
{
return initialPosition;
}
else
{
return position(t - step) + step * velocity(t - step);
}
}
float velocity(float t)
{
if(t == 0)
{
return initialVelocity;
}
else
{
return velocity(t - step) - step * position(t - step);
}
}
我得到的输出(在 textmate 上)如下
Time Position Velocity
0 1 0
0.1 1 -0.1
0.2 0.99 -0.2
bootstrap.sh: line 10: 595 Segmentation fault: 11 "$A_OUT"
我试图弄清楚分段错误是什么,但许多消息来源似乎说它与指针有关(我不知道,也没有在这个程序中使用过)。我花了一个下午试图在我的 mac 上配置 gdb,以找出确切的错误,但无济于事。
编辑 我理解花车的问题。
正在替换
if(t == 0){...}
和
if(t < 0.1 * step){...}
似乎成功了。
永远不要使用 ==
与浮点数的比较,3 * 0.1
是 0.30000000000000004
所以看起来你的程序进入了无限循环。
而是尝试在离散域中对时间建模(换句话说,使用 int
)并像这样计算实时 float t = nQuants * quantDuration
,其中 quantDuration
可能等于 0.1f
或其他小值。
你有一个 velocity
的永久递归,当堆栈结束时程序崩溃。错误的比较在第 45 行:if (f == 0) where f is float.
问题是递归,递归不会停止(因为永远不会准确达到 0)
将比较 "if (t == 0)" 更改为 "if (t <= 0)",并停止依赖浮点数,它们非常不安全(由于 binary->float 对话)