MATLAB ASSIGNMENT 4

Name: Foster, Antony (XXXX)
Course: Math 20300 XX
Instructor: Prof A Foster
Due Date: June XX, XXXX
MATLAB ASSIGNMENT 4
Purpose: Using Matlab to create 3-D plots of Space curves i.e., vector functions
of the form
and Surfaces i.e., functions
of the form
.
New Commands:
F = @(x,y)vectorized expression
plot3(x(t),y(t),z(t))
surf(x,y,f(x,y))
colormap Gray/copper
[x,y]= meshgrid(u,v)
zlabel('String',other options)
axis square/equal
axis([a, b, c, d])
rotate3d on/off
grid on/off
Exercise 4.1 Let
over the closed interval
be the space curve whose parameter ranges
.
A) Plot this space curve using the ezplot3 command (We will use plot3 instead)
Use the 'animate' option to view how the curve is traversed as the over the parameter interval? What is
the smallest parameter interval for which the curve will be traversed exactly once?
B) The following script M-file is supposed to display the parametric plot in part A) viewed from (1,1,1)
and then display three views of the same plot showing the projection into the various coordinate planes.
Complete the missing arguments in the code and place titles that state the projection that is being
viewed on that screen.
Note: The pause command halts execution of the script M-file until the user presses a key. You
may print a screen while the program is paused.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
The M-File to be Modified
%
%
syms t
%
x=cos(t);y=cos(2*t); z=cos(3*t);
%
clf
%
ezplot3(x,y,z,[0, 2*pi]);
%
view([1 1 1]);
%
title(???)
%
pause
%
view(???);
%
title(???)
%
pause
%
view([1 0 0])
%
title(???)
%
pause
%
view([0 1 0])
%
title(???)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
C) Print your script M-file and the four screens that it generates. Make sure to include your name. You
can also publish this to HTML. In doing so, you should first remove the pause commands (which
cause the publishing program to "hang."). Then, insert cell dividers after each view command. Put
appropriate titles in each cell divider line. The various graphs should now print following the commands
that generated them.
D) BONUS!! This is not a Matlab problem. Find an equation of the form
for the projection of
the curve into the -plane. Find an equation of the form
for the projection of the curve into
the -plane. Can the projection into the
-plane be described as the graph of a function
?
Justify your answer by referring to the figure you obtained in part B).
MATLAB CODE
clear all
clf reset
t=2*pi*[0:.01:1]; %This is just an alternate to t = linspace(0,2*pi,100).
x=@(t)cos(t);
y=@(t)cos(2*t);
z=@(t)cos(3*t);
figure(1);
spaceCurve = plot3(x(t),y(t),z(t));
set(spaceCurve,'Color','green','linewidth',3);
set(gca,'XTick',[-1:0.5:1],'XMinorTick','on','XTickLabel',
{'-1','-0.5','0','0.5','1'},'FontName','times','FontWeight','bold')
set(gca,'YTick',[-1:0.5:1],'YMinorTick','on','YTickLabel',
{'-1','-0.5','0','0.5','1'},'FontName','times','FontWeight','bold')
set(gca,'ZTick',[-1:0.5:1],'ZMinorTick','on','ZTickLabel',
{'-1','-0.5','0','0.5','1'},'FontName','times','FontWeight','bold')
title({'
','
','A view of the space curve','r(t) = < cos t, cos 2t, cos 3t >
for',
't in [0, 2pi] from the first octant by Antony Foster'})
xlabel('X-Axis','Color','red','FontName','mathematica','FontWeight','bold',
'FontSize',12)
ylabel('Y-Axis','Color','red','FontName','mathematica','FontWeight','bold',
'FontSize',12)
zlabel('Z-Axis','Color','red','FontName','mathematica','FontWeight','bold',
'FontSize',12)
axis equal square
axis([-1 1 -1 1 -1 1])
view([129, 26])
grid on
%%---------------------------------------------------------------------------------
MATLAB OUTPUT
A view of the space curve
r(t) = < cos t, cos 2t, cos 3t > for
t in [0, 2pi] from the first octant by Antony Foster
1
Z-Axis
0.5
0
-0.5
-1
-1
-1
-0.5
0
0
0.5
Y-Axis
-0.5
0.5
1
1
X-Axis
figure(2)
set(spaceCurve,'Color','blue','linewidth',3);
set(gca,'XTick',[-1:0.5:1],'XMinorTick','on','XTickLabel',
{'-1','-0.5','0','0.5','1'},'FontName','times','FontWeight','bold')
set(gca,'YTick',[-1:0.5:1],'YMinorTick','on','YTickLabel',
{'-1','-0.5','0','0.5','1'},'FontName','times','FontWeight','bold')
set(gca,'ZTick',[-1:0.5:1],'ZMinorTick','on','ZTickLabel',{'-1','0.5','0','0.5','1'},'FontName','times','FontWeight','bold')
title({' ','The Projection of the space curve','r(t) = < cos t, cos 2t, cos 3t >
for','t in [0, 2pi] onto the xy-plane by Antony Foster'})
xlabel('X-Axis','Color','red','FontName','mathematica','FontWeight','bold',
'FontSize',12)
ylabel('Y-Axis','Color','red','FontName','mathematica','FontWeight','bold',
'FontSize',12)
zlabel('Z-Axis','Color','red','FontName','mathematica','FontWeight','bold',
'FontSize',12)
axis equal square
axis([-1 1 -1 1 -1 1])
view([0 0 1])
grid on
%%---------------------------------------------------------------------------------
MATLAB OUTPUT
1
The Projection of the space curve
r(t) = < cos t, cos 2t, cos 3t > for
t in [0, 2pi] onto the xy-plane by Antony Foster
Y-Axis
0.5
0
-0.5
-1
-1
-0.5
0
X-Axis
0.5
1
figure(3)
spaceCurve = plot3(x(t),y(t),z(t));
set(spaceCurve,'Color','red','linewidth',3);
set(gca,'XTick',[-1:0.5:1],'XMinorTick','on','XTickLabel',
{'-1','-0.5','0','0.5','1'},'FontName','times','FontWeight','bold')
set(gca,'YTick',[-1:0.5:1],'YMinorTick','on','YTickLabel',
{'-1','-0.5','0','0.5','1'},'FontName','times','FontWeight','bold')
set(gca,'ZTick',[-1:0.5:1],'ZMinorTick','on','ZTickLabel',
{'-1','-0.5','0','0.5','1'},'FontName','times','FontWeight','bold')
title({' ',' ','The Projection of the space curve','r(t) = < cos t, cos 2t, cos 3t
> for','t in [0, 2pi] onto the xz-plane by Antony Foster'})
xlabel('X-Axis','Color','red','FontName','mathematica','FontWeight','bold',
'FontSize',12)
ylabel('Y-Axis','Color','red','FontName','mathematica','FontWeight','bold',
'FontSize',12)
zlabel('Z-Axis','Color','red','FontName','mathematica','FontWeight','bold',
'FontSize',12)
axis equal square
axis([-1 1 -1 1 -1 1])
view([0 1 0])
grid on
%%---------------------------------------------------------------------------------
MATLAB OUTPUT
The Projection of the space curve
r(t) = < cos t, cos 2t, cos 3t > for
t in [0, 2pi] onto the xz-plane by Antony Foster
1
Z-Axis
0.5
0
-0.5
-1
1
0.5
0
X-Axis
-0.5
-1
figure(4)
spaceCurve=plot3(x(t),y(t),z(t));
set(spaceCurve,'Color','black','linewidth',3);
set(gca,'XTick',[-1:0.5:1],'XMinorTick','on','XTickLabel',
{'-1','-0.5','0','0.5','1'},'FontName','times','FontWeight','bold')
set(gca,'YTick',[-1:0.5:1],'YMinorTick','on','YTickLabel',
{'-1','-0.5','0','0.5','1'},'FontName','times','FontWeight','bold')
set(gca,'ZTick',[-1:0.5:1],'ZMinorTick','on','ZTickLabel',
{'-1','-0.5','0','0.5','1'},'FontName','times','FontWeight','bold')
title({' ',' ','The Projection of the space curve','r(t) = < cos t, cos 2t, cos 3t
> for','t in [0, 2pi] onto the yz-plane'})
xlabel('X-Axis','Color','red','FontName','mathematica','FontWeight','bold',
'FontSize',12)
ylabel('Y-Axis','Color','red','FontName','mathematica','FontWeight','bold',
'FontSize',12)
zlabel('Z-Axis','Color','red','FontName','mathematica','FontWeight','bold',
'FontSize',12)
axis equal square
axis([-1 1 -1 1 -1 1])
view([1 0 0])
grid on
%%---------------------------------------------------------------------------------
MATLAB OUTPUT
1
The Projection of the space curve
r(t) = < cos t, cos 2t, cos 3t > for
t in [0, 2pi] onto the yz-plane
Z-Axis
0.5
0
-0.5
-1
-1
-0.5
0
0.5
1
Y-Axis
Answer to the Bonus Question D
Yes, we can see that the graph in figure(2) determines as a function of
(This the projection of
onto the -plane)
Yes, the graph in figure(3) determines as a function of
the projection of
onto the -plane)
as follows
as
No There is no function
that we could use represent the projection
of
onto the -plane (The graph in figure(4)). This is because the projection fails the
vertical line test; i.e., such a graph cannot represent a function explicitly in terms of .
(This
Exercise 4.2 Using the plot3 command, write a script M-file that draws
A) The helix with parametric equations
, for
B) On the same graph, but in a different color, linestyle, and linewidth, draws a portion of the tangent
line to the helix at the point
. The portion of the tangent line should be chosen to extend as
far as possible within the first octant. Include labels on the coordinate axes, and a grid. Publish to
HTML. Make sure your name appears in the printout (you can include it in a comment line, for
example), and the combined graphs use a view that clearly shows the tangent line.
MATLAB CODE
clear all
clc
s=linspace(-0.75,.75,50);
t=linspace(0,2*pi,80);
x=@(t)cos(t);
y=@(t)sin(t);
z=@(t)t;
u=@(s)sqrt(2)/2-(sqrt(2)/2)*s;
v=@(s)sqrt(2)/2+(sqrt(2)/2)*s;
w=@(s)(pi/4)+s;
figure(5)
Helix=plot3(x(t),y(t),z(t));
hold on
TangentLine=plot3(u(s),v(s),w(s));
hold on
TangencyPoint=plot3(sqrt(2)/2,sqrt(2)/2,pi/4);
set(Helix,'Color','blue','linewidth',2);
set(TangentLine,'Color','red','linewidth',3,'linestyle',':');
set(TangencyPoint,'Color','blue','linewidth',4,'linestyle','o');
set(gca,'XTick',[-1:0.5:1],'XMinorTick','on','XTickLabel',
{'-1','-0.5','0','0.5','1'},'FontName','times','FontWeight','bold')
set(gca,'YTick',[-1:0.5:1],'YMinorTick','on','YTickLabel',
{'-1','-0.5','0','0.5','1'},'FontName','times','FontWeight','bold')
set(gca,'ZTick',[-1:1:2*pi],'ZMinorTick','on','ZTickLabel',
{'-1','0','1','2','3','4','5','6'},'FontName','times','FontWeight','bold')
title({'
','
','Circular Helix','r(t) = < cos t, sin t, t > for','t in [0,
2pi]','together with tangent line a r(pi/4)','from the first octant by Antony
Foster'})
xlabel('X-Axis','Color','red','FontName','mathematica','FontWeight','bold',
'FontSize',12)
ylabel('Y-Axis','Color','red','FontName','mathematica','FontWeight','bold',
'FontSize',12)
zlabel('Z-Axis','Color','red','FontName','mathematica','FontWeight','bold',
'FontSize',12)
axis equal square
axis([-1 1 -1 1 -1 2*pi])
view([1 1 1])
grid on
%%---------------------------------------------------------------------------------
MATLAB OUTPUT
Z-Axis
Circular Helix
r(t) = < cos t, sin t, t > for
t in [0, 2pi]
together with tangent line a r(pi/4)
from the first octant by Antony Foster
6
5
4
3
2
1
0
-1
-1
-0.5
-0.5
0
0.5
0.5
1 1
Y-Axis
-1
0
X-Axis
Exercise 4.3 Using ezsurf command (use the surf command instead ) make a plot of the graph of the
surface
over the closed rectangular region
.
MATLAB CODE
clear all
clc
u=linspace(-2.5,0.6,30);
v=linspace(-3,3,30);
f=@(x,y)2*x.^3+ x.*y.^2+5*x.^2+y.^2;
[x,y] = meshgrid(u,v);
figure(6)
surf(x,y,f(x,y))
colormap Gray
set(gca,'XTick',[-2.5:3.1/7:0.6],'XMinorTick','on','XTickLabel',
{'-2.5', '-2.1','-1.6','-1.2','-0.7','-0.3','0.3','0.6'},'FontName','times',
'FontWeight','bold')
set(gca,'YTick',[-3:1:3],'YMinorTick','on','YTickLabel',
{'-3','-2','-1','0','1','2','3'},'FontName','times','FontWeight','bold')
set(gca,'ZTick',[-15:5:17],'ZMinorTick','on','FontName','times','FontWeight',
'bold')
title({'
','
','Graph of the surface z = f(x,y) = 2x^3 + xy^2+ 5x^2 + y^2
','viewed from the first octant by Antony Foster'})
xlabel('X-Axis','Color','red','FontName','mathematica','FontWeight','bold',
'FontSize',12)
ylabel('Y-Axis','Color','red','FontName','mathematica','FontWeight','bold',
'FontSize',12)
zlabel('Z-Axis','Color','red','FontName','mathematica','FontWeight','bold',
'FontSize',12)
axis square
axis([-2.5 0.6 -3 3 min(min(f(x,y))), max(max(f(x,y)))])
view([1,1,1])
rotate3d on
grid on
%%---------------------------------------------------------------------------------
MATLAB OUTPUT
Graph of the surface z = f(x,y) = 2x 3 + xy2+ 5x2 + y2
viewed from the first octant by Antony Foster
15
Z-Axis
10
5
0
-5
-10
-3
-2
-1
0
1
2
3
Y-Axis
0.6
0.3
-0.3
-0.7
-1.2
-1.6
X-Axis
-2.1
-2.5
Exercise 4.4 Using the surf command make a plot of the graph of
the closed rectangular region
Use a
over
grid.
MATLAB CODE
clear all
clc
u=linspace(-2,2,25);
v=u;
[x,y]=meshgrid(u,v);
f=@(x,y)(x.^2-y.^2).^2;
figure(7)
surf(x,y,f(x,y))
colormap Gray
set(gca,'XTick',[-2:1:2],'XMinorTick','on','FontName','times','FontWeight','bold')
set(gca,'YTick',[-2:1:2],'YMinorTick','on','FontName','times','FontWeight','bold')
set(gca,'ZTick',[0:4:16],'ZMinorTick','on','FontName','times','FontWeight','bold')
title({'
','
','Graph of the surface z = f(x,y) = (x^2 - y^2)^2 ','viewed
from the first octant by Antony Foster'})
xlabel('X-Axis','Color','red','FontName','mathematica','FontWeight','bold',
'FontSize',12)
ylabel('Y-Axis','Color','red','FontName','mathematica','FontWeight','bold',
'FontSize',12)
zlabel('Z-Axis','Color','red','FontName','mathematica','FontWeight','bold',
'FontSize',12)
axis square
axis([-2 2 -2 2 min(min(f(x,y))) max(max(f(x,y)))])
view([102,54])
grid on
rotate3d on
%%---------------------------------------------------------------------------------
MATLAB OUTPUT
Graph of the surface z = f(x,y) = (x 2 - y2)2
viewed from the first octant by Antony Foster
Z-Axis
16
-2
12
-1
8
0
4
0
-2
1
-1
0
Y-Axis
1
2
2
X-Axis
Exercise 4.5 Write a script M-file to plot the parabolic cylinder
using the surf
command. You will need to treat and as independent variables, using a meshgrid in the -plane
and generating the values using the formula. Use a
grid of the rectangle
Locate the viewpoint at
add labels to each axis, and put a title on the graph that includes the
formula for the surface. Publish to HTML. (Be sure to include your name in a comment).
MATLAB CODE
u=linspace(-2,2,30);
v=u;
[x,z]=meshgrid(u,v);
f=@(x,z)x.^2;
figure(8)
surf(x,f(x,z),z)
colormap Gray
set(gca,'XTick',[-2:1:2],'XMinorTick','on','FontName','times','FontWeight','bold')
set(gca,'YTick',[0:1:4],'YMinorTick','on','FontName','times','FontWeight','bold')
set(gca,'ZTick',[-2:1:2],'ZMinorTick','on','FontName','times','FontWeight','bold')
title({'
','
','Graph of the Parabolic cylinder y = x^2 ','viewed from the
first octant by Antony Foster'})
xlabel('X-Axis','Color','red','FontName','mathematica','FontWeight','bold',
'FontSize',12)
ylabel('Y-Axis','Color','red','FontName','mathematica','FontWeight','bold',
'FontSize',12)
zlabel('Z-Axis','Color','red','FontName','mathematica','FontWeight','bold',
'FontSize',12)
axis square
axis([-2 2 min(min(f(x,z))) max(max(f(x,z))) -2 2]);
view([1,1,1])
rotate3d on
grid on
MATLAB OUTPUT
Graph of the Parabolic cylinder y = x 2
viewed from the first octant by Antony Foster
2
Z-Axis
1
0
-1
-2
-2
1
-1
2
0
3
1
4 2
Y-Axis
X-Axis