160 likes | 269 Views
Introduction to MATLAB. Windows in MATLAB. Command Window – where you enter data, run MATLAB code, and display results Command History - displays a log of the statements run in the Command Window Workspace – displays current variables for use in Command Window
E N D
Windows in MATLAB • Command Window – where you enter data, run MATLAB code, and display results • Command History - displays a log of the statements run in the Command Window • Workspace – displays current variables for use in Command Window • Editor – where you can write your own functions • Note: the way variables are created and functions run in the command window is the same as in the editor
Help Tools in MATLAB • help ____ - type in the command window following function name to get brief description about the function • doc – MATLAB documentation, type in command window and new window will open where you can get more detailed descriptions of functions
Entering Variables • To create a 3-by-3 matrix: A = [1 2 3; 4 5 6; 7 8 9] Press enter and MATLAB gives you: A = 1 2 3 4 5 6 7 8 9 This is stored in your workspace. • A = [1 2 3; 4 5 6; 7 8 9]; This stores A in your workspace but does not print out A in the command window.
Operators • + Addition • - Subtraction • * Matrix multiplication • ^ Matrix power • \ Backslash or left matrix divide • / Slash or right matrix divide • ‘ Transpose • .‘ Nonconjugated transpose • .* Array multiplication (element-wise) • .^ Array power (element-wise) • .\ Left array divide (element-wise) • ./ Right array divide (element-wise)
Running Functions • fncName(input1) • [output1, output2] = fncName(input1, input2) Allows you to assign variable names to the outputs for future use • When you want to run your own functions make sure the current folder contains the file you want to run. • Example: size – gives array dimensions >>A = [1 2 3; 4 5 6]; >>size(A) ans = 2 >>[m,n] = size(A) m = 2 n = 3 Note: If a function is taking too long to run and you want to stop running the function press Ctrl + C
Helpful Functions • clc – clears command window • clear all – clears all variables in workspace • clear ____ - clears the variable ___ from workspace • diary on/off - saves command window text to file • min – gives smallest element in array • max – gives largest element in array • abs – absolute value • tril – lower triangular part of matrix • triu – upper triangular part of matrix • eye – idenity matrix • semilogy - plots data with logarithmic scale for the y-axis
Relational and Logical Operators • == - element-by-element comparison between two arrays. Returns a logical array of the same size, with elements set to logical 1 (true) where the elements are equal, and elements set to logical 0 (false) where they are not equal. • ~= - element-by-element comparison between two arrays. Returns a logical array of the same size, with elements set to logical 1 (true) where the elements are not equal, and elements set to logical 0 (false) where they are equal. • && - logical AND • || - logical OR • ~ - logical NOT
For Loop Example: A = [1 9 3 8 5 5 6 9 8 2]; numelA = numel(A); % number of elements in A for j = 1:numelA-1 A(j) = A(j+1); end A = [9 3 8 5 5 6 9 8 2 2]
if/else/elseif Example: x = 15; if x > 20 x = x - 20; else x = x * 2; end
if/else/elseif Example: A = [1 5 9 2 5 2]; for j = 1:numel(A) if A(j) >= 5 A(j) = 0; elseif A(j) == 1 A(j) = 2; else A(j) = 3; end end
Indexing Example: A = [1 2 3; 4 5 6]; A(2,2) = 5 A(2,:) = 4 5 6 A(:,1) = [1;4]
More Indexing Example(produces same result as on slide 9): A = [1 9 3 8 5 5 6 9 8 2]; A(1:numel(A)-1) = A(2:numel(A)); OR A(1:end-1) = A(2:end);
How to write your own function in the Editor function [x, index] = practiceFunction(A) % PRACTICEFUNCTION Determines if a square matrix has elements greater than 20 % % [x, index] = practiceFunction(A) produces x and index. % x = true if matrix has elements greater than 20 and false if not % index = logical matrix the same size as A that returns true if number % is greater than 20 and false if not numelA = numel(A); [m,n] = size(A); if m ~= n error(‘A must be square!'); end x = false; index = false(m,n); for j = 1:numelA if A(j) > 20 x = true; index(j) = true; else index(j) = false; end end
Writing Functions • File Name has to be the same as function name • MLINT • Debugging • % - comment • ; - prevents the line from being printed in the command window when function is run • Error – error(‘…’); • Print Statement – fprintf(‘The first number is %g.’,A(1)); • Smart Indent