MATLAB Answers

0

Function doesn't return a vector from a vector input

Leonardus Yudha Septian Sutanto さんによって質問されました 2019 年 11 月 15 日 22:17
最新アクティビティ Star Strider
さんによって コメントされました 2019 年 11 月 15 日 22:57
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 件のコメント

サインイン to comment.

2 件の回答

Star Strider
回答者: Star Strider
2019 年 11 月 15 日 22:40
 採用された回答

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.

  2 件のコメント

It works. Thanks a lot!
Star Strider
2019 年 11 月 15 日 22:57
As always, my pleasure!

サインイン to comment.


dpb
回答者: dpb
2019 年 11 月 15 日 22:41

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

  1 件のコメント

It also works. Thanks a lot!

サインイン to comment.



Translated by