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
2021-10-30
0 个投票
You can consider fplot(): https://www.mathworks.com/help/matlab/ref/fplot.html
1 个评论
John D'Errico
2021-10-30
Note that fplot does NOT solve the problem posed. It does not provide the ability to plot, WITH constraints.
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.
- 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.)
- 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 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!