Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 查找火箭登月所需角度的代码中存在未知错误_Matlab_Loops_Physics - Fatal编程技术网

Matlab 查找火箭登月所需角度的代码中存在未知错误

Matlab 查找火箭登月所需角度的代码中存在未知错误,matlab,loops,physics,Matlab,Loops,Physics,我需要根据代码中的条件找到火箭的角度,使其能够在月球上着陆。月球被视为在给定坐标系下静止不动。我认为唯一的尝试方法就是计算不同的速度分量,然后输入每个角度的最小值到最大值,这就是我尝试做的 不知道为什么这不起作用-它只是永远运行,不输出任何东西 多谢各位 我有 v = 0.0066; for angle = 0:.5:180 init_vel = [v*cosd(angle), v*sind(angle)]; init_pos = [3.7,0]; t = 10; moon_pos = [0

我需要根据代码中的条件找到火箭的角度,使其能够在月球上着陆。月球被视为在给定坐标系下静止不动。我认为唯一的尝试方法就是计算不同的速度分量,然后输入每个角度的最小值到最大值,这就是我尝试做的

不知道为什么这不起作用-它只是永远运行,不输出任何东西

多谢各位

我有

v = 0.0066;

for angle = 0:.5:180

init_vel = [v*cosd(angle), v*sind(angle)];
init_pos = [3.7,0];
t = 10;
moon_pos = [0,222];
%simulate rocket
[tout, pos] = simulaterocketmovement(init_pos, init_vel, moon_pos, t);

    if(length(tout)<99999)
        break;
    end
 end

 disp(angle);

 plot(pos(:,1),pos(:,2));
v=0.0066;
对于角度=0:5:180
初始水平=[v*cosd(角度),v*sind(角度)];
初始位置=[3.7,0];
t=10;
月亮位置=[0222];
%模拟火箭
[tout,pos]=模拟机锁定移动(初始位置,初始水平,月亮位置,t);
如果(长度(tout)xmax)| |(x(end)ymax))
%我们为x和y分配临时的新值。
x(end+1)=x(end)+t*vx;
y(end+1)=y(end)+t*vy;
%然后我们找到新旧x和y的加速度值
aold=acc(x(end-1),y(end-1));
重新=附件(x(结束),y(结束));
%用这个来寻找新的速度
vxnew=vx+t*(aold(1)+新(1))/2;
vynew=vy+t*(aold(2)+新(2))/2;
%x和y的最终更精确的值
x(end)=x(end-1)+t*(vxnew+vx)/2;
y(end)=y(end-1)+t*(vynew+vy)/2;
%并将其更新为新的速度
vx=vxnew;
vy=vynew;
结束
%然后我们为整个旅程的时间步长构造一个向量。
tout=0:t:((长度(x)-1)*t);
%然后创建一个包含x和y位置的位置向量。
pos=零(长度(x),2);
位置(:,1)=x;
pos(:,2)=y;
结束

SimulatorOcketMovement
有一个循环,一直运行到接近月球,但是如果这个角度不能使它接近月球怎么办?@CrisLuengo那么x的长度不会一直增加,因此向量tout的长度会增加,这是由第一组代码中的if循环捕获的?如果函数永远不存在,如果从未执行,则该
。您可能希望将该条件放在内部循环中。我建议您使用调试器逐步检查代码,以了解控制流程。
simulatorocketmovement
有一个循环一直运行到它接近月球,但是如果这个角度不能使它接近月球怎么办?@CrisLuengo那么x的长度不会一直增加,因此向量tout的长度增加了,这是由第一组代码中的if循环捕获的?如果函数从未退出,那么
if
将永远不会执行。您可能希望将该条件放在内部循环中。我建议您使用调试器逐步完成代码,以了解控制流。
function [ tout , pos ] = simulaterocketmovement ( init_pos , init_vel , moon_pos , t)

%Defining initial variables for later use
G = 9.63*10^-7;
Me = 83.3;

%Defining the two vectors we will use
x = init_pos(1);
y = init_pos(2);
vx = init_vel(1);
vy = init_vel(2);


%Need to create a seperate function that integrates to find the
%acceleration using Euler's second order method.
function a = acc(x,y)
    ax = -(G*Me*x)/((x^2 + y^2)^1.5) - (G*(x-moon_pos(1)))/(((moon_pos(1)-x)^2 + (moon_pos(2)-y)^2)^1.5);
    ay = -(G*Me*y)/((x^2 + y^2)^1.5) - (G*(y-moon_pos(2)))/(((moon_pos(1)-x)^2 + (moon_pos(2)-y)^2)^1.5);
    %After finding the vector components, we put them in an acceleration vector.
    a = [ax,ay];
end  


%Now we find the values which result in the rocket landing on the moon. The
%range of values lie between xmin and xmax, and ymin and ymax. The +/-
%represents that the rocket can land anywhere on the moon's surface.
xmax = moon_pos(1) + 1;
xmin = moon_pos(1) - 1;
ymax = moon_pos(2) + 1;
ymin = moon_pos(2) - 1;


%For each time taken, to find the x and y values we need to use a while
%loop which works until the rocket is in the range of values for it to
%land on the moon.
while((x(end) > xmax) || (x(end) < xmin) || (y(end) < ymin) || (y(end) > ymax) )

%We assign temporary new values of x and y.
x(end+1) = x(end) + t*vx;
y(end+1) = y(end) + t*vy;

%Then we find the values of acceleration for both the old and new x and y 
aold = acc(x(end-1), y(end-1));
anew = acc(x(end), y(end));

%Using this to find the new velocities
vxnew = vx + t*(aold(1)+anew(1))/2;
vynew = vy + t*(aold(2)+anew(2))/2;

%Final, more accurate values for x and y
x(end) = x(end-1) + t*(vxnew + vx)/2;
y(end) = y(end-1) + t*(vynew + vy)/2;

%And updating these as the new velocities
vx = vxnew;
vy = vynew;

end    

%Then we construct a vector for the time steps, for the entire journey.
tout = 0:t:((length(x)-1)*t);

 %And then create a position vector which includes the x and y positions.
pos = zeros(length(x),2);
pos(:,1) = x;
pos(:,2) = y;

end