龙格库塔四阶数值逼近轨道卫星

25次观看(过去30天)
史蒂文·汉
史蒂文·汉 2020年11月12日
回答: Meysam Mahooti 2021年7月28日
我正在为一颗绕地球运行的卫星写剧本。目前,当我试图运行代码时,代码显示为NaN。我不确定在哪里犯了这样的错误,我的代码除以我的0。运动方程是正确的。提前谢谢!
%% rk4 matlab脚本
%的参数
Me = 5.972e24;%物体质量a
Mq = 1000;质点q质量的%
G = 6.67408e-11;%引力常数
Tf = 30;%最后时间
H = 0.01;步长%
R = 100000;卫星到地球的距离
%状态空间形式的运动方程
F1 = @(t, r, rdot, theta, thetad) rdot;
f2 = @(t, r, rdot, theta, thetad) -G*Me/r^2 + r*thetad^2;
F3 = @(t, r, rdot, theta, thetad);
F4 = @(t, r, rdot, theta, thetad) 2*rdot*thetad/r;
初始条件(测试随机ic)
R (1) = 1000;卫星的初始位置
Rdot (1) = 50;卫星的初始转译率
(1) = 0;卫星的初始角位置
Thetad (1) = 0;卫星初始角速率
初始时间%
T (1) = 0;
% RK4循环
I = 1:tf/h
T (i+1) = T (i) + h;
k1r = f1 (t (i)、r (i), rdot(我),θ(i), thetad(我));
k1rdot = f2 (t (i)、r (i), rdot(我),θ(i), thetad(我));
k1theta = f3 (t (i)、r (i), rdot(我),θ(i), thetad(我));
k1thetad = f4 (t (i)、r (i), rdot(我),θ(i), thetad(我));
k2r = f1 (t(我)+ h / 2, r (i) + k1r * h / 2, rdot (i) + k1rdot * h / 2θ(i) + k1theta * h / 2, thetad (i) + k1thetad * h / 2);
k2rdot = f2 (t(我)+ h / 2, r (i) + k1r * h / 2, rdot (i) + k1rdot * h / 2θ(i) + k1theta * h / 2, thetad (i) + k1thetad * h / 2);
k2theta = f3 (t(我)+ h / 2, r (i) + k1r * h / 2, rdot (i) + k1rdot * h / 2θ(i) + k1theta * h / 2, thetad (i) + k1thetad * h / 2);
k2thetad = f4 (t(我)+ h / 2, r (i) + k1r * h / 2, rdot (i) + k1rdot * h / 2θ(i) + k1theta * h / 2, thetad (i) + k1thetad * h / 2);
k3r = f1 (t(我)+ h / 2, r (i) + k2r * h / 2, rdot (i) + k2rdot * h / 2θ(i) + k2theta * h / 2, thetad (i) + k2thetad * h / 2);
k3rdot = f2 (t(我)+ h / 2, r (i) + k2r * h / 2, rdot (i) + k2rdot * h / 2θ(i) + k2theta * h / 2, thetad (i) + k2thetad * h / 2);
k3theta = f3 (t(我)+ h / 2, r (i) + k2r * h / 2, rdot (i) + k2rdot * h / 2θ(i) + k2theta * h / 2, thetad (i) + k2thetad * h / 2);
k3thetad = f4 (t(我)+ h / 2, r (i) + k2r * h / 2, rdot (i) + k2rdot * h / 2θ(i) + k2theta * h / 2, thetad (i) + k2thetad * h / 2);
k4r = f1 (t(我)+ h r (i) + k3r * h, rdot (i) + k3rdot * h,θ(i) + k3theta * h, thetad(我)+ k3r * h);
k4rdot = f2 (t(我)+ h r (i) + k3r * h, rdot (i) + k3rdot * h,θ(i) + k3theta * h, thetad(我)+ k3r * h);
k4theta = f3 (t(我)+ h r (i) + k3r * h, rdot (i) + k3rdot * h,θ(i) + k3theta * h, thetad(我)+ k3r * h);
k4thetad = f4 (t(我)+ h r (i) + k3r * h, rdot (i) + k3rdot * h,θ(i) + k3theta * h, thetad(我)+ k3r * h);
%更新位置和速度
R (i+1,1) = R (i,1) + (h/6)*(k1r+2*k2r+2*k3r+k4r);
rdot (i + 1) = rdot(我,1)+ (h / 6) * (k1rdot + 2 * k2rdot + 2 * k3rdot + k4rdot);
(i+1,1) = (i,1) + (h/6)*(k1 +2*k2 +2*k3 +k4);
Thetad (i+1,1) = Thetad (i,1) + (h/6)*(k1thetad+2*k2thetad+2*k3thetad+k4thetad);
结束
%的情节
图(1)
情节(t, r,“b”);
标题(“位置”);
包含(“时间(s)”);
ylabel (“位置(m)”);

答案(2)

