Introduction to MATLAB MATLAB is a mathematics program

Introduction to MATLAB • MATLAB is a mathematics program that works by either typing in commands or by writing programs • Typing x=5*sqrt(83)/(5^3+exp(. 2)) will result in x =. 3609 • MATLAB handles matrix mathematics and complex number arithmetic with ease 6/4/2021 Intro. Matlab. ppt 1

Matlab’s Workspace • Each command or each program creates a set of variables with values in the workspace • Values are kept until you no longer need them – Use clear to wipe the workspace clean – Use clear x to eliminate the variable x – Use clear variables to clear all variables – Clear the screen of text by using clc on the command line or choosing Clear Session under Edit in the menu. This does not clear the variables. 6/4/2021 Intro. Matlab. ppt 2

Matlab’s Workspace • You can check your variables at any time – who generates a listing of all variables – whos generates a listing of all variables plus the size, bytes and class of the variables • Choosing Show Workspace under the File menu brings up a window showing the same information as whos) – Type help who for more detailed information – Type the variable to see its value(s) 6/4/2021 Intro. Matlab. ppt 3

Matlab Built-In Functions • Matlab commands and functions are broken down into many categories • Type help elfun – trigonometric functions – exponential functions – complex number functions – rounding/remainder functions 6/4/2021 Intro. Matlab. ppt 4

Common Math Functions • Trigonometric – cos, sin, tan, atan – Be sure to use radians in the argument • • • Square root – sqrt(2) yields 1. 414 Natural Log – log(3) yields 1. 0986 Base 10 Log – log 10(4) yields. 6021 Base 2 Log – log 2(4) yields 2 Exponential – exp(2) yields e 2 = 7. 3891 6/4/2021 Intro. Matlab. ppt 5

Complex Number Functions • Enter in rectangular form x=4+3 j or x=4+3 i or x=complex(4, 3) • Magnitude – abs(x) yields 5 • Phase angle – angle(x) yields. 6435 radians • Complex conjugate – conj(x) yields 4 -3 j • Real part – real(x) yields 4 • Imaginary part – imag(x) yields 3 6/4/2021 Intro. Matlab. ppt 6

Matlab – Matrix Math • Matrices are rectangular arrays of numbers • Matlab uses matrix math to simplify many calculations • Each element in a matrix can be accessed using its row and column – m(row, column) • An entire row or column can be accessed to form a vector – m( : , column) or m(row, : ) 6/4/2021 Intro. Matlab. ppt 7

Generating a Vector • Generate a vector by listing its values – Rowvector=[1 2 3 4 5 6 7 8 9] – Columnvector=[1; 2; 3; 4; 5; 6; 7; 8; 9] – the semicolon starts a new row • Create a row vector with a constant interval – T=start value : increment : end value T=0 : 2 : 10 gives T=[0 2 4 6 8 10] – T=linspace(start value, end value, number wanted) T=linspace(0, 10, 5) gives T=[0 2. 5 5 7. 5 10] 6/4/2021 Intro. Matlab. ppt 8

Generating a Vector • You can create a row vector with logarithmic spacing – T=logspace(starting power of 10, ending power of 10, number of values wanted) – T=logspace(0, 2, 4) gives T=[1 4. 6416 21. 5443 100] 6/4/2021 Intro. Matlab. ppt 9

Generating a Matrix • You can create a matrix by entering each row with semicolons between each row • A = [3 4 – 7 ; 5 – 7 3 ; 2 0 – 6] 3 4 -7 A = 5 – 7 3 2 0 – 6 6/4/2021 Intro. Matlab. ppt 10

Matrices and Sets of Equations • Matrices can be used to represent a system of equations and solve it 3 x+4 y-7 z=10 5 x-7 y+3 z=-6 2 x-6 z=6 • The matrix equation implements the above system 3 4 -7 x 10 5 – 7 3. y = -6 2 0 – 6 z 6 6/4/2021 or A * S = C Intro. Matlab. ppt 11

Matlab Solution of A*S=C A=[3 4 – 7 ; 5 – 7 3 ; 2 0 – 6] C=[10 ; -6 ; 6] Premultiply both sides by the inverse of A inv(A) * A * S = inv(A) * C Which simplifies to: S = inv(A) * C 0. 3140 or x =. 3140 Results in S = 0. 6977 or y =. 6977 -0. 8953 or z = -. 8953 6/4/2021 Intro. Matlab. ppt 12

Running Matlab Programs • Under file, select open • Find the M-file that you want to run – The M-file should appear in the editor window • Modify the M-file as needed • Under debug, choose run – Results should be displayed in the main window – Plots should be displayed in new figure windows 6/4/2021 Intro. Matlab. ppt 13
- Slides: 13