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:

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?

  1. Simple calculations
  2. Plotting and analyzing mathematical relationships (2D and 3D)
  3. List & Matrix Operations
  4. Writing script files (a type of programming)
  5. Symbolic manipulation of equations
  6. 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

Matlab Basics

  1. The command diary file_name command saves all subsequent output to file file_name
  2. Creating and using variables
  3. You can recall previous lines by using up-arrow key
  4. Arithmetic Operations & Simple Calculations (+, -, *, /, ^)
  5. The role of the Semi-colon in Matlab
  6. To find out help on a command (such as plot), type help plot (or use the pulldown help menu)
  7. 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.

  1. There are several ways to create a list
  2. 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
  3. Array operations on a list (.* ./ and .^ operators) operate on each element of the list individually
  4. Try x=1:3 y=2:4 then type operations like x.*y, x./y, x.^y
  5. Type in T=300; V = 10:20; P=n*R*T./V to get the pressure at several different volumes
  6. There are many intrinsic Functions (sin, cos, exp, ...) which can operate on list elements. angle=0:pi/4:pi, sin(angle)

Creating Plots

  1. To plot two lists, enter a command such as plot(V,P)
  2. 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])
  3. To set colors and line types use optional plot arguments
  4. To overlay graphs, type hold on and subsequent graphs will be on the same axes. To start a new graph, type hold off
  5. Labels and titles can be added by using commands such as
  6. To zoom in on a graph, type zoom on and highlight a region with a mouse

For Further Information

Electronic Copy:
Revised: 28-JAN-97 by Tom Huber, Physics Department, Gustavus Adolphus College.