Professional Documents
Culture Documents
Matlab Tutorial
Matlab Tutorial
Matlab Tutorial
The goal of this tutorial is to familiarize you with the fundamentals of Matlab (defining variables,
doing calculations and creating graphs). Later, we will use these fundamentals to develop Matlab
script files (programs) to solve more complicated models.
Matlab Basics
To start a MATLAB session (i.e. open a MATLAB window), click on the MATLAB Application
shortcut. After some time, a MATLAB window, similar to that shown in Figure 1, will appear.
For calculations in this tutorial, 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
-----------------------------------------------------------------------------------------------------------------
o Variable names are case sensitive (so T is not the same is t). Variable names can
include letters or numbers
o Other legal variable names would be Pressure, Vol, nMoles, Rvalue, T1, ...
2. You can recall previous lines by using up-arrow key
3. Arithmetic Operations & Simple Calculations (+, -, *, /, ^)
o The volume can be determined by typing V= n*R*T/P
4. The role of the Semi-colon in Matlab
oMatlab will print out the answer for any command not terminated by a semi-colon.
oMatlab will not print out the result if the line ends in a semi-colon
5. To find out help on a command (such as plot), type help plot (or use the pull down help
menu)
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.
In general using [ a : b : c ] where a , b and c are numbers will create a row vector whose 1st
element is a , second element a + b etc and whose last element is no greater than c (if b > 0 , no less
than c if b < 0). If there are only two numbers then MATLAB assumes the increment (b above) is 1.
o Using the zeros and ones commands, e.g. y=zeros(1,3); z = ones(1,5);
o Explicitly listing terms, e.g. x = [1 4 9 16]
o Reminder: If you do not include a semi-colon, the entire list will be printed
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 when you include dot operator “.”(.* ./ 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
We can simplify repetitive tasks by storing a series of Matlab commands in a script file (later we
will use these to write powerful programs)
1. Use "New, M-File" under file menu or use any text editor
2. Type in a series of commands, such as
o n=1;
o R=0.0821;
o T=300;
o V=10:.5:30;
o P300 = n*R*T ./ V;
o T=200;
o P200 = n*R*T ./ V;
o plot(V,P200,'r-',V,P300,'g:')
o legend('T=200 Kelvin','T=300 Kelvin')
o xlabel('Volume (Liters)')
o ylabel('Pressure (Atmosphere)')
Matrices
To fully understand how Matlab uses matrices, we must discuss a bit about linear algebra.
1. When we define a list using a command such as t=1:3, Matlab creates a row vector
containing 3 elements.
2. A row vectors can be turned into a column vector using the transpose (') operator. Using the
definition of t above, note what happens for the command t' and t''
3. The multiplication, division and exponential operators (*, /, ^) operating on two vectors
will do matrix multiplications, not multiplying the elements together.
4. Given a list x=0:2:10; then x.*x or x.^2 will give a list with the squares of the even
integers [0 4 16 36 64 100] because it is doing list or array multiplication
5. The command x*x will produce an error (since you can't do matrix multiplication of two
row vectors together).
6. The command x*x' be will give the value of 220 (0+4+16+36+64+100=220) since the
transpose operator (') means that we have a row times column matrix (resulting in a scalar).
7. The command x'*x will result in a 6 by 6 matrix
8. We can extract and modify elements of a list
o Selecting an element of a list, use x(3)
o We can set an element using x(3)=100;
o To select a range of a list, use x(3:5) or x(:3) or x(5:)