Lectures 16 17 18 Linear Systems and MATLAB 2016 Handouts
Lectures 16 17 18 Linear Systems and MATLAB 2016 Handouts
f = − kx
• Displacement of a
spring when a force is
applied (a linear
relationship for ‘small’
displacements)
Linear Equation
• Linear equation in variables xi
A x = b
Vector and Matrix
Representation
5 x1 − x2 + x 3 =7
−2 x1 + 6 x2 + 9 x3 =0
−7 x1 + 5 x2 − 3 x3 =
−7
5 −1 1 x1 7
−2 6 9 x2 = 0
−7 5 −3 x3 −7
A x = b
3x3 3x1 3x1
Columns
Vector and Matrix Operations
• Asian mathematicians had
already developed
algorithms for solving
linear systems by 250 BC
• Linear algebra as we
know it was first
formalized by German
mathematician Hermann
Grassmann(1809-77)
– This new algebra was
presented in his book Die
Ausdehnungslehre(1844)
– Grassmann algebra
Linear Algebra
• Deals with systems of linear equations and their
solutions, and
• linear transformations and their properties.
Solutions to Linear Systems
Solving a set of a linear equations means we determine
values for the n variables, x1, x2, ..., xn, such that each
of the m equations is satisfied.
There are three possibilities:
• The system has a single unique solution.
– e.g. Two lines meet at a point, if they are not parallel
• The system has infinitely many solutions.
– e.g. Two planes meet in a line, if the planes are not parallel
• The system has no solution.
– e.g. Two planes, or two lines, never meet if they are parallel
Solutions to Linear Systems
Under-determined (m<n) Over-determined (m>n)
• Fewer equations than • More equations than
unknowns unknowns
• If a finite solution exists, it • A finite solution may exist
is not unique – It may, or may not, be unique
• If a solution exists there are
an infinite number of
solutions
2
3 x1 − 2 x2 =
5
5 x1 + 7 x2 =
4
x2
x2
-2
Solution:
-4
-4 -2 0 2 4
x1 = 1.3871
x2 = −0.4194
x1
x1
With Matrices: 3 x1 − 2 x2 =
5
Starting equations:
5 x1 + 7 x2 =
4
3 −2 x1 5
Write as Matrix, A, times a 5 7 x = 4
2
vector, x, equal to a vector b:
Ax=b
Matrix multiply on the left -1 -1
by the inverse of A: A Ax=A b
1 0 -1
-1
A A= = Unit Matrix x=A b
0 1
• The trick is to calculate A-1: the matrix
that is the inverse of the A matrix.
Aside: 3 −2
if A =
5 7
• Do this calculation: 0.2258 0.0645
• Later, once you then A =
-1
know MATLAB, −0.1613 0.0968
calculate A-1.
1 0
Show A A =
-1
0 1
5 x1 − x2 + x 3 =7 80
−2 x1 + 6 x2 + 9 x3 =
60
0 40
x3 data (z)
20
−7 x1 + 5 x2 − 3 x3 =
−7 0
-20 15
10
5
-40 0
(x)
-5
x1 data
-60 -10
10 5 -15
0 -5 -10 -15
x2 data (y)
Example: System of Linear Equations
5 x1 − x2 + x 3 =7
−2 x1 + 6 x2 + 9 x3 =0
−7 x1 + 5 x2 − 3 x3 =
−7
5 −1 1 x1 7
−2 6 9 x = 0
2
−7 5 −3 x3 −7
A x = b
Typical Engineering Application
-1
• Find that satisfies x=A b
Three Planes Intersecting at a Point
80 x1 1.5701
x = 0.7196
60
2
40 x3 −0.1308
x3 data (z)
20
-40
5
0
•Show this yourself later using
(x)
-60 -10
10 5 -15
0 -5 -10 -15
x2 data (y)
Mesh Current Network Analysis
i1 i2 i3
i1 i2 i3
11 −5 0 50
−5 27 −4 0
0 −4 8 0
Row Reduction to Row Echelon Form
11 −5 0 50
• The system of equations
−5 27 −4 0
is written in this form: 0 −4 8 0
Unit Matrix in 3D
Another Linear Circuit Example
• Command Window
– Opens immediately when you start
MATLAB
– Used primarily for mathematical
operations, testing/development,
and for creating throw away
programs
• Figure Window
– Appears whenever a plot is
created
Video Time
1. http://www.mathworks.com/demos/matlab/getting-
started-with-matlab-video-tutorial.html
2. http://www.mathworks.com/demos/matlab/working-
in-the-development-environment-matlab-video-
tutorial.html
3. http://www.mathworks.com/demos/matlab/writing-
a-matlab-program-matlab-video-tutorial.html
4. http://www.mathworks.com/demos/matlab/analyzin
g-data-overview-matlab-video-demonstration.html
Semicolons ;
• Follow commands when you do not want the
output to be printed/echoed to the screen
• Can be used in the command window or in
your m-files (so you see only what you want)
» 1+1
ans =
2
» 2*18;
»
Variables
• You do not need to initialize variables (i.e., no
need to assign data types)
– MATLAB does it automatically
• Can have different types
– Double Precision (default)
– Integers
– Boolean
– Strings
• Can be multidimensional (vectors, matrices)
Variables
• Use the = sign to define variables
Number1 = 412;
% Value 412 is assigned to the variable Number1
Number_1 = -18;
% Note that Number1 and Number_1 are different
Number 2 = 15;% Invalid -> Names cannot have spaces
A1 = [1 2 3; 4 5 6; 7 8 9];% Assigns a matrix A1
AVector = [-1; 0; 1; 0; -1; 0];
2V = [34; 52]; % Invalid: Names must begin with a letter
Displaying Variables in Scripts
disp(Number)
% Will display the contents of the variable Number
in the command window
disp('At dawn, my father and I picked carrots off
of the trees')
% Display the message in between quotations in the
command window
% If you want an apostrophe in the message, you
have to use 2 single quotes: ''
Parentheses
( ) used to denote order of operations or standard
functions
2*(3 + 4) or sin(3.145)
>> A = [5;1;3;4];
>> B = [0;2;4;11];
>> C = A-B
C=
5
-1
-1
-7
Scalar-Vector Operations
>> a = [-1,-5,0,3,7,8];
>> 3*a
ans =
-3 -15 0 9 21 24
>> 1 + a
ans =
0 -4 1 4 8 9
Norm of a Vector
>> a = [-1;-5;0;3;7;8]
• Use the norm a=
command
-1
-5
0
norm of vector a ≡ a 3
7
8
= a + a + a …+ a
2
1
2
2
2
3
2
n >>norm(a)
ans =
12.1655
Cross Product
>> J = [2;1;-3];
>> K = [0;-2;4];
>>cross(J,K)
ans =
-2
-8
-4
>>cross(K,J)
Moment of a force Fb applied at point 'b' ans =
around point 'a' is given as : M=a rab × Fb .
2
8
a × b =a b sin θ n 4
Force on a moving charged particle in a magnetic field is: F=
B qv × B
Dot Product (Inner Product)
• The dot product of two vectors is a scalar
• The vectors must have exactly the same
number of elements
e.g. Mechanical Work, =
W F=
·d F d cos θ
Dot Product Example
Check the following dot product result
x⋅y
cos(θ ) =
x y
x⋅y x1 y1 + x2 y2 +… xn yn
cos(θ )
= =
x y x + x …+ x
2
1
2
2
2
n y + y …+ y
2
1
2
2
2
n
∑ ( xi − x )( yi − y ) 1 n
rxy = i =1 =sx ∑
n − 1 i =1
( xi − x ) 2
(n − 1) sx s y
n
1
=sy ∑
n − 1 i =1
( yi − y ) 2
10
r2 = (-0.9934)2=0.9868
Coefficient of
8
Determination
6
of the fit:
r2 = 0.9868
Y Data
4
-2
0 1 2 3 4 5 6 7 8
X Data
Coefficient of
4
Determination
2
of the fit:
r2 = 0.9868
Y -mean(y)
0
-2
-4
-6
-4 -3 -2 -1 0 1 2 3 4
X -mean(x)
7 3
x − mean( x)= 4 − 4= 0 θ = 173 o
1 -3
6
te
0.8
-1 -5.333
ordina
y − mean( y ) = 5 − 4.333 =0.667
2 0.6
Co
0.4
d
0
9 4.667
ordinate
T hi
0.2
-2
0.0
Second Co
-4
-4 -2 0 2 4
First Coordin
ate
r=1
r=0
r = -1
Recall the Car
Mileage example
Weight
Mileage
Fuel Consumption versus Car Weight
40
35
30
City Mileage (mpg)
25
20
15
10
5
10 20 30 40 50 60
yˆ =
−(0.8 ± 0.1) x + (48 ± 3) [mpg]
r2 0.89 ⇐ (-0.9425) 2
40 15
30 5
City Mileage (mpg)
25 0
20 -5
15 -10
10 -15
5 -20
10 20 30 40 50 60 -15 -10 -5 0 5 10 15 20 25
Car Weight (x 100 lbf) Car Weight - Average Car Weight (x 100 lbf)
Matrix Operations in MATLAB
Full-Colon Operator
• Use a full colon to pick out rows or columns
>> A(:,2)
>> A = [1, 2, 3; 4, 5, 7; 8, 9, 10]
ans =
A=
2
1 2 3 5
4 5 7 9
8 9 10
>> A(3,:)
ans =
8 9 10
Full-Colon Operator
Full-Colon Operator
• Easily create arrays with a fixed step size
>> [1:2:10]
ans =
1 3 5 7 9
>> [0:0.5:2.5]
ans =
1 1 1 1
e =1 + + + + +…
1
1! 2! 3! 4!
x x 2 x3 x 4
e =+
x
1 + + + +…
1! 2! 3! 4!
1 1 1 1
e1 =1 + + + + +…
1! 2! 3! 4!
Nested Loops
~ Not Boolean
& And Logical
| Or
Operators
Relational and Logical Operators
• Used when comparing and selecting values
• Used to determine if an expression will
evaluate to true or false
• Used in if statements and while loops
• Used whenever a logical decision must be
made
If Statements
• Executes the if (expression)
statements if the
expression is true statements
– Use the relational or
logical operators
end
• Otherwise, skips the
statements
• Statements can be any
code or function
– Can have multiple
statements
Notice how you
need to double
apostrophes (‘’)
if (expression)
statements
else
statements
end
Always Include Error Messages!
Formatted Printing: In a string of text written within single quotation marks:
% followed immediately with a formatting code means place the corresponding
value listed at the end of the command in this location in the string using the
specified format.
Format for output: ‘i’ for integer; ‘5.2f’ for floating point number of five characters with 2
digits to the right of the decimal point.
(\r means carriage return, and is the same as \n which means new line)
Elseif (and MATLAB Help)
You could write the
program on the previous
slide with ‘else’ and ‘if’
statements instead of
‘elseif’, but you would
need another ‘end’
statement
While Loops
• Repeat a set of
commands while a while expression
condition holds true (1) statements
– Tests the condition before
action is taken end
• Exits when the condition
is found false (0)
• Used when you do not know how many times
you will loop
• While loops carry an implicit danger: there is no
guarantee, in general, that you will exit a while
loop.
While Loops
a = 2; • Loop while the square
b = a^2; of a number is less than
a value of 2000
while b< 2000
• Find the largest even
a = a + 2;
integer with a square
b = a^2; less than 2000
end
a = a - 2;
disp(a)
Changed to
largest odd
integer whose
square is less
than 2000.
The example codes so far were all
written as script m-files
• Now we will look at function files
• In this case we create out own functions:
– for instance, the computer has built-in
functions like sin(x), cos(x), tan(x), log(x),
which take an input, x, and return a value
depending on what the function does.
• Our functions will be similar. They will
have input(s), do something with the
input(s), and output some value(s).
Function m-Files
• Syntax is given by:
function [out1, out2, ...]
=funname(input1, input2, ...)
• The function name must be the same as the
file name (funname.m)
– The m-file begins with the function declaration
• Output arguments are specified in square
brackets
• Use the function from the command prompt
or in a script file, just like any other MATLAB
function
Notice the use of the
dot in the formula