# Math Help - Matlab mesh problem

1. ## Matlab mesh problem

Hi,
I'm having a problem with the mesh & surf commands in Matlab. I am trying to plot a 3D contour, Z, in a limited area.

Using plot3 as follows works fine:

Code:

for theta1 = -0.5:0.05:2.5
for theta2 = -0.5:0.05:2.5

Z = ((0.1150 * sin(theta1) * cos(theta2) * 9.81 + 0.1150 * cos(theta1) * sin(theta2) * 9.81 + 0.3990 * sin(theta1) * g) ^ 2 / 20) + (0.00013)

plot3(theta1,theta2,Z);

end
end
But as mesh is clearer I'd prefer to use that.
However when I try following the Matlab example my plot seems to ignore one of my input arguments producing a Z matrix where all the rows are identical? I've spent quite a bit of time on this and just can't get the bottom of it. I'd realy appreciate any suggestions...

Here is my code:
Code:

[theta1,theta2] = meshgrid(-0.5:0.05:2.5);
Z = ((0.1150 * sin(theta1) * cos(theta2) * 9.81 + 0.1150 * cos(theta1) * sin(theta2) * 9.81 + 0.3990 * sin(theta1) * g) ^ 2 / 20) + (0.00013)
mesh(theta1,theta2,Z);
Strangely ezmesh seems to work fine but I'd like to know why mesh doesn't like my equation (which plot3 does like).

Many Thanks,

Ad

2. ## Sort of a solution

Just as a follow up for anyone else who is having this problem...

This method gives the result I wanted but I still don't know why my first method didn't work?

Code:

t1 = -0.5:0.1:3;
t2 = -0.5:0.1:3;

Z = zeros(length(t1)); % create a matrix of n*n dimension

for(i = 1:length(Z2))
for(j = 1:length(Z2))
th1 = t1(i);
th2 = t2(j);

Z(i,j) = (my equation);

end
end

mesh(t2,t1,Z)

Ad

3. Originally Posted by ADNL
Hi,
I'm having a problem with the mesh & surf commands in Matlab. I am trying to plot a 3D contour, Z, in a limited area.

Using plot3 as follows works fine:

Code:

for theta1 = -0.5:0.05:2.5
for theta2 = -0.5:0.05:2.5

Z = ((0.1150 * sin(theta1) * cos(theta2) * 9.81 + 0.1150 * cos(theta1) * sin(theta2) * 9.81 + 0.3990 * sin(theta1) * g) ^ 2 / 20) + (0.00013)

plot3(theta1,theta2,Z);

end
end
But as mesh is clearer I'd prefer to use that.
However when I try following the Matlab example my plot seems to ignore one of my input arguments producing a Z matrix where all the rows are identical? I've spent quite a bit of time on this and just can't get the bottom of it. I'd realy appreciate any suggestions...

Here is my code:
Code:

[theta1,theta2] = meshgrid(-0.5:0.05:2.5);
Z = ((0.1150 * sin(theta1) * cos(theta2) * 9.81 + 0.1150 * cos(theta1) * sin(theta2) * 9.81 + 0.3990 * sin(theta1) * g) ^ 2 / 20) + (0.00013)
mesh(theta1,theta2,Z);
Strangely ezmesh seems to work fine but I'd like to know why mesh doesn't like my equation (which plot3 does like).

Many Thanks,

Ad

Check the sizes of theta1, theta2 and Z in all cases.

CB

4. For your first method you never defined g.

g=9.81?

Also, sin(theta1) and sin(theta2) have the same dimension of 1x60 so they can't be multiplied together. You're going to have to do sin(theta1)*sin(theta2)' to make the matrix dimensions agree.