Problem with custom function
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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?
0 commentaires
Réponse acceptée
Raghav
le 4 Juil 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))
0 commentaires
Plus de réponses (1)
Siraj
le 4 Juil 2022
Modifié(e) : Siraj
le 4 Juil 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.
A=Bsize(10,50,100)*365
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.
0 commentaires
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!