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 ?

답변 (2개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021년 10월 30일

0 개 추천

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

댓글 수: 1

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

댓글을 달려면 로그인하십시오.

John D'Errico
John D'Errico 2021년 10월 30일

0 개 추천

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.

카테고리

도움말 센터File Exchange에서 Line Plots에 대해 자세히 알아보기

제품

릴리스

R2021a

질문:

2021년 10월 29일

답변:

2021년 10월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by