MATLAB ASSIGNMENT 3

Name: Foster, Antony (XXXX)
Course: Math 20300 XX
Instructor: Prof A Foster
Due Date: June XX, XXXX
MATLAB ASSIGNMENT 3
Purpose: Using Matlab to create plots from raw data and plot more single
variable functions in the xy-plane .
Commands Used:
linspace (a, b, n)
f = @(x)vectorized expression
plot (x,f(x))
clear all
[a:b:c]
clc
clf reset
figure(k)
hold on/off
title('Your title','color','your color', 'FontName','Your
Font','FontWeight','bold','FontSize',12)
legend('String1','String2',…,'Location', 'options')
set(handle, other options)
set(gca, other options)
xlabel ('String', other options)
ylabel ('String', other options)
axis square/equal
axis([a, b, c, d])
grid on/off
Exercise 3.5 Write a script M-file that does all of the following:
A) Uses the plot command to draw the square with vertices (1, 2), (3 ,2), (3, 4) and
(1, 4). The square should be drawn in red (at least your script M-file should show
that).
B) Uses the plot command to draw the two diagonals of the square in blue with a dotted
line.
C) Uses the axis command to change the plotting window to the region [0,5] x [0,5] and
then to adjust the axes so that the square looks like a square and not a rectangle.
D) Finally, add a title: “A Square by your name”
Publish your M-file (put your name in a comment at the beginning of the script M-file)
and the graph.
MATLAB CODE
clear all
clc
x=[1 3 3 1 1];
y=[2 2 4 4 2];
% the x coordinates of the square
% the y coordinates of the square
clf reset
figure(1)
plot(x,y,'r','LineWidth',3) % plots the square
hold on
plot([x(4),x(2)],[y(4),y(2)],[x(1),x(3)],[y(1),y(3)],'b','LineWidth',3,
'LineStyle',':') % plots the diagonals
hold off
DRESSING UP YOUR PLOt
title('A Square by Antony Foster','FontName','mathematica','FontWeight','bold')
legend('Sides','Diagonals')
set(gca,'XTick',[0:5],'XMinorTick','on','XTickLabel',{'0','1','2','3','4','5'},
'FontName','mathematica','FontWeight','bold')
set(gca,'YTick',[0:5],'YMinorTick','on','YTickLabel',{'0','1','2','3','4','5'},
'FontName','mathematica','FontWeight','bold')
xlabel('X-Axis','Color','red','FontName','mathematica','FontWeight','bold',
'FontSize',12)
ylabel('Y-Axis','Color','red','FontName','mathematica','FontWeight','bold',
'FontSize',12)
axis square equal % makes both axes the same length and the same scale
axis([0,5,0 5])
grid on
THE MATLAB OUTPUT
A Square by Antony Foster
5
Sides
Diagonals
4
Y-Axis
3
2
1
0
0
1
2
3
X-Axis
4
5
Exercise 3.6 Let
This exercise duplicates Exercise 1.1, but you are
expected to generate the output using a script M-file.) Write a script M-file that uses the plot
command to draw graphs of and over the interval –
In this problem, you
should enter the derivative formula by hand when you need numerical values of . Later in the
course we will discuss how to mix symbolic and numerical calculations. The plot should have
the following features:
A) A legend identifying which graph is which, for example one graph identified as y and one as
y' or dy. (See help on the legend command.)
B) distinct line styles for the two graphs (one dotted, one solid) and different colors if you are
using a color printer.
C) both graphs displayed without chopping any values in the range
D) labels on both axes and x-tick marks at the points
and π. See Matlab's
Help on the commands Xtick and Xticklabel to find out how to change the labeling of the axis
tick marks in an M-file. You cannot actually get the labels displayed with the Greek font. You
can use pi/2, pi, etc.
E) A title: “Graph of
The title should also contain YOUR NAME.
F) Grid displayed. Publish your M-file.
MATLAB CODE
clear all
clc
x = linspace(-pi,pi,120);
f = @(x)sin(x.^2).*cos(x); % my function f(x) = sin(x^2)cos(x)
fprime = @(x)2.*x.*cos(x.^2).*cos(x)- sin(x).*sin(x.^2); % and its
derivative
% f'(x) = 2xcos(x^2)cos(x)+ sin(x)sin(x^2)
%%Make the Graphs%%
clf reset
figure(2)
PLOT1=plot(x,f(x),'Color','black','LineWidth',3,'LineStyle','-');
hold on
PLOT2=plot(x,fprime(x),'Color','red','LineWidth',3,'LineStyle',':');
hold off
DRESSING UP YOUR GRAPH
title({'Graphs of F(x) = sin(x^2)cos(x) and F`(x) = 2xcos(x^2)cos(x) sin(x)sin(x^2)','by','Antony Foster'},'Color','red','FontName',
'mathematica','FontWeight','bold','FontSize',12) % puts a 3 line title on my plot
legend('Y','dY','Location','NorthWest')
set(gca,'XTick',[-pi:pi/2:pi],'XMinorTick','on','XTickLabel',
{'-pi','-pi/2','0','pi/2','pi'},'FontName','mathematica','FontWeight','bold')
set(gca,'YTick',[-7:1:7],'YMinorTick','on','FontName','mathematica',
'FontWeight','bold')
xlabel('X-Axis','Color','red','FontName','mathematica','FontWeight',
'bold','FontSize',12)
ylabel('Y-Axis','Color','red','FontName','mathematica','FontWeight','bold',
'FontSize',12)
axis([-pi,pi -7 7])
% set the x- y- axis limits
grid on
% adds coordinate grid lines to my plot.
MATLAB OUTPUT
2
2
2
Graphs of F(x) = sin(x )cos(x) and F`(x) = 2xcos(x )cos(x) - sin(x)sin(x )
by
Antony Foster
7
6
5
Y
dY
4
3
Y-Axis
2
1
0
-1
-2
-3
-4
-5
-6
-7
-pi
-pi/2
0
X-Axis
pi/2
pi
Exercise 3.7 The plot command can also be used to plot curves defined parametrically. One simply
uses the parametric equations
,
to generate and values from a subdivision of the
parameter interval. Using this idea, generate a plot of the circle
from the parametric equations
, for t in [0, 2π]. Use the axis
command to ensure that the graph looks circular. Label the axes and add a title. Publish your M-file.
MATLAB CODE
clear all
clc
t=linspace(0,2*pi,200);
x=@(t)cos(t);
y=@(t)sin(t);
clf reset;
figure(3)
h = plot(x(t),y(t));
% h is a handle for the plot.
DRESSING UP THE GRAPH
title({'The Unit Circle x = cos(t), y = sin(t), t in [0, 2pi]','by','Antony
Foster'},'Color','red','FontName','mathematica','FontWeight','bold','FontSize',12)
% puts a 3 line title on my plot
set(h,'Color','black','LineWidth',2,'LineStyle','-')
set(gca,'XTick',[-1:0.5:1],'XMinorTick','on','XTickLabel',
{'-1','-1/2','0','1/2','1'},'FontName','mathematica','FontWeight','bold')
set(gca,'YTick',[1:0.5:1],'YMinorTick','on','FontName','mathematica',
'FontWeight','bold')
xlabel('X-Axis','Color','red','FontName','mathematica',
'FontWeight','bold','FontSize',12)
ylabel('Y-Axis','Color','red','FontName','mathematica',
'FontWeight','bold','FontSize',12)
axis square equal
axis([-1,1 -1 1])
% set the x- y- axis limits.
grid on
% adds coordinate grid lines to my plot.
MATLAB OUTPUT
The Unit Circle x=cos(t), y=sin(t), t in [0, 2pi]
by
Antony Foster
1
Y-Axis
0.5
0
-0.5
-1
-1
-1/2
0
X-Axis
1/2
1