i'm trying to disply ht vs time in a plot, but the graph is ending up blank, what am i doing wrong.

1 visualización (últimos 30 días)
I'm trying to get this to work but im sure im just missing something fundamental. Here is my code:
function ht=find_ht(t)
if t>=0;
ht=-9.8/2*(t^2)+125*t+500;
else
ht='error';
end
plot(t,ht)
ax.XLim = [0 30]
ax.XTick= [0:0.5:30]

Respuesta aceptada

dpb
dpb el 30 de Sept. de 2016
Editada: dpb el 30 de Sept. de 2016
Several issues...first, how are you trying to use the function? If you call it in a loop over some time interval with individual values for t, it'll work but not do what you're wanting; it'll plot the one point only. Since default there isn't a marker, it's not surprising you may not even see that single pixel set. If you were to change to
plot(t,ht,'*')
then you'd see that point be redrawn (altho maybe too fast to see much other than the last one).
"The Matlab way" would be to pass in the vector of t and use the vectorized functions in Matlab to compute it all in "one swell foop" (to use a spoonerism). Then, you'll need to use the "dot" operators for the mathematical operations to operate on an element-by-element basis. See the documentation on 'Arithmetic Operators' for the full list but you'll need .^ here. ./ and .* won't hurt and would be good practice but aren't required as a constant can multiply any sized object by default.

Más respuestas (1)

Star Strider
Star Strider el 30 de Sept. de 2016
Try this:
function ht=find_ht(t)
if t>=0;
ht=-9.8/2*(t.^2)+125*t+500;
else
ht='error';
end
end
t = [0:0.5:30];
plot(t,find_ht(t))
ax.XLim = [0 30];
ax.XTick= [0:0.5:30];

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by