I am unable to understand the error, Output argument "k" (and maybe others) not assigned during call to "trail3>A".

1 vue (au cours des 30 derniers jours)
function k = A(ia,tete)
if 0<tete && tete< pi/3
k = ia;
elseif pi/3 <tete && tete<2*pi/3
k = ia;
elseif 2*pi/3<tete && tete<pi
k = 0;
elseif pi<tete && tete<4*pi/3
k = -ia;
elseif 4*pi/3<tete && tete<5*pi/3
k = -ia;
elseif 5*pi/3<tete && tete<2*pi
k =0;
end
end

Réponse acceptée

Image Analyst
Image Analyst le 7 Mai 2021
None of your if blocks ever gets entered and so k is never defined, yet you try to return it -- hence the error. Assign it to whatever value you want it to have if no criteria is met, like inf, [], 0, or -1 or whatever. I always initialize my variables to something right in the first lines in the function so that in case something goes wrong, at least you won't get the error like you got. Fixed code:
function k = A(ia,tete)
% Initialize k
k = 0;
if 0 < tete && tete < pi/3
k = ia;
elseif pi/3 < tete && tete < 2*pi/3
k = ia;
elseif 2*pi/3 < tete && tete < pi
k = 0;
elseif pi < tete && tete < 4*pi/3
k = -ia;
elseif 4*pi/3 < tete && tete < 5*pi/3
k = -ia;
elseif 5*pi/3 < tete && tete < 2*pi
k =0;
else
% You get here if none of the other criteria were met.
k = 0;
end
end

Plus de réponses (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by