- Basic graphical operations for plotting data in one and two dimensions.
- Defining grids.
- MATLAB's Help facility.
- Special mathematical and logical functions.
- Matrix manipulations (Demos).
- Implementing the Fast Fourier Transform (FFT) in MATLAB (Demos).

* Note: At any time during a MATLAB session you may type help COMMAND for MATLAB's internal information on how COMMAND may
be used, including examples.* If you do not know what command you are looking
for, you may type

__EXERCISE 1__: Type

help linspaceand after reading about

lookfor spaceto see what other relevant commands there might be.

Let's begin by graphing some familiar one-dimensional functions using MATLAB . The first step is to make a vector corresponding to the real axis, say between -5 and 5:

x = linspace(-5,5,101);The above command generates a vector with 101 components, named `x', whose elements are evenly spaced numbers beginning at -5 and ending at 5. The semicolon (;) suppresses output - try the command without it and see what happens! Notice also that we get a spacing of = 1/10 using the above command - the 101 allows us to have the endpoints included in the vector of coordinates.

Now define a function,
, which we will then plot. The
function will be a vector, f, which contains for every component of the
coordinate vector, `x`, a value according to the rule :

f = sin( x.^2 )./(1 + x.^2);The notation `.' before an operation means to apply the operation element-by-element to an array; thus

x.^2means the element by element square of

plot(x,f)which will plot the ordered pairs given by

Now, to see how to put multiple plots on one graph and change some of the styles, try the following:

plot(x,f,'r*') hold on, plot(x,1./(1+x.^2),'g'), hold off xlabel('x'),ylabel('f(x)'),title('Function and Envelope')The `hold' commands force MATLAB to draw plots without erasing previous plots. One may also plot several functions simultaneously, as in

figure(2) plot(x,cos(x),'r',x,exp(-abs(x)),'b',x,sin(x.^2),'g')Now, just to see what some of the options would be, type

help plotwhich will give you a number of examples, sets of options, and some suggestions for other commands you might find useful. Try some of these just to get the hang of it.

__EXERCISE 2__: Plot the
fundamental solution of the heat equation,

which has the form

for the following values: 1, 2, 4. Plot all on the same set of axes, over an interval from -10 to 10, for a value of .37, and organize the time slices in the colors of the MATLAB spectrum ('r','y','g','b','m'). Label the axes and give the plot a title. The

Defining functions of two variables works with the same philosophy in MATLAB , except that the underlying substrate of independent variables becomes a matrix as opposed to a vector. Let's begin by defining two coordinates, `x` and `y` and then a two-dimensionally extended version of these coordinates:

x=.1*[0:100]; y=.05*[-50:50]; [X,Y]=meshgrid(x,y);(Notice the scalar multiplication of a spacing, either .1 or .05 above, and an array of numbers,

as a surface, we could use the sequence of commands

g = sin(X.^2+pi*Y)./(1+X.^2+Y.^2); surf(X,Y,g)Can you tell which is the and which the direction? Now let's change the location of the grid. Use the up arrow, , to access previous commands; to access the previous command starting with `x=' type `x=' on the command line and then hit . Now you can use the back and right to move through the previous command. Typing a character will insert it; backspace removes it. Call up the previous command defining the grid for

surf(X,Y,g),shading flatThe

figure(2),plot(x,g(1,:))The colon (:) in the second argment of g says `all available indices,' and in this case `

Now let's plot contours and densities in another window. Try

figure(3), pcolor(X,Y,g), shading flat, colormap hotThis will create a new figure, put a density plot of

hold on, contour(X,Y,g,'b'), hold offThis will place 20 aesthetically chosen contours in blue on the color density plot.

__EXERCISE 3__: The function

is a solution to the wave equation, , which models, among other things, waves on the surface of a canal, in this case moving at speed 2 in either direction. Define a meshgrid in and which will allow you to visualize this solution for times between 0 and 4. Use surface plots, density plots, and contours to visualize the behavior of this solution. Also, in a third figure plot a `waterfall' diagram, that is several slices in constant time, varying

One of the nice features of MATLAB is the existence of logical matrices, which
are just arrays with elements that are either zero or one depending on whether
a statement is true or false. Let's return to `x` and `f` given in
the first section above. Re-input these into MATLAB (remember !) and
now let's manipulate using logical matrices. For example, suppose that we
would like to plot *only* the positive portions of f. Define

fplus=( f>=0 );The array

plot(x, f, 'y', x, fplus.*f, 'ro')By multiplying

hold on, plot(x, (1-fplus).*f,'b^'),hold offThese logical matrices can also be used to create piece-wise functions, which we will see next.

Suppose you wanted to make a function which was Gaussian for two standard
deviations (), and then uniform (with value ) for two
standard deviations, and then zero, and then that you wanted to re-normalize
it so that the resulting function still represented a probability density
function (pdf). Pick a standard deviation of .5; a normal pdf with standard
deviation .5 is given by

First let's define the space and normal vectors:

x = 6/100*[-50:50]; N = sqrt(2/pi)*exp(-2*x.^2);Now we need logical matrices for the different portions of the pdf we are building. These we will call `

norm=(abs(x)<=1); uni=(abs(x)>1 & abs(x)<=2);The MATLAB command

pdf=N.*norm + sqrt(2/pi)*exp(-2)*uni;You should plot this to see that it is correct. Finally we must normalize. We have defined a grid with spacing 6/100=.06, so a quick approximation of the actual integral would be

mass=.06*sum(pdf)The command

mass=.06*trapz(pdf)which sums the

pdf=pdf/mass;Now plot this. You can use up-arrows to just find the command you used last time, or you can compare it to the previous (unnormalized) function by using

__EXERCISE 4__: A function which we will see again in dispersal is the steady-state
solution to the diffusion-loss model with a localized source at the origin,

the steady-state ( ) solution of which is

Unfortunately this predicts a small, but perceptible presence of a diffusing particle infinitely far away from the origin. One way to address this would be to chop off the function at some realistic point and then normalize the resulting function. Let and and truncate the function at 50, 25, and 10 percent of its peak value, then re-normalize so that the resulting function is a pdf. On a single graph, compare these three pdfs to a (normalized) version of the original function.

Now that you have a MATLAB basis, you might find it useful to tour through some of MATLAB 's cababilities using **demos**. The demos are a set of GUI slide shows which illustrate various MATLAB functions. So, on the command line type

demosand a window should pop up on screen with a variety of MATLAB subjects. To get comfortable with the demos and see some of how MATLAB handles linear algebra click on

__EXERCISE 5__: The Nicholson-Bailey model for host-parasite interactions is

The steady state corresponding to coexistence of host and parasite in this model is given by

The Jacobian matrix (found by hand, not MATLAB !) is

Show that when (corresponding to the interaction between a greenhouse whitefly and its chalcid parasitoid) that the steady state is unstable. At the very least you will need to take the eigenvalues of a 2x2 matrix and to see if any of these eigenvalues have magnitude greater than one. Can you think of a way to plot a stability diagram in which will illustrate that these equilibrium populations are

In the rest of the course we will be using the Fast Fourier Transform (FFT) to
evaluate the consequences of dispersal. You can use the **demos** facility
to start learning about FFTs and their implementation in MATLAB .
To begin with, click **Numerics** in the left-hand window and then **Fast
Fourier Transform** in the right hand window, followed by clicking on **Run
Fast Fourier...** on the button in the lower right hand corner. You can now
page through slides which illustrate how one can use the FFT to analyze
sunspot data. *I suggest typing
*

help fft

*Congratulations! You have `jumped in the deep end' with MATLAB , but you didn't sink! *

2001-04-06