How to draw a graph for function with constraints?

How can I plot the function z =-(x+10)*(y^2+15) with the constraints x+2y =30; -30<x<30; -30<y<30 ?

Answers (2)

You can consider fplot(): https://www.mathworks.com/help/matlab/ref/fplot.html

1 Comment

Note that fplot does NOT solve the problem posed. It does not provide the ability to plot, WITH constraints.

Sign in to comment.

You want to plot a function, that appears to be a function of two variables. But the equality constraint reduces the problem to a 1-dimensional problem. So you have what I would suggest are two choices.
  1. Solve for y, as a function of x, in the equality constraint. You will see that y = (30-x)/2. (Or the other way around, as you please.) Substitute for y in that function. The result will be that z is now purely a function of only x. Now you will plot z(x). Be careful, because you will still need to consider the bound constraints on both x and y. Again, this will be purely a simple plot, z(x). (It should look like a cubic polynomial.)
  2. Alternatively, generate a set of points that satisfy the relationship x+2*y==30. (Subject to the bounds on x and y.) These will now be (x,y) pairs. Substitute those pairs into the relationship z(x,y). Plot said points, now using plot3(x,y,z). This will be a 3-d plot, where you will see what appears to be a curved path through the 3-d space (x,y,z). Be careful, when you form z(x,y), that you use the .* operator to compute z.
Either solution can be made to do what you want, depending on what you really want to see.

Products

Release

R2021a

Asked:

on 29 Oct 2021

Answered:

on 30 Oct 2021

Community Treasure Hunt

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

Start Hunting!