Piecewise error input argument

14 vues (au cours des 30 derniers jours)
Matthew Henry
Matthew Henry le 25 Fév 2019
Commenté : Walter Roberson le 25 Fév 2019
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?

Réponse acceptée

Walter Roberson
Walter Roberson le 25 Fév 2019
Modifié(e) : Walter Roberson le 25 Fév 2019
You do not have a function g. You have a vector g.
You initialize n = 1, H(n) = 0, so in your first iteration gravity(H) will be passing the numeric scalar h. That will try to use piecewise() with the numeric scalar 0. That will fail because piecewise() is only defined when the first parameter is symbolic.
If the piecewise did not fail, then you would try to assign to g(h) but h is 0, so that would be an attempt to assign to g(0) which is an error because subscripts must be positive integers.
You are confusing subscripts and formulas. MATLAB only permits formulas (symbolic functions) to be built for the case where the output subscript is a symbolic variable, such as
syms x; f(x) = x^2;
You are not wanting to return a formula from your functions: you are wanting to return numeric values.
Now, imagine that you had initialized H(n) = 1, so that you would have called gravity(1) the first time, and imagine you had fixed the piecewise to somehow accept numeric parameters. Then you would assign to g(1) which would be valid. Then imagine that you had also made similar fixes to the other functions. One of your assignments would be to H(n+1) which would be H(2) so H would become a vector of length 2. Now consider the second iteration of the while loop, when n has been incremented to 2. Then gravity(H) would pass in the vector of length 2 to gravity, you would use those two values as subscripts in assigning to g(h)... if that did not fail, the output would be of length 2 or more. Which you are not going to be able to assign to the scalar g(n) in the while loop...

Plus de réponses (1)

Matthew Henry
Matthew Henry le 25 Fév 2019
So my errors are:
  1. A piecewise cannot be defined when the 1st parameter is a scalar
  2. I am assuming H is trying to pass 0 as the value when instead it will pass 0 as the index for g
  3. H would pass each (n+1) as a vector to g and not its scalar value, which would make sense for the too many inputs error I am getting
I am starting to see where I'm going wrong. I will scrap the piecewise and go with an if/else loop for the piecewise statement then.
Thank you!
  1 commentaire
Walter Roberson
Walter Roberson le 25 Fév 2019
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.

Connectez-vous pour commenter.

Tags

Produits


Version

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by