You are on page 1of 7

Name: Your Last name, Your First Name ( Last 4 digits of ID)

Course: Math 20300 SECTION


Instructor: Prof. Antony Foster
Due Date: Month, xxth, 19xx

MATLAB ASSIGNMENT 1
Purpose: Using Matlab to create plots of single variable functions in the xy-plane
Commands Used:
linspace (a, b, n)
diff(f(x),k,x)
f = @(x,y,z,t,)vectorized expression
subs(expression)
plot (x,y,)
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', 'the location')
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 1.1: Use matlab to create a plot of the function


derivative
on the same set of axes over the interval
marks should be at points with coordinates

and its
. The x-axis tick
.

MATLAB CODE
clear all
clc

% clear all removes all variables.


% CLC clears the command window and homes the cursor.

GENERATING THE DATA TO BE PLOTTED


domf = linspace (-pi, pi, 60); % Creates 60 linearly spaced point over the interval
domg = domf;
f = @(x) sin(x.^2).*cos(x);

% defines f as an anonymous function (vectorized).

dfx = diff(f(x),x);

% calculates the symbolic derivative of f(x).

g = @(x)subs(dfx);

% defines the symbolic derivative of f(x) as an anonymous vectorized

%function g(x).

PLOTTING THE GENERATED DATA


clf reset

% clear the current figure window and reset it to its default values.

figure(1)

% create a figure window and called it figure 1

PLOT1 = plot (domf, f(x));

% create a graphics handle for the 2-D plot of x vs f(x).

hold on
PLOT2 = plot (domg, g(x));

% creates a second graphics handle for the 2-D plot of x vs g(x) in

%figure(1) window f(x).


hold off

DRESSING UP YOUR PLOT

title

({'Graph of f(x) = x sin(x^2) and its derivative f`(x) = 2xcos(x^2) cos(x)

sin(x)sin(x^2)','by','Foster,Antony'},'color','red','FontName','mathematica','FontWeight','bold','FontSize', 12)

legend ('f(x)','f'(x)','Location','NorthWest')
set (PLOT1,'Color','black','LineWidth','3','LineStyle','-')
set (PLOT2,'Color','red','LineWidth','3','LineStyle','-')
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: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])
grid on

END OF CODE

THE RESULTING PLOT

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

F(x)
F`(x)

4
3

Y-Axis

2
1
0
-1
-2
-3
-4
-5
-6
-7
-pi

-pi/2

X-Axis

pi/2

pi

Exercise 1.3: Plot the expressions

and

on the interval

The two graphs should form the unit circle


. Look up the axis command in Help to
see how to get the picture to actually look like a circle, rather than an ellipse.
MATLAB CODE
clear all
clc
GENERATING THE DATA TO BE PLOTTED
domf = linspace(-1, 1, 60);
domg = domf;
f =@(x)(1-x.^2).^(1/2);
% upper half of circle is the function f(x) = (1-x^2)^(1/2)
g = @(x)((1-x.^2).^(1/2)).*-1; % lower half of circle is the h(x) = -(1-x^2)^(1/2)

PLOTTING THE GENERATED DATA


clf reset
figure(1)

% make figure(1) the plotting environment.

PLOT1 = plot(domf,f(x));
hold on
PLOT2 = plot(domg,g(x) );
hold off

DRESSING UP YOUR PLOT

title({'Graph

of the Circle x^2 + y^2 =

1','by','Foster,Antony'},'Color','red','FontName','mathematica','FontWeight','bold','FontSize',12)

legend('sqrt(1-x^2)','-sqrt(1-x^2)','Location','NorthEastOutside')
set(PLOT1,'Color','black','LineWidth',3,'LineStyle',':')
set(PLOT2,'Color','black','LineWidth',3,'LineStyle','-')
set(gca,'XTick',[-1:1/2:1],'XMinorTick','on','XTickLabel',
{'-1','-1/2','0','1/2','1'},'FontName','mathematica','FontWeight','bold')

set(gca,'YTick',[-1:1/2:1],'YTickLabel',{'-1','-1/2','0','1/2','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
grid on

1 -1

1])

% increments on the x-,y- are equal in size. This


% makes a circle look round, instead of elliptical
% set the x- y- axis limits
% adds grid lines to plot

END OF CODE

THE RESULTING PLOT


2

Graph of the Circle x + y = 1


by
Foster, Antony
1
sqrt(1-x 2 )
-sqrt(1-x 2 )

Y-Axis

1/2

-1/2

-1
-1

-1/2

1/2

X-Axis

THE END OF MATLAB ASSIGNMENT 1