Lecture 5: MATLAB matrices and iterations / Learning Styles
Professor Erik Cheever
Remember… 9/29: first ASME meeting tonight (Tues.) at 6:00pm, Hicks lounge 10/1: Matrix worksheet is due – turn in paper copy in lab. 10/8: Lab 4/5 – MATLAB 2 – submitted on moodle 10/8: Learning styles questionnaire (details to come). 10/22: MATLAB 3 – submitted on moodle
The big picture… Today: MATLAB and matrices / Matrix applications Today: Nancy Burkett, Career Services Lab – Soldering and MATLAB. Next Several weeks – some more SolidWorks, .
MATLAB and matrices Define a matrix 1 6A
2 1
= −
>> A = [1 6; -2 1] % rows of matrix separated by semicolon. A = 1 6 -2 1 >> A = [1 6; % ... Or rows on separate lines. -2 1] A = 1 6 -2 1
Transpose of matrix swaps rows and columns >> transpose(A) ans = 1 -2 6 1 First row of A = [1 6] is now first column
Elements of a matrix >> A=[1 2 3 4 5; 6 7 8 9 10; -1 -2 -3 -4 -5] %define A A = 1 2 3 4 5 6 7 8 9 10 -1 -2 -3 -4 -5 >> size(A) % get size (3 rows by 5 columns) ans = 3 5 >> size(A,2) % get number of columns in A (second dimension) ans = 5 >> A(2,3) % get element of A in 2nd row, 3rd column ans = 8 >> A(3,2) % Third row, second column ans = -2 >> A(4,5) % Fourth row, fifth column Index exceeds matrix dimensions.
Parts of a Matrix >> A(2,1:5) % get entire second row ans = 6 7 8 9 10 >> A(2,1:end) % get entire second row of A ans = 6 7 8 9 10 >> A(2,:) % get entire second row of A ans = 6 7 8 9 10 >> A(2,1:size(A,2)) % get entire second row of A ans = 6 7 8 9 10 >> A(:,3) % All rows, 3rd column ans = 3 8 -3 >> A(2:3,2:4) % ? ans = 7 8 9 -2 -3 -4
A = 1 2 3 4 5 6 7 8 9 10 -1 -2 -3 -4 -5
Inverse and Identity Matrix Define a matrix Find inverse (don’t worry about how) Recall
>> A = [1 -1; 1 2] A = 1 -1 1 2 >> B = inv(A) B = 0.6667 0.3333 -0.3333 0.3333 >> A*B ans = 1.0000 0 0.0000 1.0000 >> B*A ans = 1.0000 -0.0000 0 1.0000
1 11 2 −
=
A
12 1
3 31 13 3
− = = −
B A
= = =
1 00 1
AB I BA
Aside: Computers have finite precision Define a new matrix Define identity matrix Calculate difference between C and I (it should be zero).
>> C = B*A C = 1.0000 -0.0000 0 1.0000 >> I = eye(2) % Use MATLAB function I = 1 0 0 1 >> C-I ans = 1.0e-15 * 0 -0.1110 0 0
= ∗C B A
1 00 1
=
I
Solving Simultaneous (linear) Equations Consider two equations with two unknowns
To solve, we could add equations
Then plug result into one of the equations
So the solution to our equations is x1=5, x2=4.
This quickly becomes tedious, especially for larger problems.
It is also not very systematic, so it is difficult for computers.
1 2
1 2
x x 9x 2x 13− − = −
+ =
1 2
1 2
2
x x 9x 2x 13
x 4
− − = −
+ ==
1 1x 4 9, x 5− − = − =
Solving Simultaneous Equations with Matrices Consider two equations with two unknowns
Express as a matrix equation
Or, generically
Pre-multiply by A-1.
Use fact that A-1A = I
Use fact that Ix = x.
1 2
1 2
x x 9x 2x 13− − = −
+ =
1
2
x1 1 9x1 2 13 − − −
=
1
2
x1 1 9A , x , bx1 2 13
=
− − −= = =
Ax b
1 1− −=A Ax A b
1−=Ix A b
1−=x A b Done!
Solving Simultaneous Equations with MATLAB Express as a matrix equation
Define matrices
Solve
1 2
1 2
1
2
1
21
x x 9x 2x 13
x1 1 9x1 2 13
x1 1 9A , x , bx1 2 13
−
− − = −
+ =
− − −=
=
− − −= = =
=
Ax b
x A b
>> A = [-1 -1; 1 2]; >> b=[-9;13] b = -9 13 >> x=inv(A)*b x = 5 4
Example: for i=1:10, disp(i) end
Output: 1 2 3 4 5 6 7 8 9 10
Syntax: for variable=start:finish, …commands… end
The for… loop with specified increment
Example: for i=1:4:10, disp(i) end
Output: 1 5 9
Syntax: for variable=start:increment:finish, …commands… end
For loop example (1)
Make circle move across screen % Make a circle move across the screen T=0.25; % Define delay between moves for y0=-15:2.5:15 plot(0,y0,'ro','MarkerFaceColor','b') % Plot circle axis(20*[-1 1 -1 1],'square'); % Set axis limits (and square axes) pause(T) end
For loop example (2)
Make the shape move (another method): % Make a circle move across the screen (different technique) T=0.25; % Define delay between moves y0 = -15:2.5:15; % define a vector of y0's for i=1:length(y0) % i is index into y0. plot(0,y0(i),'ro') % Plot circle axis(20*[-1 1 -1 1],'square'); % Set axis limits (and square axes) pause(T) end
For loop example (3)
Make the shape move by giving it velocity: vy=5; % initial velocity in y dt=0.2; % Time step y0=-10; % Initial position for t=0:dt:10 % t is time variable (from 0 to 10 by 0.2) plot(0,y0,'ro') % Plot circle title(num2str(t)) % Convert "t" to string and use as title axis(20*[-1 1 -1 1],'square'); % Set axis limits (and square axes) pause(dt) y0 = y0 + vy*dt; % update position. end (In a time, dt, the ball moves a distance of vy*dt.
We add that to the current location to get the next location.)
For loop example (4)
Add gravity: %% Add gravity vy=25; % initial velocity in y dt=0.2; % Time step g = 9.8; % acceleration due to gravity. y0=-10; % Initial position for t=0:dt:10 % t is time variable (from 0 to 10 by 0.2) plot(0,y0,'ro') % Plot circle title(num2str(t)) % Convert "t" to string and use as title axis(20*[-1 1 -1 1],'square'); % Set axis limits (and square axes) pause(dt) vy = vy - g*dt; % update velocity y0 = y0 + vy*dt; % update position. end
ENGINEERING 5�The big picture…MATLAB and matricesElements of a matrixParts of a MatrixInverse and Identity MatrixAside: Computers have finite precisionSolving Simultaneous (linear) EquationsSolving Simultaneous Equations with MatricesSolving Simultaneous Equations with MATLABSlide Number 11The for… loop with specified incrementFor loop example (1)For loop example (2)For loop example (3)For loop example (4)