3
$\begingroup$

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

$\endgroup$
  • 1
    $\begingroup$ This question does not make sense to me. What do you mean "print" a function ? Do you mean define and plot ? $\endgroup$ – Lotus Oct 10 '17 at 10:57
  • $\begingroup$ Use ContourPlot, but fix your variable names first. $\endgroup$ – Alan Oct 10 '17 at 11:06
  • $\begingroup$ 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! $\endgroup$ – Michael E2 Oct 10 '17 at 11:42
  • $\begingroup$ 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 $\endgroup$ – Ana Oct 10 '17 at 12:25
  • $\begingroup$ Do you mean x^2 + y^2 == 5? $\endgroup$ – m_goldberg Oct 25 '17 at 3:52
3
$\begingroup$

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]]
| improve this answer | |
$\endgroup$
  • $\begingroup$ 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 $\endgroup$ – Ana Oct 10 '17 at 12:36
  • $\begingroup$ Try use less/greater in Function[{x, y}, x + 2/3*y - 1.3 <> 0. $\endgroup$ – Eduard Oct 10 '17 at 12:50
  • $\begingroup$ that is what I was looking for :) $\endgroup$ – Ana Oct 10 '17 at 13:11
3
$\begingroup$

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]

enter image description here

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

enter image description here

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]

enter image description here

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

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ that is what I was looking for :) $\endgroup$ – Ana Oct 10 '17 at 13:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.