why is my plot not showing anything

44 visualizaciones (últimos 30 días)
lowcalorie
lowcalorie el 15 de Mayo de 2012
Comentada: Walter Roberson el 20 de Ag. de 2022
clc
k = 8.9870e+009;
q1 = .000000024820;
q2 = .000005200901;
for r = [.10,.15,.20,.25,.30,.40];
f = (k*q1*q2)/r.^2
end
plot(r,f)
hold on
axis([0 .5 0 .2])
i want to plot f vs r
  2 comentarios
Tanmay Varshney
Tanmay Varshney el 20 de Ag. de 2022
Editada: Walter Roberson el 20 de Ag. de 2022
//Please tell why my graph is not plotting//
v0 = 6.2;
g = 9.81;
theta = 45*(pi/180);
y0 = 1.5;
Vx = v0* cos(theta);
t = linspace(0,2);
x = Vx * t ;
y = y0 + v0 * t * sin(theta) - 0.5 * g * t.^2;
for i=1:size(x,2)
if (i>1 && y(i)<=0)
break;
end
plot(x(i),y(i))
xlabel ('Horizontal Distance(m)')
ylabel ('Vertical Distance(m)')
hold on;
pause(0.05);
end
Walter Roberson
Walter Roberson el 20 de Ag. de 2022
plot() does not add lines to points that have already been drawn in previous calls. plot() only draws lines when during a single call there are two or more finite coordinates in a row.
Your plot() call is only drawing one point at a time, not two or more, so it is not going to draw lines.
  • you can record all of the output and plot after the calculation loop. This is recommended
  • you can leave the plot call but add a marker such as '+' so that it will draw each point
  • instead of plot() you could switch to animatedline() and addpoints()

Iniciar sesión para comentar.

Respuesta aceptada

Oleg Komarov
Oleg Komarov el 15 de Mayo de 2012
You are overwriting f at every iteration and in the end you just have one point.
plot(r,f)
will plot thus one point. If you look carefully you'll find it.
Try this, the vectorized code:
k = 8.9870e+009;
q1 = .000000024820;
q2 = .000005200901;
r = [.10,.15,.20,.25,.30,.40];
f = (k*q1*q2)./r.^2
plot(r,f)
hold on
axis([0 .5 0 .2])

Más respuestas (2)

Thomas
Thomas el 15 de Mayo de 2012
try
k = 8.9870e+009;
q1 = .000000024820;
q2 = .000005200901;
r = [.10,.15,.20,.25,.30,.40];
for count=1:numel(r)
f(count) = (k*q1*q2)/r(count).^2 % save f as array
end
plot(r,f)
hold on
axis([0 .5 0 .2])
You were not saving f as array and hence were only plotting one f and one r pair..
You can avoid the loop totally as well using:
f = (k*q1*q2)/r.^2;
plot(r,f)

rajdeep singh
rajdeep singh el 9 de Feb. de 2020
What is r is a function of random numbers? Like data points
  1 comentario
Walter Roberson
Walter Roberson el 9 de Feb. de 2020
Oleg's and Thomas's solutions do not care how you create r and do not care whether r is sorted.
The one thing to watch out for if r is not sorted is that because by default plot() connects points that are adjacent in data order, you would get a lot of lines back and forth across the screen. sorting on r prevents that problem.
for idx=1:10
r(idx) = rand() ;
f(idx) = (k*q1*q2)/r(idx).^2;
end
[sr, sidx] = sort(r) ;
sf = f(sidx);
plot(sr, sf)

Iniciar sesión para comentar.

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by