How can my function work?

조회 수: 3 (최근 30일)
Wytse Petrie
Wytse Petrie 2019년 12월 12일
답변: Steven Lord 2019년 12월 12일
Hi all,
My function does not work and I do not understand. Is there someone who can help me?
function W = fishing_load_factor(t)
if 0<=t & t<=3/12
Wans=0
elseif 3/12<=t & t<8/12
Wans=2
elseif 8/12<=t & t<1
Wans=0
elseif
Wans=fishing_load_factor(t-1)
end

답변 (3개)

Star Strider
Star Strider 2019년 12월 12일
Either change the function output to ‘Wans’:
function Wans = fishing_load_factor(t)
or change the ‘Wans’ within it to ‘W’.

Tom Holz
Tom Holz 2019년 12월 12일
First, change Wans to W everywhere, otherwise you are not setting the output variable.
Second, your final elseif should be changed to an else.
Finally, you might want to add a semicolon to the end of all the lines where you assign values. (Not required, just prevents lots of extra output).

Steven Lord
Steven Lord 2019년 12월 12일
In addition to what the others have stated, since you're using & in your if conditions I'm guessing you're passing a vector, matrix, or other non-scalar t into fishing_load_factor.
How does if handle its condition being non-scalar? From the documentation "An expression is true when its result is nonempty and contains only nonzero elements (logical or real numeric). Otherwise, the expression is false." So your the first if condition will only be satisfied if all the elements of t are greater than or equal to 0 and less than or equal to 3/12.If even one element fails that test, the if condition is not satisfied and MATLAB moves on to the first elseif.
If you are passing a non-scalar t into fishing_load_factor you may want to use logical indexing to fill in the appropriate elements in Wans or W.

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by