Plot a function in 3D that can be either positive or negative?

3 views (last 30 days)
Hello, I have to plot a 3D function that is described by: y= +- (B/2)[1-(2*x/L)^2][1-(2*z/T)^2].
I have B=1.2, L=4, T=0.5, -2<=x<=2 and -0.5<=z<=2.
I know I have to make two matrices from x and z using meshgrid and then get the value of matrix y from the corresponding equation so I substituted the values of B,L and T in the equation and simplified it. Here is my code:
x=-2:2;
z=-0.5:0.5:2;
[X,Z]=meshgrid(x,z)
Y=(2.4.*X.^2.*Z.^2 - 9.6.*Z.^2 - 0.15.*X.^2 + 0.6);
mesh (X,Y,Z)
grid off
xlabel('x'); ylabel('y'); zlabel('z');
It runs and I see a figure close to the real shape but not the real shape because the function has +- at the beginning of it (+ or -) and I don't know how to write this in Matlab. I uploaded the figure it should looks like and my figure for comparison, any help will be much appreciated. Thanks in advance

Accepted Answer

Mischa Kim
Mischa Kim on 18 Apr 2014
Edited: Mischa Kim on 18 Apr 2014
Mahmoud, a quick fix would be to add
hold on
Y = -(2.4.*X.^2.*Z.^2 - 9.6.*Z.^2 - 0.15.*X.^2 + 0.6);
mesh (X,Y,Z)
As an alternative, you could - it looks like - solve for z(x,y).

More Answers (0)

Community Treasure Hunt

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

Start Hunting!