How to plot one variable with every value of another in loop

R=250;
for d=0:R
HMmax=20;
for i=1:10:250
HM1(i)= max(HMmax*((1-d/R)^4),0);
end
plot(HM1(i),d);
end
For every value of HM1, plot d.

2 comentarios

Why? In your "for i" loop, the only thing that changes is the subscript of HM1, so all the HM1 are going to be identical. There does not appear to be any value in plotting d for each value of HM1.
Akanksha
Akanksha el 31 de Mzo. de 2013
Editada: Akanksha el 31 de Mzo. de 2013
But along with the "for i" loop, there is "for d" loop also. I want to change the value of HM1 with different values of d (such that as d changes HM1 also changes) & in the end plot should be between d and HM1. I could not find better solution to this. graph should be somewhat like this

Iniciar sesión para comentar.

 Respuesta aceptada

Change the plot call to
plot(HM1(i), d, 'o');
hold on
Good luck interpreting it.

4 comentarios

Akanksha
Akanksha el 31 de Mzo. de 2013
Editada: Walter Roberson el 31 de Mzo. de 2013
R=250;
for i=1:15:250
for d=0:R
HMmax=20;
HM1(i)= max(HMmax*((1-d/R)^4),0);
%end
plot(HM1(i),d,);
hold on
end
end
I have changed the code this way now but still the exact plot i'm not getting it.
I want a graph like this
Thanks alot for your continuous help!
Your code writes to the same location of HM1 for all "d" values for any one iteration of "i". The effect is as-if you had only executed the "for d" loop once with d being the final value, R.
I cannot tell from your variable names which variables correspond to the ones shown in the axes of that link.
As a design note: if you are plotting inside two nested "for" loops, then you should be expecting that the plot has a value for each combination of the two variables, either a z=f(x,y) 3D plot or else as many complete lines as iterations of one of the variables. If you are wanting to produce a single line, then you should expect that your program structure should not have plotting commands in an inner "for" loop. Indeed if you are wanting to produce a single solid line you should expect that your plotting command is after any loops you might need to construct the data.
Hello
my code is something like this
p=3
for k=1:p
%something
if %something
d=d+1
elseif %something
d=d-2
end
plot(k,d,'o');
hold on
end
It gives me 3 points.. how can I connect them with lines?
p=3
ds = zeros(p,1);
for k=1:p
%something
if %something
d=d+1
elseif %something
d=d-2
end
ds(k) = d;
end
plot(d,'-o');

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 30 de Mzo. de 2013

Comentada:

el 4 de Abr. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by