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

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

 채택된 답변

Mischa Kim
Mischa Kim 2014년 4월 18일
편집: Mischa Kim 2014년 4월 18일
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).

추가 답변 (0개)

카테고리

질문:

2014년 4월 18일

편집:

2014년 4월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by