What does this syntax [0:1:5] mean (do) in the context of the given code?

孤街醉人 提交于 2019-12-13 09:14:47

问题


I don't understand how [0:1:5] is being used in the code below:

function [x , y] = plotTrajectory(Vo,O,t,g)
% calculating x and y values
x = Vo * cos(O) * t ;
y = Vo*(sin(O)*t)-(0.5*g*(t.^2));
plot (x,y);
hold on
end

for i = (0: (pi/8): pi);
[x,y] = plotTrajectory(10,i,[0:1:5],9.8);
end

回答1:


Each of the parameters are being used to find particular X and Y values. O changes from 0 to pi in steps of pi/8 while Vo, t and g remain unchanged.

The t variable is simply an array from 0 to 5 in steps of 1 and so there are 6 time points defined all together. With these time points and with a particular value of O, but with the values of Vo, t and g being held constant throughout this entire endeavour, 6 X and Y points are defined and are thus plotted on a graph. A graph is generated for each value of O and thus a set of 6 different X and Y points are generated. Each graph with each value of O are all plotted on the same graph.

We can rewrite the above code in pseudo-code to make it easier to understand as follows:

 for i = 0, pi/8, 2*pi/8, ..., pi
     define Vo = 10
     define O = i
     define t = [0, 1, 2, 3, 4, 5]
     define g = 9.8
     run function plotTrajectory(Vo, O, t, g)
 end

 function plotTrajectory(Vo, O, t, g)
     calculate x = Vo * cos(O) * t, for t = 0, 1, 2, 3, 4, 5
     calculate y = Vo * (sin(O) * t) - (0.5 * g * t^2), for t = 0, 1, 2, 3, 4, 5
     plot x and y for t = 0, 1, 2, 3, 4, 5 on the same graph
 end


来源:https://stackoverflow.com/questions/36119586/what-does-this-syntax-015-mean-do-in-the-context-of-the-given-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!