Matlab is a versatile program which allows you to use a computer to solve a vast number of problems in science and mathematics both numerically and through visualization. Some of the strengths of Matlab are:

- Flexibility to solve a large number of problems
- Good tools for visualization
- Can be used on most computer systems (Mac, PC, Unix Workstation, Cray, ...)
- Used at many colleges, universities and industry in many different disciplines (science, math, computer science, finance)
- The scripting language is relatively easy to learn, and is complementary with traditional programming languages (BASIC, FORTRAN, C, Pascal)

The goal of this tutorial is to familiarize you with the fundamentals of Matlab (defining variables, doing calculations and creating graphs). In our next session, we will use these fundamentals to develop Matlab script files (programs) to solve more complicated models.

- Simple calculations
- Plotting and analyzing mathematical relationships (2D and 3D)
- List & Matrix Operations
- Writing script files (a type of programming)
- Symbolic manipulation of equations
- Advanced visualization, animation and GUI interface tools

We will concentrate on Numbers 1-3 (with a brief discussion of Numbers 4 and 5) in this session and Number 4 in our next session

For many of the calculations in this document, we will be using the ideal gas equation

**P*V = n*R*T**

- P = Pressure in Atmospheres
- V = Volume (in Liters)
- n = Number of Moles of Gas
- R = 0.0821 Atm*Liters/mol*Kelvin
- T = Temperature in Kelvin

- The command
command saves all subsequent output to file`diary`*file_name*`file_name` - Creating and using variables
- Type in
**P=1**<ENTER>**n=1**<ENTER>**R=0.0821**<ENTER>**T=273**<ENTER> - Variable names are case sensitive (so
is not the same is`T`). Variable names can include letters or numbers`t` - Other legal variable names would be
`Pressure, Vol, nMoles, Rvalue, T1, ...` - You can recall previous lines by using up-arrow key
- Arithmetic Operations & Simple Calculations (+, -, *, /, ^)
- The volume can be determined by typing
`V= n*R*T/P` - The role of the Semi-colon in Matlab
- Matlab will print out the answer for any command not terminated by a semi-colon.
- Matlab will not print out the result if the line ends in a semi-colon
- To find out help on a command (such as
`plot`), type(or use the pulldown help menu)`help plot` - To list all commands which have a keyword in their description (such
as sin), type
`lookfor sin`

In addition to doing operations on single numbers, Matlab allows us to perform operations on a list of numbers. These lists are also called Vectors, Arrays or Matrices in the Matlab manuals.

- There are several ways to create a list
- Using the Colon notation, e.g.
`T= 300:10:400;` - Using the zeros and ones commands, e.g.
`y=zeros(1,3); z = ones(1,5);` - Explicitly listing terms, e.g.
`x = [1 4 9 16]` *Reminder: If you do not include a semi-colon, the entire list will be printed*- We can do addition, subtraction, multiplication and division of a list
and a value using the (
) operators. Try, for example,`+, -, *, /``T*2 , T/5, T+20, T/10+5` - Array operations on a list (
and`.* ./`operators) operate on each element of the list individually`.^` - Try
then type operations like`x=1:3 y=2:4``x.*y, x./y, x.^y` - Type in
to get the pressure at several different volumes`T=300; V = 10:20; P=n*R*T./V` - There are many intrinsic Functions (sin, cos, exp, ...) which can operate
on list elements.
`angle=0:pi/4:pi, sin(angle)`

- To plot two lists, enter a command such as
`plot(V,P)` - To select axis range use the
command with a 4 element list containing [xmin, xmax, ymin, ymax],`axis`*e.g.*`axis([5 8 1 2])` - To set colors and line types use optional
arguments`plot` or`plot(V,P,'ro')``plot(V,P,'b--')`- To overlay graphs, type
and subsequent graphs will be on the same axes. To start a new graph, type`hold on``hold off` - Labels and titles can be added by using commands such as
`xlabel('Volume (in Liters)')``ylabel('Pressure (in Atm)')``title('Ideal Gas Plot')`- To zoom in on a graph, type
and highlight a region with a mouse`zoom on`

- There are additional handouts on the fundamentals of matrices, script files and symbolic math. There are several problems which you can study in a separate document
- There is an excellent tutorial in the
*Student Edition of Matlab*manual - The book
*Mastering Matlab,*by Duane Hanselman & Bruce Littlefield (Prentice Hall, 1996) - There are many well written tutorials on the WWW, including
- The Mathworks homepage for Matlab is
`http://www.mathworks.com` - The Mathworks education homepage is
`http://education.mathworks.com` - Other resources can be found at
`http://physics.gac.edu/~huber/matlab/`

Electronic Copy: `http://physics.gac.edu/~huber/matlab/mtlabfun.htm
`Revised: 28-JAN-97 by Tom
Huber, Physics Department, Gustavus
Adolphus College.