Math 111: MATLAB Assignment 1

Due Date: September 29, 2017

The example below shows how MATLAB is used to plot simple functions. To begin, click on the Matlab icon on your desktop to open a Matlab screen. Click on File -> New -> m-file to create a new file. Cut-and-paste, or type in, the code below. Save as an .m file, e.g., plotcode.m. Now, at the >> prompt in the Command window, type the file name plotcode. The program will run and the plots should appear. Consult the MATLAB TA's if you have any questions.

Contents

Creating arrays to plot

First we create arrays of numbers x, y, z, and w. The array x contains 1001 evenly-spaced numbers from -pi to pi. The arrays y, z, and w are functions of x.

Note each line ends with a ';'. If this semicolon is missing, matlab will display the value of the variable you have created.

x = linspace(-pi,pi,1001);
y = sin(10 * x);
z = cos(10 * x);
w = (y + z) / 2;

Plotting the functions y(x), z(x), and w(x)

The '-', '-.', and '*' commands tell matlab to use different styles for drawing the different lines

It is always important to label your x-axis, your y-axis, and give the graph a title!

The 'axis' command tells Matlab to set the graph axes to

plot(x,y,'-',x, z, '-.',x, w, '*');
axis([-pi pi -1 1])
xlabel('x')
ylabel('y, z, and w')
title('My first matlab graph')

Your assignment

After running the above example, do the following to hand in. For this assignment, you should hand in at most 3 pages, consisting of two plots and your code.

1) Use MATLAB to plot

on an interval

make sure to use enough points so that the graph looks smooth!

Use your graph to help you answer the following question. What is the limit of this function as x approaches zero?

2) On a separate graph, plot

and

on the interval

3) Put your own name in the 'title' of the graph to prove this is your own work.

Matlab note: In the above example we defined sin(10*x). We can do this because 10 is a scalar, not an array. The '*' sign will not, however, multiply two arrays together. If we want to multiply two arrays together, or divide by an array we need to use the matlab commands '.*' and './' rather than '*' or '/'. Similarly, to raise an array to a power, we need to use the '.^' command. Finally, use the matlab command 'sqrt(x)' to take the square root of x.