Docsity

MATLAB Lab for Math 526: Two-Dimensional Plotting with MATLAB Commands, Lab Reports of Mathematics

Toggle sidebar
bg1
MATLAB Lab for Math 526 Week 6
Two-Dimensional Plotting
Susanne Brenner and Li-Yeng Sung
(modified by Douglas B. Meade)
Department of Mathematics
Overview
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.
Part I
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 xand 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 xon [โˆ’4,4]
๎˜clf % clear the MATLAB figure window
Changing the Line Style in a Plot
๎˜clear x y % clear values from xand 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
โˆ’x2for each component of x
๎˜plot( x, y ) % display graph of y=e
โˆ’x2on [โˆ’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
pf2

Partial preview of the text

Download MATLAB Lab for Math 526: Two-Dimensional Plotting with MATLAB Commands and more Lab Reports Mathematics in PDF only on Docsity!

MATLAB Lab for Math 526 Week 6

Two-Dimensional Plotting

Susanne Brenner and Li-Yeng Sung

(modified by Douglas B. Meade)

Department of Mathematics

Overview

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.

Part I

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