Tuesday, March 21, 2017

                                                    Chapter-8
                                       THREE-DIMENSIONAL PLOTS
A three-dimensional line plot is a line that is obtained by connecting points in a three-dimensional space. A basic 3-D plot is created by plot3 command. Syntax for using plot3 command:


Line color specifier is an optional property that can be used to define the color of line. The line color specifiers are following:

Line color
Specifier
Red
Green
Blue
Cyan
Magenta
Blue
Black
yellow
r
g
b
c
m
b
k
y

LineWidth is an optional property that specifies the width of line. Default value of LineWidth is 0.5 and we can specify its value in the command. The three vectors x, y and z must have the same number of elements.
If x, y and z are given as function of parameter t:

x = (2 + 4*cos(t)).*cos(t);
y = (2 + 4*cos(t)).*sin(t);
z = t.^2;

The xlabel and ylabel commands:
Labels can be placed next to the axis with the xlabel and ylabel commands which have the form:
xlabel('text as string');
ylabel('text as string');
zlabel('text as string');
The grid command:
grid on     Add grid lines to the plot
grid off     Removes grid lines from the plot
A plot of points for t = 0:0.01:20 can be produced by following script file:

%3D graph
%use of plot3 command to plot the graph
clc
clear all
t = 0:0.01:20;
x = (2 + 4*cos(t)).*cos(t);
y = (2 + 4*cos(t)).*sin(t);
z = t.^2;
plot3(x,y,z,'k','linewidth',1)
grid on%add grid lines to the plot
xlabel('x');
ylabel('y');
zlabel('z');

*link for downloading program threedgraph.m from folder 3Dgraph


By running following script file in MATLAB we will get idea on 3-D plotting in MATLAB using plot3 command.
MESH AND SURFACE PLOTS:
Mesh and surface plots are three-dimensional plots that are used for plotting functions of form z = f (x, y) where the x and y are the independent variables and z is the dependent variable.
Mesh and surface plots are created in three steps:
  • ·         Divide the domain of independent variables x and y.
  • ·         form the matrices of independent variables.
  • ·         Calculate the value of dependent variable z at each point in domain.
  • ·         Create the plot.

MATLAB has in-built command called meshgrid, that can be used for creating X and Y matrices. Syntax for using meshgrid command:



Fig: 8.1 shows how to create the matrices of independent variables
>> x = -1:4;
>> y = 1:5;
>> [X,Y] = meshgrid(x,y)
X =

    -1     0     1     2     3     4
    -1     0     1     2     3     4
    -1     0     1     2     3     4
    -1     0     1     2     3     4
    -1     0     1     2     3     4
Y =

     1     1     1     1     1     1
     2     2     2     2     2     2
     3     3     3     3     3     3
     4     4     4     4     4     4
     5     5     5     5     5     5

The value of z at each point can be created by using element-by-element calculations in the same way it is used with vectors.
Mesh or surface plot is created with mesh or surf commands, which have the form:
surf(X,Y,Z)                    mesh(X,Y,Z)
Where X and Y are matrices of independent variable and Z is a matrix with value at each point in the domain of x and y.
As an example, the following script file contains a complete program that plots the function:


%use of mesh command and use of surf command
x = -3:0.25:3;
y = -3:0.25:3;
[X,Y] = meshgrid(x,y);
Z = 1.8.^(-1.5*sqrt(X.^2+Y.^2)).*sin(X).*cos(0.5*Y);
subplot(1,2,1),surf(X,Y,Z)
subplot(1,2,2),mesh(X,Y,Z)
%by watching the output we can clearly saw the difference between surf and
%mesh command and subplot command is used for plotting two graph on same %sheet
xlabel('x')
ylabel('y')
zlabel('z')


*link for downloading program: meshcommand.m from folder 3Dgraph
OTHER COMMANDS USED FOR THREE DIMENSIONAL PLOTTING:
meshz and meshc command: meshz command draws curtain around the mesh and meshc commands draws a contour plot beneath the mesh.



Q) plot the function
over the domain -3<x<3 and -3<y<3 using meshz and meshc command and observe the difference between two commands?



%use of meshz command and use of meshc command
x = -3:0.25:3;
y = -3:0.25:3;
[X,Y] = meshgrid(x,y);
Z = 1.8.^(-1.5*sqrt(X.^2+Y.^2)).*sin(X).*cos(0.5*Y);
subplot(1,2,1),meshc(X,Y,Z)%draws a contour plot beneath the mesh
subplot(1,2,2),meshz(X,Y,Z)%draws a curtain around the mesh
%by watching the output we can clearly saw the difference between meshz and
%meshc command
xlabel('x')
ylabel('y')
zlabel('z')


