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 件のコメント

dpb
dpb 2020 年 4 月 3 日
編集済み: dpb 2020 年 4 月 3 日
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.
Dimitris Moutzouris
Dimitris Moutzouris 2020 年 4 月 3 日
I get an error which says "Index exceeds matrix dimensions.". Do you maybe know why is that?
dpb
dpb 2020 年 4 月 3 日
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.
Dimitris Moutzouris
Dimitris Moutzouris 2020 年 4 月 3 日
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.
Ameer Hamza
Ameer Hamza 2020 年 4 月 3 日
dbp, that correct. I didn't notice that at first, too. :D

サインインしてコメントする。

 採用された回答

Ameer Hamza
Ameer Hamza 2020 年 4 月 3 日
編集済み: Ameer Hamza 2020 年 4 月 3 日

0 投票

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 件のコメント

Dimitris Moutzouris
Dimitris Moutzouris 2020 年 4 月 3 日
I get an error,like the code in the comment above which says that the index exceeds matrix dimensions
Ameer Hamza
Ameer Hamza 2020 年 4 月 3 日
Can you paste your code here? Note that number of rows in the colors matrix should be equal to the iterations of for loop.
Dimitris Moutzouris
Dimitris Moutzouris 2020 年 4 月 3 日
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 :( .
Ameer Hamza
Ameer Hamza 2020 年 4 月 3 日
Ok. I found a mistake in my code. Please check the updated code.
Dimitris Moutzouris
Dimitris Moutzouris 2020 年 4 月 3 日
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" .
Ameer Hamza
Ameer Hamza 2020 年 4 月 3 日
Yes, thats correct.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLine Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by