This preview shows page 1. Sign up to view the full content.
Unformatted text preview: Engineering 101 Quote of the Day An eye for an eye makes the whole world blind.
 Mahatma Gandhi Matrices in MATLAB Accessing Individual Vector Elements
You can access specific locations in a vector just like C++:
a = [0 1 2 3 4 5 6 7 8 9] a(1) [0] a(8) [7] a(1:5) [0 1 2 3 4] a(4:10) [3 4 5 6 7 8 9] a(3:3:10) [2 5 8] Shortcut Expressions for Matrix Initialization
If you want a matrix to have all zeros or all ones you can use a builtin function. builtzeros(n) zeros(n, m) ones(n) ones(n, m) eye(n) eye(n, m) creates an n x n matrix of zeros creates an n x m matrix of zeros creates an n x n matrix of ones creates an n x m matrix of ones creates an n x n identity matrix creates an n x m identity matrix Shortcut Expressions for Matrix Initialization
You can use the size of a previous matrix as a template for a new matrix length(arr) returns the length of a vector or the longest dimension of a matrix size(arr) returns the rows and columns of an array x = [1 2 3; 4 5 6]; y = zeros(size(x)); Getting Data From the User
You can use the input function to prompt the user for input. val = input(`Enter a number:'); The user can enter a number or an array in brackets If you need to get a string then val = input(`Enter a string:', `s'); 1 Exercise
After these commands b=? a = [7:2:11]' b = [a a a] 127 7 9 11 7 9 11 9 9 9 9 11 7 9 11 11 11 11 7 9 11 7 9 11 Exercise
After these commands b=? b = 3 * eye(4); b(1, 4) = 3; b(4, 1) = 5; 4 7 9 11 7 9 11 7 9 11 1 3 0 0 5 0 3 5 0 0 3 3 0 3 0 0 3 2 3 0 0 3 0 3 3 0 0 5 3 0 5 0 0 3 3 3 0 0 3 0 3 0 0 0 0 3 0 5 0 0 3 4 3 0 0 5 0 3 0 0 0 0 3 0 3 0 0 3 3 7 7 7 Scalar Operations
The following operations are defined between two scalars: Addition Subtraction Multiplication Division Exponentiation a+b ab a*b a/b a^b Scalar / Array Operations
The same operations may be performed between an array and a scalar. In this case the operation is performed to every element of the array. Array vs. Matrix operations
When two arrays are being operated on MATLAB makes a distinction between array an matrix operations. Matrix operations are the standard Linear Algebra operations Array operations are done on an elementbyelementbyelement basis Array vs. Matrix operations
Array or Matrix operations (no difference)
Addition Subtraction Multiplication Right Division Left Division Exponentiation a+b ab a .* b a ./ b a .\ b .\ a .^ b Array (Elementbyelement) operations (Elementby 2 Exercise Array vs. Matrix operations
Matrix operations
Multiplication Right Division Left Division a*b a/b b\a Match each expression to the matrix that corresponds to the result of the operation. a = [1 2 ; 5 1 ] b = [0 1; 1 2 ] Multiplication does standard matrix multiplication. The columns of a must equal the rows of b. Division inverts the denominator matrix and multiplies by the numerator. 1 a+b
A1 4 1 3 2 ab
B0 5 2 2 3 a.*b
C2 1 4 a*b
5 3 D1 6 3 1 Matrix operations
Recall that we often want to solve problems where: [A] x = b That is where [A] is a matrix and b is a set of known quantities and we want to know x. We can solve this using MATLAB by the command: x=b/A MFiles: MATLAB Programs
We can solve our statics problem by creating an Mfile. Once we create an Mfile we can execute it by Msimply typing the name of the Mfile leaving off Mthe ".m" Comments in Mfiles are preceded by a % sign. M Recall our General Statics Problem
W1 T1 T5 W2 y x T4 W/2 Convert to a Matrix
We were able to convert these equations into a matrix which could be solved by computer.
1 1/2 1/ 0 1/2 1/ 0 0 0 0 1 0 0 0 0 1/2 1/ 0 1/2 1/ 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1/2 1/ 0 1/2 1/ 1 1/2 1/ 0 1/2 1/ 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 T1 0 T2 0 T3 0 T4 0 T5 = 0 T6 0 W1x 0 W1y W/2 W2x 0 W2y W/2 T1 + W1x = 0  T1  T5 / 2 + T2 / 2 = 0 W1y = 0  T5 / 2  T6  T2 / 2 = 0
W2 x + T4 + T5 / 2 = 0 T6 T3 W/2 OPEN 24 HOURS T2 W2 y + T5 / 2 = 0 T3  T4 = 0  W / 2 + T6 = 0
 T3  T2 / 2 = 0  W / 2 + T2 / 2 = 0 3 cs = 1/sqrt(2.0); labels= ['T1 ';'T2 ';'T3 ';'T4 ';'T5 ';'T6 ';'W1x';'W1y';'W2x';'W2y`]; matrix = [ 1 0 0 0 1 0 0 0 0 0 cs cs 0 0 0 0 cs cs 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1 0 0 0 0 0 1 0 cs cs cs cs 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 ; ; ; ; ; ; ; ; ; ]; Script Files
A script file is created by typing your command line inputs into a file and saving it with a .m suffix (multi_plots.m) (multi_plots.m) Variables in a script file have global scope Variables in a function file have local scope W = 1.0; b = [ 0 0 0 0 0 0 0 W/2.0 0 W/2.0 ]'; sol = matrix\b; strcat(labels, ' = ', num2str(sol)) Input
Define the variable in the command window prior to running the script file:
>> score1 = 50; >> score2 = 75; >> score3 = 100; >> calculateAverage average = 75
% This script file calculates the average % of three values that are defined in the % command window average=(score1+score2+score3) / 3 Input
Define the variable inside the script file. It will remain "active" after the script file is finished.
>> calculateAverage average = 75
% This script file calculates the average % of three values that are defined in the % script file score1 = 50; score2 = 75; score3 = 100; average=(score1+score2+score3) / 3 display_test.m Input
Ask for a value in the script using:
>> calculateAverage Gimme score1! Gimme score2! Gimme score3! average = 75 50 75 100
% This script file calculates the average % of three values that are input by the user score1 = input(`Gimme score1! '); score2 = input(`Gimme score2! '); score3 = input(`Gimme score3! '); average=(score1+score2+score3) / 3 Output
display( ) is called when you don't include a semicolon after a MATLAB line where an assignment occurs disp( data ) will output only the data to the screen. Unlike display( ), disp( ) does not put a ), "data =" message before the output. 4 Next Lecture
Function Files and Managing Data 5 ...
View Full
Document
 Spring '07
 JeffRingenberg

Click to edit the document details