How to change plot marks with each step

Hello everyone,
So I have the following code. What I want is to plot the same colors at marks under the same "i" value. So for example if i =200 then the marks are blue, if i= 300 all marks are red etc. Does anybody know how to do that? I tried several things but didn't work.
R = 83.1446;
b = 58;
for i = 200:100:1000
T = i + 273.15;
d = (9380 - (8.53.*T))
c = (28.31 + (0.10721.*T))
e = (-368654 + (715.9.*T))
for v = 150:1:250;
y = b./(4*v);
a = c + d./v + e./(v.^2);
P = (R*T.*(1 + y + y.^2 - y.^3))./(v.*(1-y).^3)- a./(sqrt(T)*v.*(v+b));
Z = (1+y+y.^2-y.^3)./((1-y).^3) - a./(R*T^1.5.*(v+b));
lnfi =(8*y-9*y.^2+3*y.^3)
fi=exp(lnfi);
f=abs(fi.*P);
plot(P,f,'.')
drawnow
hold on
end
end
hold off

5 Comments

In plot(P,f,'.') there's no reference to color and so you'll just get default. Make up a color list for each i and use it:
If you use plot, each plot call is a new line handle; and a line can only have one color. You could simplify that process by rearranging to save the values for each T and plot all at one time with the chosen color. That could also eliminate one loop by using the vectorized form of the equations you've written as
v=150:250;
y = b./(4*v);
a = c + d./v + e./(v.^2);
P = (R*T.*(1 + y + y.^2 - y.^3))./(v.*(1-y).^3)- a./(sqrt(T)*v.*(v+b));
Z = (1+y+y.^2-y.^3)./((1-y).^3) - a./(R*T^1.5.*(v+b));
lnfi =(8*y-9*y.^2+3*y.^3)
fi=exp(lnfi);
f=abs(fi.*P);
hL(i)=plot(P,f,'.',clr(i,:));
Where clr(i,:) is the defined color triplet for ith T you'll have defined before beginning the loop.
I get an error which says "Index exceeds matrix dimensions.". Do you maybe know why is that?
Because you buried data in the for...end loop on i and both Ameer and I fell into the trap...
Instead of
for i = 200:100:1000
T = i + 273.15;
...
use something like
T0=[200:100:1000]; % put the temperature data out of code so easily changed
for i = 1:numel(T0)
T = T0(i) + 273.15; % do the units conversion
...
Would be even better to vectorize, but above is the least editing of existing code to make data independent.
Thank you so much dpb !! I was planning to make that change as well but I was really frustrated that i couldn't solve the color problem haha!
I still have many things to learn.
dbp, that correct. I didn't notice that at first, too. :D

Sign in to comment.

 Accepted Answer

Ameer Hamza
Ameer Hamza on 3 Apr 2020
Edited: Ameer Hamza on 3 Apr 2020
See the twi lines i changed in your code. You need to input colors as RGB values.
R = 83.1446;
b = 58;
colors = rand(9,3); % <---- RGB value of colors. Each row contain one color.
count = 1;
for i = 200:100:1000
T = i + 273.15;
d = (9380 - (8.53.*T))
c = (28.31 + (0.10721.*T))
e = (-368654 + (715.9.*T))
for v = 150:1:250;
y = b./(4*v);
a = c + d./v + e./(v.^2);
P = (R*T.*(1 + y + y.^2 - y.^3))./(v.*(1-y).^3)- a./(sqrt(T)*v.*(v+b));
Z = (1+y+y.^2-y.^3)./((1-y).^3) - a./(R*T^1.5.*(v+b));
lnfi =(8*y-9*y.^2+3*y.^3)
fi=exp(lnfi);
f=abs(fi.*P);
plot(P,f,'.', 'Color', colors(count,:)); % <---- change here
drawnow
hold on
end
count = count + 1;
end
hold off

6 Comments

I get an error,like the code in the comment above which says that the index exceeds matrix dimensions
Can you paste your code here? Note that number of rows in the colors matrix should be equal to the iterations of for loop.
Well, I used the code that you gave me above in the answer. The i iterations are 9 in total And you have inserted 9 rows in the colors matrix. So I don't get why it doesn't work :( .
Ok. I found a mistake in my code. Please check the updated code.
Worked perfectly! So what you did is that you entered one color in each row and then you moved to the next one with the "count +1" .
Yes, thats correct.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!