Problem with custom function

4 Ansichten (letzte 30 Tage)
Pietro Fiondella
Pietro Fiondella am 4 Jul. 2022
Bearbeitet: Siraj am 4 Jul. 2022
I have write this function
function BESSsize = Bsize(x,y,z)
if 0.8*z >=x+y
Bsize=(x+y)*0.8/365
elseif 0.5*x+y<= z <0.8*x+y
Bsize=z/365
else
disp("Bad sizing")
end
It dosent run here but I saved in matlab and it works in my scripts.My problem is that if i try for example to multiply and use the result for example A=Bsize(10,50,100)*365 i obtain this error:
>> Bsize(10,50,100)
Bsize =
0.1315
>> A=Bsize(10,50,100)*365
Bsize =
0.1315
Output argument "BESSsize" (and possibly others) not assigned a value in the execution with "Bsize" function.
How can i solve this?

Akzeptierte Antwort

Raghav
Raghav am 4 Jul. 2022
In the function definition:
function BESSsize = Bsize(x,y,z)
'BESSsize' is the variable which will be returned after function execution and the 'Bsize' is the function name.
You are assigning return value to 'Bsize', instead assign them to the returning variable 'BESSsize'.
So assign in the following way:
BESSsize=(x+y)*0.8/365;
and
BESSsize=z/365;
Also use ; if you do not want an output from statement in which you are assigning values.
One more thing is you are using
0.5*x+y<= z <0.8*x+y
which is of the form (a<=b)<c
I think it would be better to write in the form (a<=b) && (b<c) like:
((0.5*x+y)<=z) && (z<(0.8*x+y))

Weitere Antworten (1)

Siraj
Siraj am 4 Jul. 2022
Bearbeitet: Siraj am 4 Jul. 2022
Hi,
It is my understanding that you are confusing between the function name “Bsize” and the variable “BESSsize” which is to be returned by the function.
In the if and elseif part you are creating a variable “Bsize” with the same name of the function and the output variable that you declared to be “BESSsize” is not being assigned any value. Therefore, you are seeing the error.
The solution is to change the “Bsize” to “BESSsize” in the "if" and "else if" part and also assign a value to "BESSsize" for the else part.
Refer to the documentation for more on functions in MATLAB.
A=Bsize(10,50,100)*365
A = 48.0000
function BESSsize = Bsize(x,y,z)
if 0.8*z >=x+y
BESSsize = (x+y)*0.8/365;
elseif 0.5*x+y<= z <0.8*x+y
BESSsize = z/365;
else
disp("Bad sizing");
BESSsize = 0;
end
end
Hope it helps.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by