

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


MATLAB Lab for Math 526 Week 6
Any decent mathematical software must provide some ability to present results in a graphical format. MATLAB is decent software, so it must have graphical capabilities. This lab introduces the basics skills needed to create and manipulate MATLAB graphs. (There is no real linear algebra content.) MATLAB plot commands introduced in this lab include: plot, clf, xlabel, ylabel, title, hold, figure, and close. Other new MATLAB commands introduced in this lab include the array multiplica- tion (.*) and array power (.^) commands.
The best place to start to learn about MATLAB graphics is the online help: help plot. Take a look at this help information - NOW! Notice that the simplest usage of plot is plot( x, y ) where x and y are vectors with the same number of elements.
Plotting a Function on an Interval
x = -4 : 0.04 : 4; % partition interval [โ 4 , 4] w/ mesh size 0. 04 y = sin(x); % apply sine function to each component of x plot( x, y ) % display graph of y = sin x on [โ 4 , 4] clf % clear the MATLAB figure window
Changing the Line Style in a Plot
clear x y % clear values from x and y x = -1.5 : 0.01 : 1.5; % partition interval [โ 1. 5 , 1 .5] with mesh size 0. 01 y = exp(-x.ห2); % compute eโx 2 for each component of x plot( x, y ) % display graph of y = eโx
2 on [โ 4 , 4] xlabel( โx-axisโ ) % add label to the x-axis ylabel( โy-axisโ ) % add label to the y-axis title( โMy Second MATLAB Plotโ ) % add title to the figure
This shows only the most basic usage of these commands. Please consult help xlabel, help ylabel, and help title for additional usages of these commands. See help arith to better understand array multiplication (.) and array power (.^) and how they compare with matrix multiplication () and matrix power (^).
Version 3 (Fall 2005) Created 13 September 2005
MATLAB Lab for Math 526 Week 6
Changing the Line Style in a Plot
clf % clear any existing figure plot( x, y, โ-.โ ) % what does the third argument do?
Additional information about the line styles, color, and symbols is contained in help plot.
Multiple Plots in One Figure
t = -1.5 : 0.1 : 1.5; s = t.ห3; % note the use of .^ plot( t, s ) % graph of y = x^3 replaces graph of y = eโx
2
hold on % future plot commands add to existing figure plot( x, y ) % graph of y = eโx
2 and graph of y = x^3
Two plots can be shown in the same figure using a single plot command as follows: figure % create a new figure (plotting window) plot( x,y,โroโ, x,x.ห3,โb+โ ) % graphs of y = eโx 2 with red circles % and graph of y = x^3 with blue plusses close all % close all figure windows
Please remember that a figure that looks good on the screen (in color) might not look very good when printed on a black-and-white printer. In Part II you will be asked to use MATLAB to create some specific figures.
Clear all variables before you begin to work on Part II.
Version 3 (Fall 2005) Created 13 September 2005