Piecewise error input argument
显示 更早的评论
Howdy all. Another homework question.
Writing a function dependent upon a local function (two, in fact), which is a piecewise.
function [T, H, V] = rocket(Tf, dt)
global m
m = 10e+3;
n = 1;
V(n) = 0;
H(n) = 0;
T(n) = 0;
while T(n) < Tf
g(n) = gravity(H);
Th(n) = thrust(T);
V(n+1) = V(n) + (-(g(n)) + (Th(n)/m))*dt;
H(n+1) = H(n) + V(n+1)*dt;
T(n+1) = T(n) + dt;
n = n+1;
end
end
function [Th] = thrust(t)
%Input time and output thrust magnitude.
%Th(t) = piecewise((0 >= t) & (t < 2), 670, (2 <= t)&&(t < 4), 1366.5, t >= 4, 0);
Th(t) = piecewise(0 <= t < 2, 670, 2 <= t < 4, 1366.5, t >= 4, 0);
end
function [g] = gravity(h)
%Input height and output gravitational acceleration magnitude.
g(h) = piecewise((0 <= h)&(h < 10e+3), (9.81*(1-(h/10e+3)^3)), h >= 10e+3, 0);
end
When I run the script, it tells me the local function (g) has too many input arguements. I was under the impression g(n) = gravity(H) will pass the current value of H to the local function gravity(h) for it to compute g. Am I reading this wrong or do I have something mixed up in my piecewise?
采纳的回答
更多回答(1 个)
Matthew Henry
2019-2-25
0 个投票
1 个评论
Walter Roberson
2019-2-25
piecewise cannot be defined with first parameter being a numeric scalar. It could be a symbolic scalar.
The problem is not that H is being sent as 0: the problem is that you are trying to define
g(h) = ....
when you should be just defining
g = ...
And you need to be sure to pass only H(n) not all of H. But this is not what is going to cause an error about too many inputs.
类别
在 帮助中心 和 File Exchange 中查找有关 Assumptions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!