Plotting Multiple Curves on the Same Graph

I am plotting a graph of radial velocity against varying radius, but there is also a parameter in the velocity expression similar to the Reynolds number which can have various values, so I would like to set the value of the parameter to 0, 1 and 0.2 and to plot three curves on the same graph, what would be the easiest way to do this?

1 Comment

doc hold
Set e.g.
hold( hAxes, 'on' )
for your axes with handle hAxes, then you can add as many plots as you like.
Alternatively if you have all the results together in a matrix (assuming they are the same length for each parameter) you can just plot the matrix all in one go using
doc plot
and making sure you have it oriented the right way.

Sign in to comment.

 Accepted Answer

It depends on your function and what you want to do:
t = linspace(0, 2*pi);
freq = [1 5 9];
ampl = [1 2 3];
s = bsxfun(@times, ampl(:), sin(freq(:)*t));
figure
plot(t, s)
grid

6 Comments

As an example, say I have a constant bp
bp=-(60*pi + 345*pi*Kn + 450*(1+pi)*Kn.^2)/(40*pi + 270*pi*Kn + 18*(25*pi + 18)*Kn.^2 + 648*Kn.^3);
which is used to define a function
ur = (1 + (bp)/R - (1 + bp)/R.^3);
I then want to plot ur for R ranging from 1 to 10 and for 3 separate curves in each case where Kn=0, Kn=0.2 and for Kn=1.
You can plot them in at least three different ways:
bp = @(Kn) -(60*pi + 345*pi*Kn + 450*(1+pi)*Kn.^2)./(40*pi + 270*pi*Kn + 18*(25*pi + 18)*Kn.^2 + 648*Kn.^3);
ur = @(R,Kn) (1 + bp(Kn))./R - (1 + bp(Kn))./R.^3;
Knv = [0, 0.2, 1];
Rv = 1:10;
[Rm,Knm] = meshgrid(Rv,Knv);
figure
plot(Knv, ur(Rm,Knm))
grid
xlabel('K_n')
lgdc = sprintfc('R = %2d', Rv);
legend(lgdc, 'Location','SE')
figure
plot(Rv, ur(Rm,Knm))
grid
xlabel('R')
lgdc = sprintfc('K_n = %3.1f', Knv);
legend(lgdc, 'Location','SE')
figure
surfc(Rm,Knm,ur(Rm,Knm))
grid on
xlabel('R')
ylabel('K_n')
zlabel('ur')
This creates anonymous functions of your equations, then uses meshgrid to create matrices from the functions (you can also use ndgrid), then plots them with respect to each variable in the first two figure objects, and both variables in the last figure object. The sprintfc function is undocumented although highly useful. You can also use the compose function.
I tried the first example but did not get what I was intending, to be clear I basically need the function ur plotted on the y axis and then the radius R on the x axis running continuously from 1 to 10, with 3 separate curves on the graph corresponding to the results when Kn is given the 3 values I mention.
The second example also doesn't give me the curves I am expecting as I am expecting the y-axis to run from 0 to 1, so are you sure that the y-axis is the function ur?
testimage.png
... so are you sure that the y-axis is the function ur?
The original version of ‘ur’ that you provided had parentheses around ‘bp’ and in creating the function version of it I got confused by the extra parentheses:
ur = (1 + (bp)/R - (1 + bp)/R.^3);
since I was not sure where the parentheses belonged. However, that also resulted in the function being different.
Try this corrected version:
bp = @(Kn) -(60*pi + 345*pi*Kn + 450*(1+pi)*Kn.^2)./(40*pi + 270*pi*Kn + 18*(25*pi + 18)*Kn.^2 + 648*Kn.^3);
ur = @(R,Kn) (1 + bp(Kn)./R - (1 + bp(Kn))./R.^3);
Knv = [0, 0.2, 1];
Rv = 1:10;
[Rm,Knm] = meshgrid(Rv,Knv);
figure
plot(Knv, ur(Rm,Knm))
grid
xlabel('K_n')
lgdc = sprintfc('R = %2d', Rv);
legend(lgdc, 'Location','SE')
figure
plot(Rv, ur(Rm,Knm))
grid
xlabel('R')
lgdc = sprintfc('K_n = %3.1f', Knv);
legend(lgdc, 'Location','SE')
figure
surfc(Rm,Knm,ur(Rm,Knm))
grid on
xlabel('R')
ylabel('K_n')
zlabel('ur')
This appears to do what you want.
Many thanks for your answer, I have tried something slightly different but getting a bit of an unexpected error message. So now I am trying:
bt = @(Kn) -sqrt(pi/2)*(30*Kn.^2 + 180*(1+(1./pi))*Kn.^3)./(20*pi + 135*pi*Kn + 9*(25*pi +18)*Kn.^2 +324*Kn.^3);
theta = @(R,Kn) (bt./(R.^2));
Knv = [0, 0.2, 1];
Rv = 1:10;
[Rm,Knm] = meshgrid(Rv,Knv);
figure
plot(Rv, theta(Rm,Knm))
grid
%xlabel('R')
lgdc = sprintfc('K_n = %3.1f', Knv);
legend(lgdc, 'Location','SE')
I seem to get an error message related saying that the ./ in the expression for theta is an undefined operator for input arguments of type 'function_handle', although I have used it elsewhere with no problems.
As always, my pleasure!
Remember that in my code, ‘bt’ is a function, so it needs to be evaluated in any calculation using it, and that requires that it be supplied with a numeric argument so that it can return a numeric result:
theta = @(R,Kn) (bt(Kn)./(R.^2));
The complete code is now:
bt = @(Kn) -sqrt(pi/2)*(30*Kn.^2 + 180*(1+(1./pi))*Kn.^3)./(20*pi + 135*pi*Kn + 9*(25*pi +18)*Kn.^2 +324*Kn.^3);
theta = @(R,Kn) (bt(Kn)./(R.^2));
Knv = [0, 0.2, 1];
Rv = 1:10;
[Rm,Knm] = meshgrid(Rv,Knv);
figure
plot(Rv, theta(Rm,Knm))
grid
%xlabel('R')
lgdc = sprintfc('K_n = %3.1f', Knv);
legend(lgdc, 'Location','SE')
I tested that to be sure it works. It does.

Sign in to comment.

More Answers (0)

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!