# [Matlab] 2D Sequence Plot in 3D

• Apr 23rd 2009, 02:50 AM
bkarpuz
[Matlab] 2D Sequence Plot in 3D
Dear Friends,

I am trying to plot a 2D sequence in 3D with the following code, but I dont know why it does not work.
Code:

```z=ones(30,30); for i=1:30     for j=1:30         z(i,j)=sin(i*j);     end end x=1:30; y=1:30; plot3(x,y,z); grid on; box on; xlabel('x'); ylabel('y'); zlabel('z');```
My main idea is to put points at \$\displaystyle (x,y,sin(x*y))\$ when \$\displaystyle x\$ and \$\displaystyle y\$ travels through the integers from \$\displaystyle 1\$ to \$\displaystyle 30\$.

Thanks for the help.
• Apr 23rd 2009, 04:11 AM
CaptainBlack
Quote:

Originally Posted by bkarpuz
Dear Friends,

I am trying to plot a 2D sequence in 3D with the following code, but I dont know why it does not work.
Code:

```z=ones(30,30); for i=1:30     for j=1:30         z(i,j)=sin(i*j);     end end x=1:30; y=1:30; plot3(x,y,z); grid on; box on; xlabel('x'); ylabel('y'); zlabel('z');```
My main idea is to put points at \$\displaystyle (x,y,sin(x*y))\$ when \$\displaystyle x\$ and \$\displaystyle y\$ travels through the integers from \$\displaystyle 1\$ to \$\displaystyle 30\$.

Thanks for the help.

Try:

Code:

```x=1:30; y=1:30;   [X,Y]=meshgrid(x,y) Z=sin(X.*Y);   plot3(X,Y,Z);```
• Apr 23rd 2009, 04:28 AM
bkarpuz
Quote:

Originally Posted by CaptainBlack
Try:

Code:

```x=1:30; y=1:30;   [X,Y]=meshgrid(x,y) Z=sin(X.*Y);   plot3(X,Y,Z);```

Ty CB but I have a double sequence (I need to calculate it by iteration).
I could write the following code and it works but I wonder if I can show it as a surface or not (something like meshing but negative parts yellow and positive parts red)?
Code:

```z=ones(30,30); for x=2:30     for y=2:30         z(x,y)=power(-1,y)*z(x-1,y)+2*power(-1,x)*z(x,y-1);     end; end; for x=1:30     for y=1:30         if z(x,y)>0             plot3(x,y,z(x,y),'r*');         else             plot3(x,y,z(x,y),'y*');         end         grid on;hold on;     end; end; box on; view(0,90); xlabel('x'); ylabel('y'); zlabel('z');```
Thanks again.