



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An introduction to MATLAB, a high-level programming language and graphing calculator commonly used in scientific computing. It covers the basics of MATLAB, including arithmetic operations, programming syntax, and variable usage. The document also includes examples and exercises to help readers better understand the concepts.
Typology: Lecture notes
1 / 27
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