fplot with two y-axis

3 views (last 30 days)
Angel Cuadras
Angel Cuadras on 20 Sep 2024
Edited: dpb on 20 Sep 2024
I would like to fplot two curves Sp and St. Sp in the left Y-axis and the ST in the right Y-Axis. I can get the figure with two axis, I can plot the two curves independently but not together in a single graph. Is it possible?
I am using this code
figure (10)
hold on
fplot(Sp,[0.001 1000],"black")
yyaxis left
ylabel("S_{p}")
ylim ([0.000 0.04])
hold on
fplot(St,[0.001 1000],"--")
yyaxis right
ylabel("S_{t} (JK^{-1})")
ylim ([0.000 0.004])
xscale log
box on
xlabel("R_{2}/R_{1}")
hold off
Thank you very much

Accepted Answer

Star Strider
Star Strider on 20 Sep 2024
Edited: Star Strider on 20 Sep 2024
This seems to work —
Sp = @(t) (5+sin(2*pi*log(t)))/750;
St = @(t) (1+cos(1.5*pi*log(t)))/500;
figure (10)
yyaxis left
fplot(Sp,[0.001 1000],"black")
ylabel("S_{p}")
ylim ([0.000 0.04])
yyaxis right
fplot(St,[0.001 1000],"--")
ylabel("S_{t} (JK^{-1})")
ylim ([0.000 0.004])
xscale log
box on
xlabel("R_{2}/R_{1}")
hold off
.

More Answers (2)

Shashi Kiran
Shashi Kiran on 20 Sep 2024
I understand that you want to plot two curves using yyaxis with fplot.
Based on the code you provided, here are some corrections made to achieve the correct result.
% Example functions
Sp = @(x) 0.02 * sin(log(x)) + 0.02;
St = @(x) 0.002 * cos(log(x)) + 0.002;
figure(10)
hold on
% Plot Sp using the left y-axis
yyaxis left
fplot(Sp, [0.001 1000], 'black')
ylabel("S_{p}")
ylim([0.000 0.04])
% Plot St using the right y-axis
yyaxis right
fplot(St, [0.001 1000], '--')
ylabel("S_{t} (JK^{-1})")
ylim([0.000 0.004])
% Common settings
set(gca, 'XScale', 'log') % Set x-axis to log scale
box on
xlabel("R_{2}/R_{1}")
hold off
Refer to the following documentations for more details about the functions:
  1. yyaxis: https://www.mathworks.com/help/matlab/ref/yyaxis.html?s_tid=doc_ta
  2. set: https://www.mathworks.com/help/matlab/ref/set.html?s_tid=doc_ta

dpb
dpb on 20 Sep 2024
Edited: dpb on 20 Sep 2024
You forgot to define the two variables to plot, but presuming they were defined, then
yyaxis left
fplot(Sp,[0.001 1000],"black")
ylabel("S_{p}")
ylim ([0.000 0.04])
yyaxis right
fplot(St,[0.001 1000],"--")
ylabel("S_{t} (JK^{-1})")
ylim ([0.000 0.004])
xscale log
xlabel("R_{2}/R_{1}")
should work. You tried to set the axes targets after the fact, not before...

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!