# Plotting function with 2 variables within constraints

I have a function of two variables:

With some values

Now I have to plot this, so that tha values of U are between -5 and 0. How do I do this? (Note: It's not the values of x and y that need to be between -5 and 0.)

Thanks!

A nice smooth surface can be obtained simply by using the plot3d command along with your operator U as given.

restart:

U:=(x,y)->-M1/sqrt(x^2+y^2)-M2/sqrt((x-a)^2+y^2)-.5*omega^2*((x-M2*a/(M1+M2))^2+y^2):

M1:=1: M2:=1: a:=5: omega:=0.2:

The view option will restrict the z-range displayed. So it is easy to constrain it to z=-5..0.

plot3d(U, -20..20, -20..20, view=-5..0, labels=[x,y,z]);

But you could also compute the x- and y-ranges for the plot3d call so that they are tightly constrained by the lower value of z=-5. (There are various ways to compute those tight bounds.)

xlo:=Optimization:-Minimize(x,{U(x,y)>=-5,U(x,y)<=0,x<=-10,x>=-20},y=-10..10)[1];

xlo := -13.1019759104502107

xhi:=Optimization:-Maximize(x,{U(x,y)>=-5,U(x,y)<=0,x>=10,x<=20},y=-10..10)[1];

xhi := 18.1019759104502107

ylo:=Optimization:-Minimize(y,{U(x,y)>=-5,U(x,y)<=0,y<=-10,y>=-20},x=-10..10)[1];

ylo := -15.6100765315323216

yhi:=Optimization:-Maximize(y,{U(x,y)>=-5,U(x,y)<=0,y>=10,y<=20},x=-10..10)[1];

yhi := 15.6100765315323216

plot3d(U, xlo..xhi, ylo..yhi, view=-5..0,labels=[x,y,z]);

• thanks for your nice and correct remarks. Honestly, what I did was what I know about the request. I am not professional as you always do here for various kind of questions tagged by Maple. :-) I changed some points in the plot to be good looking and that's why you saw some inconsistent factors between codes and plot. Any way thanks again. Indeed, your complete approach should have been accepted. :-) Jan 11, 2014 at 8:38
• This works brilliantly, thanks a lot! Jan 11, 2014 at 10:48

Here are the required codes in Maple:

[> f := (m1, m2, w, a)-> -m1/sqrt(x^2+y^2)-m2/sqrt((x-a)^2+y^2)-.5*w^2*((x-m2*a/(m1+m2))^2+y^2):
[> g:=(x,y)-> f(1,2,0.2,5):
with(plots): implicitplot3d(z=g(x,y),x=-10..10,y=-10..10,z=-5..0);

• Can you recommend a good book for learning Maple? Nice Plot +1 Jan 11, 2014 at 3:14
• Differential Equation with Maple by B. R. Hunt is a good one in our interest. Jan 11, 2014 at 3:25
• The image shown is not what that Maple code produces, for the following reasons: the x- and y-ranges in the code are -10..10 but the plot shows about -50..50, and the plot appears to be style=patchnogrid, and implicitplot3d will not produce such a smooth surface by default (you'd have to add some option, eg grid=[50,50,50] or some other option). Also, M2 was stated as being 1, rather than 2, but that's a quibble. More relevant is that implicitplot3d is an expensive a slow way to produce a smooth surface for this example. More simple is just to use the plot3d command.
– acer
Jan 11, 2014 at 8:06