Undefined function or variable 'v'.??

t=-5:50;
if t<=8
v=10.*(t.^2)-(5.*t);
elseif (t>=8) & (t<=16)
v=624-(5.*t);
elseif (t>=16) & (t<=26)
v=(36.*t)+12.*(t-16).^2;
elseif (t>26)
v=2136.*exp(-0.1.*(t-26));
elseif (t<0)
v=0;
end
plot(t,v)

Respuestas (1)

Rik
Rik el 3 de Jun. de 2018
You made the classic mistake of thinking that this set of logical statements would be handled for each element of your vector. But Matlab doesn't work that way, unless you tell it with a for-loop.
The code below uses logical indexing to get the result.
t=-5:50;
v=zeros(size(t));
L=t<=8;
v(L)=10.*(t(L).^2)-(5.*t(L));
L=(t>=8) & (t<=16);
v(L)=624-(5.*t(L));
L=(t>=16) & (t<=26);
v(L)=(36.*t(L))+12.*(t(L)-16).^2;
L=t>26;
v(L)=2136.*exp(-0.1.*(t(L)-26));
L=t<0;
v(L)=0;
plot(t,v)

3 comentarios

Libby Goodes
Libby Goodes el 3 de Jun. de 2018
What does the second line do?
Rik
Rik el 3 de Jun. de 2018
The second line pre-allocates the vector v by filling it with zeros. This way we can use the same logical vector for v as for t.
Have you tried reading the documentation for zeros and size?
Rik
Rik el 6 de Jun. de 2018
Did this suggestion solve your problem? If so, please consider marking it as accepted answer. It will make it easier for other people with the same question to find an answer. If this didn't solve your question, please comment with what problems you are still having.

Iniciar sesión para comentar.

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Preguntada:

el 3 de Jun. de 2018

Comentada:

Rik
el 6 de Jun. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by