- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
有人要求我解这个微分方程:
(x,y,vx,vy)'=(vx,vy,vy,-vx)
它应该返回周期为 2*pi
的圆周运动。我实现了功能:
class FunzioneBase
{
public:
virtual VettoreLineare Eval(double t, const VettoreLineare& v) const = 0;
};
class Circonferenza: public FunzioneBase
{
private:
double _alpha;
public:
Circonferenza(double alpha) { _alpha = alpha; };
void SetAlpha(double alpha) { _alpha = alpha; };
virtual VettoreLineare Eval(double t, const VettoreLineare& v) const;
};
VettoreLineare Circonferenza::Eval(double t, const VettoreLineare& v) const
{
VettoreLineare y(4);
if (v.GetN() != 4)
{
std::cout << "errore" << std::endl;
return 0;
};
y.SetComponent(0, v.GetComponent(2));
y.SetComponent(1, v.GetComponent(3));
y.SetComponent(2, pow(pow(v.GetComponent(0), 2.) + pow(v.GetComponent(1), 2.), _alpha) * v.GetComponent(3));
y.SetComponent(3, - pow(pow(v.GetComponent(0), 2.) + pow(v.GetComponent(1), 2.), _alpha)) * v.GetComponent(2));
return y;
};
其中 _alpha
等于 0
。现在,这适用于欧拉方法:如果我对 2 * pi * 10
积分这个 ODE,给定初始条件 (1, 0, 0, -1)
,以 0.003
精度,我得到该位置与 (1, 0)
在 1 ± 0.1
范围内相当,因为我们应该期待。但是,如果我将相同的 ODE 与 Runge Kutta 的方法(具有 0.003
精度,2 * pi * 10
秒)集成,实现如下:
class EqDifferenzialeBase
{
public:
virtual VettoreLineare Passo (double t, VettoreLineare& x, double h, FunzioneBase* f) const = 0;
};
class Runge_Kutta: public EqDifferenzialeBase
{
public:
virtual VettoreLineare Passo(double t, VettoreLineare& v, double h, FunzioneBase* f) const;
};
VettoreLineare Runge_Kutta::Passo(double t, VettoreLineare& v, double h, FunzioneBase* _f) const
{
VettoreLineare k1 = _f->Eval(t, v);
VettoreLineare k2 = _f->Eval(t + h / 2., v + k1 *(h / 2.));
VettoreLineare k3 = _f->Eval(t + h / 2., v + k2 * (h / 2.));
VettoreLineare k4 = _f->Eval(t + h, v + k3 * h);
VettoreLineare y = v + (k1 + k2 * 2. + k3 * 2. + k4) * (h / 6.);
return y;
}
程序返回一个 x
位置,大约等于 0.39
,对于四阶 Runge Kutta 方法,理论上精度应该在 1E- 附近6
。我检查了一下,发现使用 Runge_Kutta 的周期似乎几乎翻了四倍(因为在 2 * pi
失效中,x
从 1
到0.48
),但我不明白为什么。这是我主要的内容:
VettoreLineare DatiIniziali (4);
Circonferenza* myCirc = new Circonferenza(0);
DatiIniziali.SetComponent(0, 1.);
DatiIniziali.SetComponent(1, 0.);
DatiIniziali.SetComponent(2, 0.);
DatiIniziali.SetComponent(3, -1.);
double passo = 0.003;
Runge_Kutta myKutta;
for(int i = 0; i <= 2. * M_PI / passo; i++)
{
DatiIniziali = myKutta.Passo(0, DatiIniziali, passo, myCirc);
cout << DatiIniziali.GetComponent(0) << endl;
};
cout << 1 - DatiIniziali.GetComponent(0) << endl;
提前谢谢你。
最佳答案
更新:发现一个错误:始终使用 -Wall
选项进行编译,以捕获编译器的所有警告和自动代码更正。那么你会发现
fff: In member function ‘virtual VettoreLineare Circonferenza::Eval(double, const VettoreLineare&) const’:
fff:xxx:114: error: invalid operands of types ‘void’ and ‘double’ to binary ‘operator*’
y.SetComponent(3, - pow(pow(v.GetComponent(0), 2.) + pow(v.GetComponent(1), 2.), _alpha)) * v.GetComponent(2));
^
您在 _alpha
之后过早关闭,因此 SetComponent
的 void
成为一个因素。
更新 II: 识别出的第二个错误:在 another post of yours 中给出了线性 vector 类的代码。在那里,与加法 (operator+
) 相比,标量 vector 积 (operator*(double)
) 正在修改调用实例。因此,在计算 k2
时,k1
的分量将乘以 h/2
。但是随后这个修改后的 k1
(以及修改后的 k2
和 k3
)被用于组装结果 y
导致一些几乎完全无用的状态更新。
原始快速原型(prototype)制作:我可以告诉你,在 python 中精简的基本实现可以完美运行
import numpy as np
def odeint(f,t0,y0,tf,h):
'''Classical RK4 with fixed step size, modify h to fit
the full interval'''
N = np.ceil( (tf-t0)/h )
h = (tf-t0)/N
t = t0
y = np.array(y0)
for k in range(int(N)):
k1 = h*np.array(f(t ,y ))
k2 = h*np.array(f(t+0.5*h,y+0.5*k1))
k3 = h*np.array(f(t+0.5*h,y+0.5*k2))
k4 = h*np.array(f(t+ h,y+ k3))
y = y + (k1+2*(k2+k3)+k4)/6
t = t + h
return t, y
def odefunc(t,y):
x,y,vx,vy = y
return [ vx,vx,vy,-vx ]
pi = 4*np.arctan(1);
print odeint(odefunc, 0, [1,0,0,-1], 2*pi, 0.003)
结束于
(t, [ x,y,vx,vy]) = (6.2831853071794184,
[ 1.00000000e+00, -6.76088739e-15, 4.23436503e-12,
-1.00000000e+00])
如预期。您将需要一个调试器或中间输出来找出计算出错的地方。
关于c++ - Runge Kutta 圆周运动法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34965829/
我一直在尝试将 RK4 集成到我正在做的模拟中。下面的函数是我基于第 12 页 this 上的方程式使用 RK4 对 3 维力场进行积分的最佳尝试。网站。 在我的代码中,粒子类主要存储速度和位置列表,
我试图获得一个简单追逐问题的数值解 (动靶+定速模块火箭) 每次迭代我的速度模块都会减少一点,将误差加起来;在几百次迭代之后,错误爆发,速度急剧下降。 但是,使用 Euler 方法(大块下方的代码)时
有人要求我解这个微分方程: (x,y,vx,vy)'=(vx,vy,vy,-vx) 它应该返回周期为 2*pi 的圆周运动。我实现了功能: class FunzioneBase { public:
我一直在研究四阶龙格-库塔求解器,但遇到了一些困难。我已经根据文章 on gafferongames 编写了求解器,但是当我运行一个包含的小例子时,我得到的错误比我用简单的欧拉积分得到的错误要糟糕得多
几乎每个游戏都倾向于使用一些游戏循环。 Gafferongames 有一篇关于如何制作精心设计的游戏循环的好文章:http://gafferongames.com/game-physics/fix-y
我有以下微分方程组: 根据他们告诉我的论文,我可以使用 RK 四阶数值求解它。 如您所见,最后两个方程是耦合的,我构造了一个关联 xn 和 yn 的矩阵(概率),其中 n = 1..(例如,N- 对数
我正在使用 Runge-Kutta 四阶方法数值求解具有四次势的弯曲时空背景标量场的常见运动方程: $\phi^{''}=-3\left(1+\frac{H^{'}}{3H}\right)\phi^{
我用 C++ 编写了一个程序来求解微分方程。问题是,它似乎不适用于 ROOT。它编译得很好,但是当我执行时,这就是我得到的: *** Break *** segmentation violation
我有一个耦合方程组:流体静力学平衡方程、质量连续性方程和理想气体的状态方程。这些是,在数学语法中, \frac{dP}{dr}=-\rho*g, 其中 \rho 是密度,g 是重力加速度。 \frac
有人能告诉我,为什么它总是返回相同的“y”值吗?我在 Internet 上搜索了很多,但我仍然不知道为什么它不起作用。 using System; using System.Collections.G
下面是我用于求解一阶 ODE 的四阶 Runge-Kutta 算法。我正在根据找到的维基百科示例检查它 here解决: \frac{dx}{dt} = tan(x) + 1 不幸的是,它有点出局了。我
我正在尝试实现 Runge-Kutta Method for Systems of DEs在 MATLAB 中。我没有收到 correct answers ,我不确定代码或我用来运行它的命令是否有问题
尝试实现自适应步长 Runge-Kutta Cash-Karp 但失败并出现此错误: home/anaconda/lib/python3.6/site-packages/ipykernel_launc
我正在尝试编写一个函数,该函数将使用 4 阶隐式 Runge-Kutta 方法 (IRK) 求解 ODES 系统,但我无法正确定义循环。这里我们定义 IRK 任何建议将不胜感激! function [
Gaffer on Games 有一个 great article关于使用RK4 integration为了更好的游戏物理。实现很简单,但其背后的数学让我感到困惑。我在概念层面上了解导数和积分,但已经
这个公式主要是这个线程的结果:Differential Equations in Java . 基本上,我尝试遵循 Jason S. 的建议,并通过 Runge-Kutta 方法 (RK4) 实现微分
我是Python的新手,我对编程语言的了解还处于起步阶段,所以我复制了所示的Runge-Kutta Python脚本here并根据我的目的对其进行了修改。这是我当前的脚本: import numpy
我一直在将 4 个链接微分方程的欧拉方法实现转换为四阶龙格库塔实现。我相当确信我的一般方法是正确的,并且我已经了解如何应用 RK4,但我可能已经有 6 年没有做过任何半严肃的数学了,所以我可能会错过某
我尝试求解简单的数值方程 - 没有源的线性波动方程:utt = v2 uxx 其中 v - 波速。 我使用初始条件: u(x, 0) = sin(x) ux(x, 0) = -v * sin(x) 对
我的问题与 Runge-Kutta 4 (RK4) 方法和轨道卫星状态向量所需的正确迭代步骤有关。下面的代码(在 Python 中)根据此链接 (http://www.navipedia.net/in
我是一名优秀的程序员,十分优秀!