詹姆斯Tursa
詹姆斯Tursa 2020年11月12日
编辑:詹姆斯Tursa 2020年11月14日
这段代码
k4r = f1 (t(我)+ h r (i) + k3r * h, rdot (i) + k3rdot * h,θ(i) + k3theta * h, thetad(我)+ k3r * h);
k4rdot = f2 (t(我)+ h r (i) + k3r * h, rdot (i) + k3rdot * h,θ(i) + k3theta * h, thetad(我)+ k3r * h);
k4theta = f3 (t(我)+ h r (i) + k3r * h, rdot (i) + k3rdot * h,θ(i) + k3theta * h, thetad(我)+ k3r * h);
k4thetad = f4 (t(我)+ h r (i) + k3r * h, rdot (i) + k3rdot * h,θ(i) + k3theta * h, thetad(我)+ k3r * h);
应该是这个
k4r = f1 (t(我)+ h r (i) + k3r * h, rdot (i) + k3rdot * h,θ(i) + k3theta * h, thetad(我)+ k3thetad * h);
K4rdot = f2(t(i)+h, r(i)+k3r*h, rdot(i)+k3rdot*h, theta(i)+k3theta*h, thetad(i)+k3thetad*h);
k4 = f3(t(i)+h, r(i)+k3r*h, rdot(i)+k3rdot*h, theta(i)+k3theta*h, thetad(i)+k3thetad*h);
K4thetad = f4(t(i)+h, r(i)+k3r*h, rdot(i)+k3rdot*h, theta(i)+k3theta*h, thetad(i)+k3thetad*h);
话虽如此,你的代码是 道路 它太复杂了。你基本上需要为四个标量方程写四组RK4代码。它会是 最好是定义一个4元素的状态向量,然后写 一个 RK4方程的集合来处理传播。这意味着你目前所写的代码的1/4,更少的机会出现像你所做的复制粘贴错误。
还有这些初始条件
R (1) = 1000;卫星的初始位置
Rdot (1) = 50;卫星的初始转译率
(1) = 0;卫星的初始角位置
Thetad (1) = 0;卫星初始角速率
首先把卫星放在地球表面内,结果是直线运动。我觉得这不是你想要的。
此外,
Q1:为什么f2 - g *Me/r^2 - r*thetad^2不是
编辑:
错误符号修正建议如上。我应该建议的是保持f2不变,并进行更改
为什么不是f4 -2*rdot*thetad/r
另外,你可以看到这个相关的线程:
3评论
詹姆斯Tursa
詹姆斯Tursa 2020年11月13日
如果我做以下事情:
  • 对上面提到的k4项进行更正
  • 修改f4函数句柄的符号
  • 使用合理的初始条件
然后得到合理的结果。例如,
k4r = f1 (t(我)+ h r (i) + k3r * h, rdot (i) + k3rdot * h,θ(i) + k3theta * h, thetad(我)+ k3thetad * h);
K4rdot = f2(t(i)+h, r(i)+k3r*h, rdot(i)+k3rdot*h, theta(i)+k3theta*h, thetad(i)+k3thetad*h);
k4 = f3(t(i)+h, r(i)+k3r*h, rdot(i)+k3rdot*h, theta(i)+k3theta*h, thetad(i)+k3thetad*h);
K4thetad = f4(t(i)+h, r(i)+k3r*h, rdot(i)+k3rdot*h, theta(i)+k3theta*h, thetad(i)+k3thetad*h);
而且
F4 = @(t, r, rdot, theta, thetad) -2*rdot*thetad/r;
而且
H = 1;
Tf = 2*86400;% 2天
而且
R (1) = 42164000;卫星的初始位置
Rdot (1) = 0;%圆轨道
(1) = 0;%圆轨道
thetad(1) =根号(G*Me/r(1)^3);%圆轨道
而且
Th = t/3600;
%的情节
数字
情节(th, r,“b”);
标题(“R”);
包含(的时间(小时));
ylabel (R (m)的);
网格
数字
情节(th、rdot“b”);
标题(“Rdot”);
包含(的时间(小时));
ylabel (“Rdot(米/秒));
网格
数字
情节(th,θ*(180 /π),“b”);
标题(“θ”);
包含(的时间(小时));
ylabel (“θ(度)”);
网格
数字
情节(th, thetad *(180 /π)* 86400,“b”);
标题(“Thetadot”);
包含(的时间(小时));
ylabel (“Thetadot(度/天));
网格
X = r.*cos();
Y = r.*sin();
数字
情节(x, y,“b”);
标题(“轨道”);
包含(“X (m)”);
ylabel (“Y (m)”);
广场
网格
得出以下结论,对于圆形轨道来说似乎都是合理的:

登录发表评论。


Meysam Mahooti
Meysam Mahooti 2021年7月28日
//www.tianjin-qmedu.com/matlabcentral/fileexchange/55430-runge-kutta-4th-order?s_tid=srchtitle

社区寻宝

在MATLAB中央找到宝藏,并发现社区可以如何帮助你!

开始狩猎!