Download presentation
Presentation is loading. Please wait.
Published byClementine Stewart Modified over 8 years ago
1
Finishing up Chapter 5
2
Will this code enter the if statement? G=[30,55,10] if G<50 count = count +1; disp(G); end The code inside the if statement is not executed, because the comparison is not true!!
3
For Loops for index = [matrix] commands to be executed end The loop starts with a for statement, and ends with the word end. The first line in the loop defines the number of times the loops will repeat, using an index number. The loop is executed once for each element of the index matrix identified in the first line The index of a for loop must be a variable.
4
Here’s a simple example In this case k is the index – the loop is repeated once for each value of k the index can be defined using any of the techniques we’ve learned
5
While Loops while criterion commands to be executed end While loops are very similar to for loops. The big difference is the way MATLAB decides how many times to repeat the loop. While loops continue until some criterion is met.
6
This loop creates the matrix a, one element at a time
7
Improving the Efficiency of Loops In general, using a for loop (or a while loop) is less efficient in MATLAB than using array operations.
8
Chapter 6: Mathematical Insight and Engineering
9
What is A’? >> A = [1, 2, 3; 4, 3, 6]; >> A’ A.1 4 2 3 3 6 B. 4 1 3 2 6 3 C. 1 2 3 4 3 6 D. 4 3 6 1 2 3 This is the transpose= the rows and columns are flipped
10
What is C? >> A = [1, 2, 3]; >> B = [4, 2, 1]; >> C=dot(A,B) A.1 4 2 2 3 1 B. 11 C. 1 2 3 4 2 1 D. 4 8 3 Dot product = sum of the elements in vector A multiplied by same elements in vector B
11
What is C? >> A = [1, 2, 3; 3, 2, 1]; >> B = [1, 1; 1, 1; 1, 1]; >> C=A*B A.1 4 2 2 3 1 B. 11 C. 6 6 6 6 D. 4 8 3 Matrix multiplication= dot product of each row of A with each column of B
12
Matrix Inverse MATLAB offers two approaches The matrix inverse function inv(A) Raising a matrix to the -1 power A -1
13
A matrix times its inverse is the identity matrix Equivalent approaches to finding the inverse of a matrix
14
Not all matrices have an inverse Called Singular Ill-conditioned matrices Attempting to take the inverse of a singular matrix results in an error statement
15
Determinants Related to the matrix inverse If the determinant is equal to 0, the matrix does not have an inverse The MATLAB function to find a determinant is det(A)
16
What is B? >> A = [1, 2; 3, 2]; >> B=det(A) A.0B. 4 C. -4 D. 8 Don’t have to know the mathematics of the determinent
17
Solutions to Systems of Linear Equations
18
Using Matrix Nomenclature and AX=B
19
We can solve this problem using the matrix inverse approach This approach is easy to understand, but its not the more efficient computationally
20
Matrix left division uses Gaussian elimination, which is much more efficient, and less prone to round-off error
21
What is C? >> A = [1, 0; 0, 1]; >> B = [5; 2] >> C = inv(A)*B A.1 1B. 5 2 C. 5 1 D.5 2 Don’t have to know the mathematics of the determinent
22
In-class assignment – Chapter 6 problems 3, 7, 8, and 10
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.