*link for downloading program meshzandmeshc.m


surfc and surfl command: surfc command draws a contour plot beneath the surface and surfl command draws surface plot with lighting






Q)  plot the function

over the domain -3<x<3 and -3<y<3 using surfc and surfl command and observe the difference between two commands?



%comparison between surf and surfc command
x = -3:0.25:3;
y = -3:0.25:3;
[X,Y] = meshgrid(x,y);
Z = 1.8.^(-1.5*sqrt(X.^2+Y.^2)).*sin(X).*cos(0.5*Y);
subplot(1,2,1),surfc(X,Y,Z)%draws a contour plot beneath the surface
subplot(1,2,2),surfl(X,Y,Z)
%by watching the output we can clearly saw the difference between surfc and
%surf command
xlabel('x')
ylabel('y')
zlabel('z')
 *link for downloading program surflandsurfc.m from folder 3Dgraph


The view command:

The view command controls the direction from which the plot is viewed. This is done by specifying the direction in terms of azimuthal and elevation angles or by defining points in space from which the plot is viewed. view command has syntax:
view(azimuthal angle,elevation angle) or view([azimuthal angle,elevation angle])
  • ·         Azimuthal angle in the x-y plane is measured relative to the negative y axis direction and defined as positive in counter-clockwise direction.
  • ·         Elevation angle is measured from the x-y plane. A positive value corresponds to opening an angle in the direction of z axis.
  • ·         The default azimuthal angle = -37.5 and default elevation angle = 30.

By choosing appropriate azimuthal and elevation angle the view command can be used to plot projections of 3-D plots on various planes according to following table:
Projection plane
az value
el value
x-y(top view)
0
90
x-z(side view)
0
0
y-z(side view)
90
0

Q) Write three different programs to Draw the projection of function 

 on x-y plane, y-z plane and x-z plane?
First program: projection on x-y plane


clc
clear all
close all
x = -3:0.25:3;
y = -3:0.25:3;
[X,Y] = meshgrid(x,y);
Z = 1.8.^(-1.5*sqrt(X.^2+Y.^2)).*sin(X).*cos(0.5*Y);
surf(X,Y,Z)
view(0,90)
 *link for downloading program projectiononxy.m from folder 3D graph




Second program: projection on y-z plane
%projection on yz plane of function by using view command
clc
clear all
close all
x = -3:0.25:3;
y = -3:0.25:3;
[X,Y] = meshgrid(x,y);
Z = 1.8.^(-1.5*sqrt(X.^2+Y.^2)).*sin(X).*cos(0.5*Y);
surf(X,Y,Z)
view(90,0)

*link for downloading program projectiononyz.m from folder 3D graph




Third program: projection on x-z plane
%projection on xz plane of function by using view command
clc
clear all
close all
x = -3:0.25:3;
y = -3:0.25:3;
[X,Y] = meshgrid(x,y);
Z = 1.8.^(-1.5*sqrt(X.^2+Y.^2)).*sin(X).*cos(0.5*Y);
surf(X,Y,Z)
view(0,0)

*link for downloading program projectiononxzplane.m from folder 3D graph



Q) four charges of magnitude 2×10-10 C are placed at point (0.25,0,0) (-0.25,0,0) (0,0.25,0) and                    (0,-0.25,0). plot the electrical potential due to the particles at points in x-y plane that are located in domain -0.5<x<0.5 -0.5<y<0.5. Make the plot such that the x-y plane is the plane of the points and the
z-axis is the magnitude of electrical potential?


clc
clear all
eps0 = 8.85e-12;
q1 = 2e-10;
q2 = 2e-10;
q3 = 2e-10;
q4 = 2e-10;
k = 1/(4*pi*eps0);
x = -0.5:0.01:0.5;
y = -0.5:0.01:0.5;
[X,Y] = meshgrid(x,y);
r1 = sqrt((X+0.25).^2 + Y.^2);
r2 = sqrt((X-0.25).^2 + Y.^2);
r3 = sqrt((Y+0.25).^2 + X.^2);
r4 = sqrt((Y-0.25).^2 + X.^2);
V = k*(q1./r1 + q2./r2 + q3./r3 + q4./r4);
mesh(X,Y,V)
xlabel('x');
ylabel('y');
zlabel('z');
  

*link for downloading program potential.m from folder 3Dgraph
























No comments:

Post a Comment