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!