Piecewise 3D plotting

Hi, I need to do a 3D plot of the following functions using piecewise:
y1 = -0.00278018473212*x.^3 + 0.0764514697018*x.^2 + -0.481036946424*x + 4.37885609332;% both need to be in the interval of 4 < x < 14 as well
y2 = 0.0000772436429838*x^3+0.0146583319491*x^2-0.184551271403*x+4.99872818128;% both need to be in the interval of 4 < x < 14 as well
y3 = 4.5 %interval 2.5 < x < 4
y5 = 5.5 %interval 14 < x < 14.5
y6 = 5.0 %interval 14 < x < 14.5
Z = piecewise(2.5 < x < 4 ,y3,4 < x < 14,y2,14 < x < 14.5, y5);
Error using surf
Z must be a matrix, not a scalar or vector.
How can I:
1) plot the function correctly
and
2) add 'y1' into the piecewise as it occupies the same interval as y2 (4<x<14)?
Thanks in advance.

5 个评论

Matt J
Matt J 2023-9-5
编辑:Matt J 2023-9-5
Surface plots have 2 independent variables, but you appear to have only one, namely x. So, in what way is it a 3D plot?
I just edited it for it to look clearer, the other plot was supposed to be 2D, but I now need to make a 3D plot
Matt J
Matt J 2023-9-5
编辑:Matt J 2023-9-5
It's not clearer because Z still depends on only one independent variable, not two.
As a preliminary remark, though, ndgrid and surf are not doing anything meaningful here. Z is a symbolic variable, which surf() doesn't know how to deal with.
So what would Z need to be in this case?
Matt J
Matt J 2023-9-5
编辑:Matt J 2023-9-5
I see that you have edited your post a second time, but the revisions you've made are irrelevant to the questions I asked you before.You will have no hope of clarifying the post until you answer them.
So once again: Are you trying to make a surface plot? In a surface plot, Z is a function of two variables Z(x,y). But in your post, Z depends on only one one variable Z(x). So, in what way is Z() to be seen as a function of two variables?

请先登录,再进行评论。

回答(1 个)

Perhaps this is what you meant:
syms Z(x,y)
y1 = -0.00278018473212*x.^3 + 0.0764514697018*x.^2 + -0.481036946424*x + 4.37885609332;% both need to be in the interval of 4 < x < 14 as well
y2 = 0.0000772436429838*x^3+0.0146583319491*x^2-0.184551271403*x+4.99872818128;% both need to be in the interval of 4 < x < 14 as well
Z1(x,y) = piecewise( 2.5 < x & x < 4 , 4.5,...
4 < x & x < 14, y1,...
14 < x & x < 14.5, 5.5 );
Z2(x,y) = piecewise( 2.5 < x & x < 4 , 4.5,...
4 < x & x < 14, y2,...
14 < x & x < 14.5, 5.5 );
fsurf(Z1); hold on; fsurf(Z2); hold off
xlim([2.5 14.5])
ylim([2.5 14.5])

1 个评论

Not quite, maybe I should rewrite my code and rephrase my post so that it is clearer what I want to do

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Mathematics 的更多信息

产品

版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by