Title: Introduction to MATLAB
1Introduction to MATLAB
- Division of Engineering and Applied Sciences
2What is MATLAB
- High level language for technical computing
- Stands for MATrix LABoratory
- Everything is a matrix - easy to do linear algebra
3The MATLAB System
- Development Environment
- Mathematical Function Library
- MATLAB language
- Application Programming Language (not discussed
today)
4Where to get MATLAB
- FAS computing
- Download from http//fas.harvard.edu/computing/sof
tware/ - You must be on FAS network to use MATLAB
- DEAS IT
- If you are working with a DEAS faculty member
- Mathworks
- Student version is affordable and complete.
5MATLAB Desktop
Menu and toolbar
Workspace
History
Command
6Matrices Vectors
- All (almost) entities in MATLAB are matrices
- Easy to define
- Use , or to separate row elements -- use
to separate rows - Order of Matrix -
- mno. of rows, nno. of columns
7Matrices Vectors - II
- Vectors - special case
- n 1 column vector
- m 1 row vector
- Transpose
8Creating Vectors
Create vector with equally spaced intervals gtgt
x00.5pi x 0 0.5000 1.0000 1.5000 2.0000
2.5000 3.0000
Create vector with n equally spaced intervals gtgt
xlinspace(0, pi, 7) x 0 0.5236
1.0472 1.5708 2.0944 2.6180 3.1416
Equal spaced intervals in logarithm space gtgt
xlogspace(1,2,7) x 10.0000 14.6780
21.5443 68.1292 100.0000
9Creating Matrices
- zeroes(m, n) matrix with all zeros
- ones(m, n) matrix with all ones.
- eye(m, n) the identity matrix
- rand(m, n) uniformly distributed random
- randn(m, n) normally distributed random
- magic(m) square matrix whose elements have the
same sum, along the row, column and diagonal. - pascal(m) Pascal matrix.
10Matrix operations
- exponentiation
- multiplication
- / division
- \ left division. The operation A\B is
effectively the same as INV(A)B, although left
division is calculated differently and is much
quicker. - addition
- - subtraction
11Array Operations
- Evaluated element by element
- .' array transpose (non-conjugated transpose)
- . array power
- . array multiplication
- ./ array division
- Very different from Matrix operations
gtgt A1 23 4 gtgt B5 67 8 gtgt AB 19
22 43 50
But gtgt A.B 5 12 21 32
12Indexing Matrices
- Given the matrix
- Then
- A(1,2) 0.6068
- A(3) 0.6068
- A(1,) 0.9501 0.6068 operator
-
n
A 0.9501 0.6068 0.2311 0.4860
m
1n
13Graphics - 2D Plots
- plot(xdata, ydata, marker_style)
- For example Gives
gtgt x-50.15 gtgt sqrx.2 gtgt pl1plot(x, sqr,
'rs')
14Graphics - Overlay Plots
- Use hold on for overlaying graphs
- So the following Gives
gtgt hold on gtgt cubx.3 gtgt pl2plot(x,
cub,b-o')
15Graphics - Annotation
- Use title, xlabel, ylabel and legend for
annotation
gtgt title('Demo plot') gtgt xlabel('X Axis') gtgt
ylabel('Y Axis') gtgt legend(pl1, pl2, 'x2',
'x3')
16Graphics - Annotation
17Tabular data
gtgt xlinspace(0, 15, 10) gtgt fidfopen('tabular.tx
t', 'wt') gtgt fprintf(fid, '15s 15s\n', 'x',
'sin(x)') gtgt fprintf(fid,'15.10f 15.10f\n',
x sin(x)) gtgt fclose(fid) gtgt type
tabular.txt x sin(x)
0.0000000000 0.0000000000 1.6666666667
0.9954079578 3.3333333333 -0.1905679629
5.0000000000 -0.9589242747 6.6666666667
0.3741512306
Use fscanf to read data back into Matlab
18Programming MATLAB
Writing an M-File
- function f fact(n) definition
- FACT Factorial. help line
- FACT(N) returns the factorial of N
- usually denoted by N!
- Put simply, FACT(N) is PROD(1N).
- f prod(1n) Function body
- return
19Programming - functions
- Functions have inputs and outputs
- Variables defined in the function isnt defined
after function completes evaluating
function y myaverage(x) m,n
size(x) if (((m 1) (n 1)) (m 1 n
1)) error('Input must be a
vector') end y sum(x)/length(x)
Actual computation return
20Programming - Scripts
- Scripts have no input and output
- List of commands
- Variables are available on completion
- Simple example
theta -pi0.01pi Computations rho
2 sin(5 theta).2 polar(theta,rho)
Graphics output
21Flow control
If/elseif/else switch/case/otherwise
- Logic control structures
- Iterative structures
- Traditional for loop
- Matlab vector for loop
for while
for i110 for j110 a(i,j)b(i,j)c(i,j)
end end
a b.c
22Workspace
- Matlab remembers old commands
- And variables as well
- Each Function maintains its own scope
- The keyword clear removes all variables from
workspace - The keyword who lists the variables
23File I/O
- Matlab has a native file format to save and load
workspaces. Use keywords load and save. - In addition MATLAB knows a large number of
popular formats. Type help fileformats for a
listing. - In addition MATLAB supports C style low level
file I/O. Type help fprintf for more
information.
24For more assistance
- DEAS Matlab help email
- matlab-fall04_at_deas.harvard.edu
- Mathworks web site
- http//www.mathworks.com