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!