MATLAB: How to write a program and plot a 3-D graph

for loopsum

There are 60 points in a plane. (15 points in the X-axis and 4 points in the Y-axis,respectively). The plane is divided into elements with various size. In the first element there are 20 points (X – from 1 to 5 and Y – form 1 to 4), the second – 16 points (X – from 6 to 9 and Y – form 1 to 4), the third (X – from 10 to 12 and Y – form 1 to 4), the fourth (X – from 13 to 14 and Y – form 1 to 4), the last ( X – 15 , Y from 1 to 4). I need to sum up all the points in each element and show the result in 3-D graph.
%To sum all the points in the first element
for i=1:5
for j=1:4
x(i)=i;
y(j)=j;
z(i,j)=x(i)+y(j);
end
end
%To sum all the points in the second element
for i=6:9
for j=1:4
x(i)=i;
y(j)=j;
z(i,j)=x(i)+y(j);
end
end
Finally, I need to show the results one element after another in 3-D plot. So, I have to write many for loops and I don't want it. Kindly help with your suggestions please. Sorry for my poor English.

Best Answer

x=1:5;y=1:4; % Example
[x1,y1]=meshgrid(x,y)
z=x1+y1
mesh(x1,y1,z)
%or
plot3(x1(:),y1(:),z(:))
Related Question