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

7 views (last 30 days)
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.

Accepted Answer

dpb
dpb on 6 Sep 2022
%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 Comments
dpb
dpb on 6 Sep 2022
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 on 6 Sep 2022
Edited: Aditya Prasad on 6 Sep 2022
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.

Sign in to comment.

More Answers (1)

Cris LaPierre
Cris LaPierre on 6 Sep 2022
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 Comments
Aditya Prasad
Aditya Prasad on 6 Sep 2022
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 on 6 Sep 2022
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])

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!