# Plot multivariable function with constraints [closed]

Is it possible to plot some function similar to $f(x,y)=x+2y$ with a constraint like this? $x2+y2=5$

We want to optimize (i.e. find the minimum and maximum value of) a function, $f(x, y)$, subject to the constraint $g(x,y)=0$. And I would like to know if it is possible to plot function f with the restriction g

Thanks

• This question does not make sense to me. What do you mean "print" a function ? Do you mean define and plot ? Commented Oct 10, 2017 at 10:57
• Use ContourPlot, but fix your variable names first.
– Alan
Commented Oct 10, 2017 at 11:06
• Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! Commented Oct 10, 2017 at 11:42
• The problem is this: We want to optimize (i.e. find the minimum and maximum value of) a function, f(x, y), subject to the constraint g(x,y)=0. And I would like to know if it is possible to plot function f with the restriction g
– Ana
Commented Oct 10, 2017 at 12:25
• Do you mean x^2 + y^2 == 5? Commented Oct 25, 2017 at 3:52

Something like?

Plot3D[x + 2 y, {x, -Sqrt[5], Sqrt[5]}, {y, -Sqrt[5], Sqrt[5]},
RegionFunction -> Function[{x, y}, x^2 + y^2 < 5]]

• Plot3D[4000*x*y^3, {x, 0, 1}, {y, 0, 2}, RegionFunction -> Function[{x, y}, x + 2/3*y - 1.3 = 0]] works in the same way for me with or without constraint
– Ana
Commented Oct 10, 2017 at 12:36
• Try use less/greater in Function[{x, y}, x + 2/3*y - 1.3 <> 0. Commented Oct 10, 2017 at 12:50
• that is what I was looking for :)
– Ana
Commented Oct 10, 2017 at 13:11

Using ConditionalExpression:

Plot3D[ConditionalExpression[x + 2 y, x^2 + y^2 <= 5],
{x, -Sqrt[5], Sqrt[5]}, {y, -Sqrt[5], Sqrt[5]},
BoundaryStyle -> Directive[Thick, Red], Mesh -> None]


Add the option PlotStyle->None to display only the boundary:

Using MeshFunctions:

Plot3D[x + 2 y, {x, -Sqrt[5], Sqrt[5]}, {y, -Sqrt[5], Sqrt[5]},
MeshFunctions -> Function[{x, y, z}, x^2 + y^2], Mesh -> {{5}},
MeshStyle -> Directive[Thick, Red],
MeshShading -> {Automatic, None}, BoundaryStyle -> None]


Use MeshShading -> {None, None} to get the boundary only:

• that is what I was looking for :)
– Ana
Commented Oct 10, 2017 at 13:10