【问题标题】:What does this syntax [0:1:5] mean (do) in the context of the given code?在给定代码的上下文中,这个语法 [0:1:5] 是什么意思(做)?
【发布时间】:2016-03-20 21:04:07
【问题描述】:

我不明白[0:1:5] 在下面的代码中是如何使用的:

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

【问题讨论】:

    标签: matlab syntax octave


    【解决方案1】:

    每个参数都用于查找特定的XY 值。 O 从 0 变为 pi,步长为 pi/8,而 Votg 保持不变。

    t 变量只是一个从 0 到 5 以 1 为步长的数组,因此总共定义了 6 个时间点。通过这些时间点和特定值O,但Votg 的值在整个努力过程中保持不变,定义了6 个XY 点,并且因此绘制在图表上。为O 的每个值生成一个图表,从而生成一组6 个不同的XY 点。每个值为O 的图都绘制在同一个图上。

    我们可以把上面的代码改写成伪代码,这样更容易理解:

     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
    

    【讨论】:

    • 非常感谢您的详细解释!这对我来说更有意义。
    猜你喜欢
    • 1970-01-01
    • 2015-12-04
    • 1970-01-01
    • 2016-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-02
    相关资源
    最近更新 更多