21 February, 2013

PLOTTING 3D GRAPHS USING MATLAB

In continuation to my post PLOTTING 2-D GRAPHS USING MATLAB which explained plotting various 2-D graphs and defining graph, figure, colour and background properties etcetera, my current post explains plotting 3-D graphs and related functions.

To start with, the function used here is plot3( ) for plotting 3-D graphs. We will plot a sphere and before it let us see the code to plot a circle centred at (2,2) of radius 2.


x=0:0.01:4;
y=((4-(x-2).^2).^0.5)+2; % semicircle for y>0
plot(x,y)
hold on
y=-((4-(x-2).^2).^0.5)+2; % semicircle for y<0
plot(x,y)



Keeping the above example in mind let us plot a sphere centred at (2,2,0) of radius 2.


for i=-20:20      % for plotting 40 circles.. u can increase or
                  % decrease
   z=i/10;        % z can obtain the values from -2 to +2 in a
                  % sphere with centre (2,2,0) and radius 2
   r=(4-z*z)^0.5; % r is radius of circle in xy plane, equation
                  %of a sphere is x^2+y^2+z^2=2^2 and x^2+y^2=r^2
   x=2-r:0.01:2+r;% as 2,2 is the centre of xy plane 
   a=x./x;        % a converted to a vector of same size as x
   z=z*a;
   y=((r^2-(x-2).^2).^0.5)+2; % circle eqn
   plot3(x,y,z) 
   hold on
   y=-((r^2-(x-2).^2).^0.5)+2;% circle eqn
   plot3(x,y,z)
end



In order to study the 3-D plot from every angle, MATLAB provides "Rotate 3D" feature which is demonstrated by rotating the above plotted sphere to an angle.

There are some more codes by which a sphere can be drawn. Let us plot a sphere of radius 2 by using polar coordinates.

r=2;          % radius 2
o=0:0.1:2*pi; % theta
for i=0:31.4
  h=i/10;     % phi
  x=r*sin(o)*cos(h);
  y=r*sin(o)*sin(h);
  z=r*cos(o);
  plot3(x,y,z);
  hold on
end



One of the function available in MATLAB is meshgrid( ). Let us see how we can plot a 3-D plot with its help.
like for e.g.
a= [1 2 3];
b=[6 7 8];
and we want to have elements (1,6) , (1,7), (1,8), (2,6), (2,7), (2,8), (3,6),(3,7), (3,8)
then [X,Y]=meshgrid(a,b) solves our problem
as it creates X= 1 2 3    Y= 6 6 6 
                         1 2 3          7 7 7
                         1 2 3          8 8 8 

Now see the following code to generate the sphere via meshgrid( )


a=-1:0.05:1;          % an array
b=-1:0.05:1;          % another array
[x,y]=meshgrid(a,b);  % generates all the possible coordinate          
                      %range from (-1,-1) to (1,1)
z=(1-(x.^2+y.^2)).^0.5; % equation of sphere with radius 1 and           
                        %origin as centre
for i=1:41
  for j=1:41  
    if imag(z(i,j))==0   % check to plot only real values that 
                         %satisfy the eqn of sphere as they are 
                         %the true values
        plot3(x(i,j),y(i,j),z(i,j)) % plotting individual points
        hold on
    end
  end
end

hold on
z=-((1-(x.^2+y.^2)).^0.5);
for i=1:41
  for j=1:41  
    if imag(z(i,j))==0
        plot3(x(i,j),y(i,j),z(i,j))
        hold on
    end
  end
end



Using the meshgrid function and polar coordinates we have the following code for a sphere.


r=2;                    % radius =2
phi=linspace(0,pi,50);  % phi=0 to pi divided in 50 slots
theta=linspace(0,2*pi,50);
[phi,theta]=meshgrid(phi,theta);

x=r*sin(phi).*cos(theta);
y=r*sin(phi).*sin(theta);
z=r*cos(phi);
plot3(x,y,z)


Now let us plot some other figures.Following is the code to plot a cone

o=pi/4;                % apex angle/2
l=5;
for h=0:0.01:l*cos(o); % height from min to max
r=(l*cos(o)-h)*tan(o); % radius of the circle to pe plot in x,y plane
x=-r:0.01:r;          % range of x
z=(x./x)*h;           % z converted to a vector of same size as x
y=(r^2-x.^2).^0.5;       
plot3(x,y,z);
hold on
w=-(r^2-x.^2).^0.5;
plot3(x,w,z);
hold on
end



Now let us plot a cube with 4 sides of side length 2 :-

x=0:0.01:2;
y=(x./x)*0;
w=(x./x)*2;
for a=0:0.01:2
    z=(x./x)*a;
    plot3(x,y,z) % xz plane plotted at y=0
    hold on
    plot3(x,w,z) % xz plane plotted at y=2
    hold on
    plot3(w,x,z) % yz plane plotted at x=2
    hold on
    plot3(y,x,z) % yz plane plotted at x=0
    hold on
end
    

Now let us plot a line with direction cosines(2,1,1) and passing through (1,2,3)

k=-5:0.01:5;
x=2*k+1;
y=k+2;
z=k+3;
plot3(x,y,z)



Now let us plot a plane with equation 2x + 3y + 5z =10

a=0:0.1:5;
b=0:0.1:5;
[x,y]=meshgrid(a,b);
z=(10-(2*x+3*y))./5;
plot3(x,y,z,'b')




Now let us plot a cylinder of radius 2 and height 4.
r=2;
h=0:0.1:4;
theta=0:0.1:2*pi; 
[H,THETA]=meshgrid(h,theta);
R=H./H.*r;
x=R.*cos(THETA);
y=R.*sin(THETA);
z=H;
plot3(x,y,z,'b');



3-D BAR GRAPHS AND PIE CHARTS

We learned to plot 2-D bar graphs and pie charts in my previous post. Now taking the profits of two companies in the corresponding years we will study 3-D bar graphs and the function used to plot the same is bar3( ).

year1= [2005 2006 2007 2008 2009 2010];
profit1=[500.43     600.76
         562.62     970.45
         357.81     1100.3
         597.39     708.7
         624.61     300.34    %first column company A
         731.98     900.32 ]; %second column company B 
bar3(year1,profit1,0.6);% default detached
title('detached');



Keeping all the rest statements same and changing the last two, in the above program, let us see some variations.The first is grouped and the other is stacked.

bar3(year1,profit1,'grouped');
title('grouped');




bar3(year1,profit1,'stacked');
title('stacked');


Let us now have a look at 3-D pie charts and the function used for it is pie3. Suppose we have 52 engineering, 24 medical ,16 art, 20 pharma and 80 maths students.
a=[52 24 16 20 80];
pie3(a);



Let us see a variation in the above code.
a=[52 24 16 20 80];
b=[1 0 0 1 0];
pie3(a,b);


This ends the basics of graph plotting in MATLAB.