Error in function at if-elseif

2 visualizaciones (últimos 30 días)
Raul Vaida
Raul Vaida el 10 de Mzo. de 2018
Comentada: Raul Vaida el 10 de Mzo. de 2018
I've got the error Output argument "val" (and maybe others) not assigned during call to "yt". yt(x) funtion:
function [val] = yt(x)
if ((-9<=x) & (x<-3)) | ((3<=x) & (x<=9))
val = sin(5*x);
elseif (-3<=x) & (x<3)
val = cos(x) - cos(3) - sin(15);
end
and i call the function using the following
x = -9:9;
val = yt(x)
I've got no output from this.
  1 comentario
per isakson
per isakson el 10 de Mzo. de 2018
Editada: per isakson el 10 de Mzo. de 2018

Iniciar sesión para comentar.

Respuesta aceptada

Ahmet Cecen
Ahmet Cecen el 10 de Mzo. de 2018
Editada: Ahmet Cecen el 10 de Mzo. de 2018
x is a vector at this point so your comparisons are not resolving to a single true false. I am guessing this is what you meant to do:
x = -9:9;
val = yt(x)
function [val] = yt(x)
for i = 1:length(x)
if ((-9<=x(i)) && (x(i)<-3)) || ((3<=x(i)) && (x(i)<=9))
val(i) = sin(5*x(i));
elseif (-3<=x(i)) && (x(i)<3)
val(i) = cos(x(i)) - cos(3) - sin(15);
end
end
end
There are better ways to do this of course, but this is probably the easiest to relate.
  1 comentario
Raul Vaida
Raul Vaida el 10 de Mzo. de 2018
Thank you very much, it worked perfectly this way!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by