Matlab Fundamentals
Tom Huber, Gustavus Adolphus College
Envision It Workshop, February 1, 1997
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.
What can we use Matlab for?
- 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
Matlab Basics
- The command diary file_name command saves all
subsequent output to file 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 T is not the
same is t). Variable names can include letters or numbers
- 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 help
plot (or use the pulldown help menu)
- To list all commands which have a keyword in their description (such
as sin), type lookfor sin
Lists & List Arithmetic
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 x=1:3 y=2:4 then type operations like x.*y,
x./y, x.^y
- Type in T=300; V = 10:20; P=n*R*T./V to get the pressure
at several different volumes
- There are many intrinsic Functions (sin, cos, exp, ...) which can operate
on list elements. angle=0:pi/4:pi, sin(angle)
Creating Plots
- To plot two lists, enter a command such as plot(V,P)
- To select axis range use the axis command with a 4
element list containing [xmin, xmax, ymin, ymax], e.g. axis([5
8 1 2])
- To set colors and line types use optional plot arguments
- plot(V,P,'ro') or plot(V,P,'b--')
- To overlay graphs, type hold on and subsequent graphs
will be on the same axes. To start a new graph, type 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 zoom on and highlight a
region with a mouse
For Further Information
Electronic Copy: http://physics.gac.edu/~huber/matlab/mtlabfun.htm
Revised: 28-JAN-97 by Tom
Huber, Physics Department, Gustavus
Adolphus College.