필터 지우기
필터 지우기

How to create different Plots for different functions while each function has been plotted using hold on

조회 수: 3 (최근 30일)
I created a code to calculate entropy and enthalpy for different pressures (which forms my x axis) at different temperatures.
I want to plot isotherms for enthalpy and using hold on, multiple isotherms in the same plot.
But using "hold on" not only clubs the different isotherms for enthalpy together in one plot, but also clubs entropy isotherms into the same plot too. Due to the difference in order of magnitude between the two, the entropy lines are simply not visible. Scaling enthalpy down by 10^3 has helped in making both of them visible but I want them to be in seperate graphs or plots.
%enthalpy plot (KJ/mol Vs Bar)-
hold on;
for t = 1:nt %varying temperature to create isotherms
for p = 1:np
enth(1, p) = 0.001*enthalpy(T(1, t), P(1, p), [T_ref, P_ref, H_ref], [A B], [a b c], R);
end
plot1 = plot(P, enth); %one plot for particular value of temperature
end
hold off
%entropy plot (J/K Vs Bar)-
hold on
for t = 1:nt
for p = 1:np
ent(1, p) = entropy(T(1, t), P(1, p), [T_ref, P_ref, S_ref], [A B], [a b c], R);
end
plot2 = plot(P, ent);
end
the indivisual functions of entropy and enthalpy are trivial, the long list of arguments is basically temperature and pressure that i want to calculate the function in plus the refernce states and constants.

채택된 답변

dpb
dpb 2022년 9월 6일
%enthalpy plot (KJ/mol Vs Bar)-
hold on;
for t = 1:nt %varying temperature to create isotherms
for p = 1:np
enth(1, p) = enthalpy(T(1, t), P(1, p), [T_ref, P_ref, H_ref], [A B], [a b c], R);
end
plot1 = plot(P, enth); %one plot for particular value of temperature
end
figure
%entropy plot (J/K Vs Bar)-
hold on
for t = 1:nt
for p = 1:np
ent(1, p) = entropy(T(1, t), P(1, p), [T_ref, P_ref, S_ref], [A B], [a b c], R);
end
plot2 = plot(P, ent);
end
What you missed is simply creating a new figure for the second plot.
Or you could use tiledlayout and have two subplots instead.
As a stylistic and computing note; if the two functions enth and entropy were vectorized, then you could simply call them with the T,P arrays and get back the arrays of results eliminating the looping at the outer level.
  댓글 수: 3
dpb
dpb 2022년 9월 6일
That's what I put in the code I posted in the Answer -- just insert the figure command and presto! a whole new figure window appears....
Aditya Prasad
Aditya Prasad 2022년 9월 6일
편집: Aditya Prasad 2022년 9월 6일
Oh i didn't see the change lol. Thanks a ton ! exactly what i had in mind, didn't know it would be this simple.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Cris LaPierre
Cris LaPierre 2022년 9월 6일
Perhaps what you need is to create a plot with two y axes, one for entropy and the other for enthalpy? If so, use yyaxis.
x = linspace(0,10);
y = sin(3*x);
yyaxis left
plot(x,y)
z = sin(3*x).*exp(0.5*x);
yyaxis right
plot(x,z)
ylim([-150 150])
  댓글 수: 2
Aditya Prasad
Aditya Prasad 2022년 9월 6일
this does solve the problem of scaling perhaps but as there are multiple isotherms corresponding to multiple temperatures, i'd much rather have them on different graphs all together
Cris LaPierre
Cris LaPierre 2022년 9월 6일
Then you need to create a new figure for each one before plotting. Use the figure command
x = linspace(0,10);
y = sin(3*x);
plot(x,y)
figure
z = sin(3*x).*exp(0.5*x);
plot(x,z)
ylim([-150 150])

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by