MATLAB ASSIGNMENT 2

Name: Foster, Antony
Course: Math 20300
Sony Vaio PCG-FRV35
Instructor: Prof A Foster
Due Date: June 11, 2008
[MATLAB ASSIGNMENT 2 ]
Purpose: Using Matlab to create more plots of single variable functions in the xyplane (introducing Script M-Files)
Commands Used:
linspace (a, b, n)
f = @(x,y,z,t,…)expression (This maybe symbolic/vectorized)
subs(expression)
plot (x,y,)
grid on/off
clear all
[a:b:c]
clc
clf reset
figure(k)
hold on/off
axis square equal
axis([a, b, c, d])
xlabel ('String', other options)
ylabel ('String', other options)
legend('String1','String2',…,'Location', 'options')
title('Your title','color','your color', 'FontName','Your Font','FontWeight','bold','FontSize',12)
set(handle, other options)
set(gca, other options)
Exercise 2.1 Write a script M-file that displays the graphs of
a) The ellipse with parametric equations,
.
b) The largest circle that can be inscribed in the ellipse above (Make sure the circle looks like a circle –
use the axis equal command.)
c) The ellipse should be drawn in a solid line style and the circle in a dotted style.
d) The graph should have a title that includes your name.
e) The graph should display a legend that shows which graph is the ellipse and which is the circle.
Publish your output to HTML.
THE CODE
clear all
clc
constants
a = 0;
b = 2*pi;
d = 2*pi;
r1 = 4;
r2 = 3;
% left endpoint of the parameter domain
% right endpoint of the parameter domain
% length of the major axis
% length of the minor axis
GENERATING THE DATA TO BE PLOTTED
t = linspace(0,2pi,80); % the domain of the parameter t.
DEFINITION OF THE ELLIPSE
x = @(t)r1.*cos(t);
y = @(t)r2.*sin(t);
% x coordinate as a function of t (the parameter)
% y coordinate as a function of t
DEFINITION OF THE REQUIRED CIRCLE
r = min([r1,r2]);
u = @(t)r.*cos(t);
v = @(t)r.*sin(t);
PLOTTING THE GENERATED DATA
clf reset
figure(1)
ELLIPSE = plot(x(t),y(t));
hold on
CIRCLE = plot(u(t),v(t));
hold off
DRESSING UP YOUR PLOT
title({' ','LARGEST CIRCLE INSCRIBED IN THE ELLIPSE','x = 4cos(t), y = 3 sin(t) for
t in [0, 2pi]','by','Antony
Foster'},'Color','black','FontName','mathematica','FontWeight','bold','FontSize',12)
% puts a 3 line title on my plot
legend('Ellipse','Circle','Location','NorthEastOutside')
set(ELLIPSE,'Color','black','LineWidth',3,'LineStyle','-')
set(CIRCLE,'Color','black','LineWidth',3,'LineStyle',':')
set(gca,'XTick',[-4:1:4],'XMinorTick','on','XTickLabel',{'-4','-3','-2','1','0','1','2','3','4'},'YTick',[-4:1:4],'YTickLabel',{'-4','-3','-2','1','0','1','2','3','4'},'YMinorTick','on','FontName','mathematica','FontWeight','bol
d')
xlabel('XAxis','Color','black','FontName','mathematica','FontWeight','bold','FontSize',12)
ylabel('YAxis','Color','black','FontName','mathematica','FontWeight','bold','FontSize',12)
axis square equal
axis([-4 4,-4,4])
grid on
% adds coordinate grid lines to my plot.
END OF THE CODE
THE PLOT FOR EXERCISE 2.1
LARGEST CIRCLE INSCRIBED IN THE ELLIPSE
x = 4 cos(t), y = 3 sin(t) for t in [0, 2pi]
by
Antony Foster
4
Ellipse
Circle
3
2
Y-Axis
1
0
-1
-2
-3
-4
-4
-3
-2
-1
0
X-Axis
1
2
3
4
Exercise 2.3 A polar curve
can be plotted by using
the parametric representation
with the polar angle θ as the parameter. Write a script M-file that displays the graphs of
a) The polar curve with equation r = 10cos(4θ) with 0 ≤ θ ≤ 2π (use t instead of θ in your Matlab
code). This curve should be drawn with a solid line style.
b) The polar curve with equation r = θ with 0 ≤ θ ≤ 4π. This curve should be drawn in a dashed line
style.
c) Include a title that contains your name.
d) Include a legend that identifies the two graphs. Publish your output to HTML.
THE CODE
constants
a = 0;
b = 2*pi;
d = 2*b;
GENERATING THE DATA TO BE PLOTTED
Parametric representation of the polar curves r = 10 cos(4 t) and r = t
t = linspace(0,b,60);
T = linspace(0,d,60);
r1 = @(t)10*cos(4*t);
r2 = @(t)t;
Definition of the leaves
x = @(t)r1(t).*cos(t);
y = @(t)r1(t).*sin(t);
% x coordinate as a function of t (the parameter)
% y coordinate as a function of t
Definition of the spiral
u = @(T)r2(T).*cos(T);
v = @(T)r2(T).*sin(T);
% plot both curves in the same coordinate system %
figure(3)
LEAVES = plot(x(t),y(t));
hold on
SPIRAL = plot(u(T),v(T));
hold off
Dress up the plot
title({' ','Graphs of the polar curves r = 10 cos(4 t) for t in [0, 2pi]','and r = t
for t in [0, 4pi]','by','Antony
Foster'},'Color','black','FontName','mathematica','FontWeight','bold','FontSize',12)
% puts a 3 line title on my plot
legend('LEAVES','SPIRAL','Location','NorthEastOutside')
set(LEAVES,'Color','black','LineWidth',3,'LineStyle','-')
set(SPIRAL,'Color','black','LineWidth',3,'LineStyle',':')
set(gca,'XTick',[-14:2:14],'XMinorTick','on','XTickLabel',{'-14','-12','-10','-8','6','-4','-2','0','2','4','6','8','10','12','14'},'YTick',[14:2:14],'YMinorTick','on','YTickLabel',{'-14','-12','-10','-8','-6','-4','2','0','2','4','6','8','10','12','14'})
set(gca,'FontName','mathematica','FontWeight','bold')
xlabel('XAxis','Color','black','FontName','mathematica','FontWeight','bold','FontSize',12)
ylabel('YAxis','Color','black','FontName','mathematica','FontWeight','bold','FontSize',12)
axis square equal % scale x and y axes equally.
axis([-14 14 -14 14])
grid on
% adds coordinate grid lines to my plot.
END OF CODE
THE PLOT FOR EXERCISE 2.3
Graphs of the polar curves r = 10 cos(4 t) for t in [0, 2pi]
and r = t for t in [0, 4pi]
by
Antony Foster
14
LEAVES
SPIRAL
12
10
8
6
Y-Axis
4
2
0
-2
-4
-6
-8
-10
-12
-14
-14 -12 -10 -8 -6 -4 -2
0
2
X-Axis
4
6
8 10 12 14
Exercise 2.2 Look up the input command in Help. Rewrite the script M-file called param1.m (shown
below) so that the input section prompts the user for the parametric equations and the range of the
parameter. Print a copy of your code and the output generated from a typical session. (Note that you
cannot use the option "publish to html" when the code uses the input command.)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
%
param1.m
%
%
%
% Plot the Parametric Curve x=cos(t) and y=sin(t) on the interval [0,2pi] %
% in the plane.
%
%
THE OLD CODE
%
%
% Beginning of User Input.
%
% clear all % Clear all variables
%
% clc
% Clear command window, command history is lost.
%
% syms t
% declaring the parameter t as a symbolic variable.
%
% x=cos(t); % the variable x is the x-coordinate of the curve
%
% y=sin(t); % the variable y is the y-coordinate of the curve
%
% a=0;
% the variable a is the left endpoint of the interval.
%
% b=2*pi;
% the variable b is the right endpoint of the interval.
%
%
% End of User Input.
%
% clf
% Clear any previous figure window.
%
% ezplot(x,y,[a,b]) % Plot the parametric curve.
%
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
THE NEW CODE
e1 = input('Enter the right hand side of your first parametric equation -> ','s');
e2 = input('Enter the right hand side of your second parametric equation -> ','s');
a = input('Enter the leftmost value of your parameter interval [a,b] -> ');
b = input('Enter the rightmost value of your parameter interval [a,b] -> ');
x=@(t)subs(e1);
x=@(t)subs(e2);
t=linspace(a,b,110);
figure(2)
h=plot(x(t),x(t));
hold off
DRESSING UP THE PLOT
title({'Graph of the parametric equations x=cos(t), y=sin(t), t in [0,
2*pi]','by','Antony Foster'},'FontName','mathematica','FontWeight','bold')
set(gca,'XTick',[-1:.25:1],'XMinorTick','on')
set(gca,'XTickLabel',{'-1','-3/4','-1/2','-1/4','0','1/4','1/2','3/4','1'})
set(gca,'YTick',[-1:.25:1],'YMinorTick','on')
set(gca,'YTickLabel',{'-1','-3/4','-1/2','-1/4','0','1/4','1/2','3/4','1'})
xlabel('XAxis','Color','black','FontName','mathematica','FontWeight','bold','FontSize',12)
ylabel('YAxis','Color','black','FontName','mathematica','FontWeight','bold','FontSize',12)
set(h,'Color','black','LineWidth',3,'LineStyle','-')
axis square equal
axis([-1 1, -1 1])
grid on
THE PLOT FOR EXERCISE 2.2
Graph of the parametric equations x=cos(t), y=sin(t), t in [0, 2*pi]
by
Antony Foster
1
3/4
1/2
Y-Axis
1/4
0
-1/4
-1/2
-3/4
-1
-1
-3/4
-1/2
-1/4
0
X-Axis
1/4
1/2
3/4
1