MATLAB Answers

how to plot a function symbolically?

1 view (last 30 days)
Randy Chen
Randy Chen on 26 Nov 2020
Edited: John D'Errico on 26 Nov 2020
I would like to plot the following function:
where g is the only known constant = 9.8. How should I plot this function z(r) without specifying exact values for H,C,and r ? I just need the general shape of this function (including r = 0 where there should be an asymptote)

  0 Comments

Sign in to comment.

Answers (3)

Walter Roberson
Walter Roberson on 26 Nov 2020
You have 3 independent variables and 1 dependent variable. You will need a 4D plot. Unfortunately:
  1. there are no symbolic 4d plotting routines
  2. with the work-arounds needed to represent 4d, you will not be able to get the general shape of the curve.
If you just want to see the shape with respect to r then I recommend that you do pick specific values of H and C, as the choice will not matter for the purposes of seeing the shape of the curve, provided that H and C are real valued. You might as well choose H=0 and C=sqrt(2*g) which would make the equation -1/r^2 which you can fplot.
However there is no asymptope at r=0, there is a singularity to infinity unless C=0

  1 Comment

Randy Chen
Randy Chen on 26 Nov 2020
oh sorry, i forgot that C and H should be cosntants here.So assuming C=1, H = 0.35, how should I plot this function?

Sign in to comment.


Stephan
Stephan on 26 Nov 2020
g = 9.8;
r = -10:0.1:10;
H = -10:0.1:10;
C = -10:0.1:10;
fun = @(r,H,C) H - C.^2./(2*g*r.^2);
z_r = fun(r,1,1);
z_H = fun(1,H,1);
z_C = fun(1,1,C);
subplot(3,1,1)
plot(r,z_r,'b-','LineWidth',2)
title('Varying r in the range (-10...10), H=1, C=1')
xlabel('r')
ylabel('z')
subplot(3,1,2)
plot(H,z_H,'b-','LineWidth',2)
title('Varying H in the range (-10...10), r=1, C=1')
xlabel('H')
ylabel('z')
subplot(3,1,3)
plot(C,z_C,'b-','LineWidth',2)
title('Varying C in the range (-10...10), H=1, r=1')
xlabel('C')
ylabel('z')

  1 Comment

Stephan
Stephan on 26 Nov 2020
When C, H known:
g = 9.8;
r = -10:0.1:10;
fun = @(r,H,C) H - C.^2./(2*g*r.^2);
z_r = fun(r,0.35,1);
plot(r,z_r,'b-','LineWidth',2)
title('r in the range (-10...10), H=0.35, C=1')
xlabel('r')
ylabel('z')
or:
g = 9.8;
H = 0.35;
C = 1;
fun = @(r) H - C^2./(2*g*r.^2);
fplot(fun,[-10 10],'b-','LineWidth',2)
title('r in the range (-10...10), H=0.35, C=1')
xlabel('r')
ylabel('z')

Sign in to comment.


John D'Errico
John D'Errico on 26 Nov 2020
Edited: John D'Errico on 26 Nov 2020
You have the relationship
z(r) = H - C^2/(2*g*r^2)
If H and C are not known, with g fixed at 9.8, then they make this into an entire family of curves. But the family varies trivially as a function of H and C. (For that matter, the entire curve is pretty basic, but I won't go there.) And plotting the entire family would make the result into a 4 dimensional thing.
But suppose you knew H and C? I'll write z as a function of all parameters, with g fixed.
g = 9.8;
z = @(r,H,C) H - C^2./(2*g*r.^2);
That now builds the value of g into z. We could plot z, for a known pair of H and C.You suggested C == 1, H = 0.35.
fplot(@(r) z(r,0.35,1),[0,5])
h = gca;
h.YLim = [-5,.5];
yline(0.35);
I added the black line to indicate the upper asymptote. As r-->0 from the right, this curve appoaches -inf, so there is a singularity at 0.
Could you overlay additional curves on top of that, showing the behavior of the curve as H varies? Well, yes. It would get complicated though, and more difficult to visualize.
If you change H, the entire curve becomes translated vertically in y.
Changing C changes only the scaling on the Y axis.
Honestly, those parameters are pretty simple in how they change the curve, but if you tried to stuff all that into one plot, it would become unwieldy and difficult to visualize. You might try picking just a few values for H, thus...
fplot(@(r) z(r,0.35,1),[0,5],'k')
hold on
fplot(@(r) z(r,0.5,1),[0,5],'r')
fplot(@(r) z(r,0.25,1),[0,5],'r')
fplot(@(r) z(r,0.35,2),[0,5],'b')
fplot(@(r) z(r,0.35,.5),[0,5],'b')
legend('H = 0.35,C = 1','H = 0.5,C = 1','H = 0.25,C = 1','H = 0.35,C = 2','H = 0.35,C = 0.5')
h = gca;
h.YLim = [-5,1];
As you can see, this becomes complex, even for a simple set of parameter variations. Far easier to just understand how the curves vary.

  0 Comments

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by