



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




















Introduction to MATLAB
Our introduction follows, in part, section 1 of the excellent “An introduction to Matlab for dynamic modeling” by Guckenheimer and Ell- ner: www.cam.cornell.edu/ ∼ dmb/DMBsupplements.html, as well as mate- rial from Prof. Mark Goldman, UC Davis
Launch matlab! Command window (graphing calculator) mode
1+ ans = 4
Assigning values to variables
a=1+ a = 4 Displaying values a a = 4 Suppressing display of output in MATLAB a=1+3;
Arithmetic
/ (divide)
The latter are built-in functions
a = sqrt(5) a = 1. a=2 ; b=4 ; c=aˆb c = 16
There are also some built-in variables
pi ans = 3. cos(pi) ans = -
Variable names:
a1= my_favorite_variable_number_2=
VALID: Letter followed by letters, numbers, un- derscore character. NOTE! capitalization matters. A 6 = a !!
NOT VALID:
my_favorite_variable#2= ??? my_favorite_variable#2= 2a= Error: Unexpected MATLAB expression.
And using periods gives a different, more advanced type of variable (data structure) that we won’t discuss now
my.favorite.variable.name= my = favorite: [1x1 struct]
parenthesis exponentiation multiplication division addition subtraction Say we want: a = 2 , b = 4 , c = (^) a+ab
a=1 ; b=4 ; c=a/a+b c = 5
a=1 ; b=4 ; c=a/(a+b) c = 0. !! When in doubt, put lots of parentheses !!
CHECK: What do you get for the below, and why?
a=1 ; b=4 ; c =0 ; d=a/0+b d=a/(0+b) d=-aˆ d=(-a)ˆ
Representation of numbers in scientific computing: finite precision Standard: IEEE floating point arithmetic. Important feature – finite precision: approx 16 significant digits
Display more digits:
a=0. a =
format long a a = 0.
Roundoff error:
a=4/3 ; b=a-1 ; c = (3*b)-
c =
-2.220446049250313e-
Maximum number: ≈ 10308 Minimum number: ≈ 10 −^308 Overflow:
a=10ˆ a = Inf
Underflow
a=10ˆ- a = 0
Another special number: not defined
ans = NaN
A vector value is just a list of scalar values. Arranged horizontally into a row vector:
~x = (6 12 5). (1) Or vertically into a column vector:
~x =
In MATLAB, row vector:
x=[6 12 5] x = 6 12 5
OR
x=[6 ,12 ,5] x = 6 12 5
In MATLAB, column vector:
x=[6 ; 12 ; 5]
x =
6 12 5
The transpose operation switches between row and column vectors. This is given by dot prime in MATLAB. That is, in MATLAB:
y=x.’
Exercise 0.1 : Have MATLAB compute the values of
Multiplication by scalar
c~x =
x 1 x 2 x 3
cx 1 cx 2 cx 3
; that is, xj → c xj
Matlab *
x=[1;2;3] ; 3*x
ans = 3 6 9
Addition of two vectors
~x + ~y =
x 1 + y 1 x 2 + y 2 x 3 + y 3
Subtraction similar
Works the same for row and column vectors
Matlab + and -
x=[1;2] ; y=[0;-2]; z=x+y z = 1 0 z+ [2 2] ??? Error using ==> plus
Matrix dimensions must agree: add row + row or col + col
CAUTION! Multiplication of vectors does NOT work the same way ... more later (matrix- vec- tor multiplication)
FUNDAMENTAL CONCEPT: Matrix-vector multiplication
( A 1 , 1 A 1 , 2 A 2 , 1 A 2 , 2
x 1 x 2
= x 1
e.g. (^) ( 1 1 1 2
In general,
a 1 · · · an | · · · |
x 1 ... xn
j
xj
aj |
Exercise 0.3 : Compute the below by hand ...
MATLAB * operator
A=[1 1 ; 1 2] ; A*[1 ; 2]
ans =
3 5
In y = Ax, A must have same number of columns as x has rows. Nonsense:
( 1 1 1 2
??? Error using ==> mtimes Inner matrix dimensions must agree.
Exercise 0.4 :
The for loop
Use for repeated operations. Basic structure: MATLAB CODE
for n= 1: disp(n) end
n loop variable 1:3 loop vector disp(n) or print(n) command
Code starts with n equal to first element in loop vector runs command advances n to next element repeats quits when have covered all elements of loop vector
In more detail:
Run and describe what happens for these examples:
for p=[4 6 67 -1] disp(p) end
for k=1:2: disp(k) end
a= for k=1: a=a+k end