Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to MATLAB. Windows in MATLAB Command Window – where you enter data, run MATLAB code, and display results Command History - displays a log.

Similar presentations


Presentation on theme: "Introduction to MATLAB. Windows in MATLAB Command Window – where you enter data, run MATLAB code, and display results Command History - displays a log."— Presentation transcript:

1 Introduction to MATLAB

2 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

3 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

4 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.

5 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)

6 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

7 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

8 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

9 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]

10 if/else/elseif Example: x = 15; if x > 20 x = x - 20; else x = x * 2; end

11 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

12 Indexing Example: A = [1 2 3; 4 5 6]; A(2,2) = 5 A(2,:) = 4 5 6 A(:,1) = [1;4]

13 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);

14 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

15 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


Download ppt "Introduction to MATLAB. Windows in MATLAB Command Window – where you enter data, run MATLAB code, and display results Command History - displays a log."

Similar presentations


Ads by Google