MATLAB Answers

Function doesn't return a vector from a vector input

2 views (last 30 days)
function Ffork = Fforkfase3(t)
Feff=35; %Load applied by the driver at the shift lever [N], Maximum value allowed: 180-250 N
Feff_max=90; %Max effort applied by the driver [N]
eff=0.6; %Efficiency of the shift mechanism (normally <70%)
TR=7; %Transmission ratios of the shift mechanism (Normally varies between 7:1 and 12:1)
Ffork0=Feff*eff*TR; %Load on the sleeve [N]
Ffork_max=Feff_max*eff*TR; %Max load on the sleeve [N]
Inc_Ffork=(Ffork_max-Ffork0)/0.2;
if Ffork0+Inc_Ffork*t <= Ffork_max
Ffork=Ffork0+Inc_Ffork*t;
elseif Ffork0+Inc_Ffork*t > Ffork_max
Ffork = Ffork_max;
end
end
Hi,
I have a function with a distinction of cases. The force Ffork should be constant once it reached the maximum value, as you can see in the code.
The input t should be a vector, I called the function in this way:
t=0.08:0.01:0.2727
Ffork = Fforkfase3(t)
I would like to get a vector of Ffork as output. However, I got an error message after running it:
Output argument "Ffork" (and maybe others) not assigned during call to "Fforkfase3".
Could someone tell me my mistake?

  0 Comments

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 15 Nov 2019
Could someone tell me my mistake?
You need to use the any or all functions in the if statements, depending on what you want to do:
if any(Ffork0+Inc_Ffork*t <= Ffork_max)
Ffork=Ffork0+Inc_Ffork*t;
elseif any(Ffork0+Inc_Ffork*t > Ffork_max)
Ffork = Ffork_max;
end
See the documentation on any (and all) to determine the correct code for your function.

More Answers (1)

dpb
dpb on 15 Nov 2019
Yes, you didn't read about logical IF
if Ffork0+Inc_Ffork*t <= Ffork_max
is True iff every element of Ffork0+Inc_Ffork*t is <= Ffork_max and similarly for the eseif clause. The upshot is your code didn't pass either test and so the if clause was never executed.
However, "the MATLAB way" doesn't need the if clause anyways; use the vector nature...
function Ffork = Fforkfase3(t)
Feff=35; %Load applied by the driver at the shift lever [N], Maximum value allowed: 180-250 N
Feff_max=90; %Max effort applied by the driver [N]
eff=0.6; %Efficiency of the shift mechanism (normally <70%)
TR=7; %Transmission ratios of the shift mechanism (Normally varies between 7:1 and 12:1)
Ffork0=Feff*eff*TR; %Load on the sleeve [N]
Ffork_max=Feff_max*eff*TR; %Max load on the sleeve [N]
Inc_Ffork=(Ffork_max-Ffork0)/0.2;
Ffork=min(Ffork0+Inc_Ffork*t,Ffork_max);
end

Sign in to answer this question.