Using mesh to Plot a Piecewise Function in 3D
8 views (last 30 days)
Show older comments
Hi,
I am trying to use the mesh function to plot a piecewise function in 3D. I have the two parabola equations:
Z1: Y-((X+20).^2); Z2: Y-((X-20).^2);
When X>0 and Y>0, I would like to plot Z2,and when X<0 and Y>0, I would like to plot Z1. This should create a surface that looks like a curved downward V. I found a similar post (Piecewise constant surface in MATLAB) and tried modifying the code for this scenario, but am getting "In an assignment A(I) = B, the number of elements in B and I must be the same." (I've also tried using for loops and piecewise function equations, but have been running into matrix dimension errors.)
Any tips on the best way to create this surface would be greatly appreciated!
x=-100:10:100; y=-100:100;
[X,Y]=meshgrid(x,y);
Z=zeros(size(X));
Z(Y>0 & Y<100 & X>0 & X<100)=Y-((X-20).^2);
Z(Y>0 & Y<100 & X<0 & X>-100)=Y-((X+20).^2);
mesh(X,Y,Z);
Thank you!
(Using the code below, my surface should be the curves below the intersection point, but am stuck on figuring out how to get rid of the surface above the intersection.)
x=-100:10:100;
y=-100:10:100;
[X,Y]=meshgrid(x,y);
Z1=Y-((X+20).^2);
mesh(X,Y,Z1);
hold on;
Z2=Y-((X-20).^2);
mesh(X,Y,Z2);
0 Comments
Answers (1)
Star Strider
on 22 Nov 2014
This seems to work, but you may need to experiment with it to get the result you want:
x1=-100:10:0;
y1=-100:10:100;
x2= 0:10:100;
y2=-100:10:100;
[X1,Y1]=meshgrid(x1,y1);
[X2,Y2]=meshgrid(x2,y2);
figure(2)
Z1=Y1-((X1+20).^2);
mesh(X1,Y1,Z1); % Right
hold on;
Z2=Y2-((X2-20).^2); % Left
mesh(X2,Y2,Z2);
xlabel('X')
ylabel('Y')
0 Comments
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!