Presentation is loading. Please wait.

Presentation is loading. Please wait.

ENGR-25_MATLAB_OverView-1.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

Similar presentations


Presentation on theme: "ENGR-25_MATLAB_OverView-1.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical."— Presentation transcript:

1 ENGR-25_MATLAB_OverView-1.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical & Mechanical Engineer Engr/Math/Physics 25 Chp1 MATLAB OverView: Part-1

2 ENGR-25_MATLAB_OverView-1.ppt 2 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Learning Goals Turn On MATLAB and use as a calculator Create Basic Cartesian Plots Write and Save simple Script Program-files Execute Conditional Statements IF, THEN, ELSE, >, =, etc. Execute Loop Statements FOR & WHILE

3 ENGR-25_MATLAB_OverView-1.ppt 3 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods MATLAB Environment TWO Interaction Modes INTERACTIVE –Type in the COMMAND WINDOW –Often Called a Command-Window Session –Interaction is NOT Saved to Disk Commands (NOT results) Stored in Command History Buffer Window STORED Two Types –SCRIPT Files –FUNCTION Files

4 ENGR-25_MATLAB_OverView-1.ppt 4 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods MATLAB Command Window

5 ENGR-25_MATLAB_OverView-1.ppt 5 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Example Cmd Window Session >> %Use MATLAB As Calculator >> 17*19 ans = 323 >> 77/ ans = >> 64^(1/3) + 32^0.2 ans = 6 >> (7+11)*2.5 ans = 45 >> L = 14.4 L = >> W = 13.3 W = >> Area = L*W Area =

6 ENGR-25_MATLAB_OverView-1.ppt 6 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Script/Function File Editor

7 ENGR-25_MATLAB_OverView-1.ppt 7 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Script & Function Files (m-files) SCRIPTS and FUNCTIONS in MATLAB are stored in text files that end with the extension.m These files are called m-files SCRIPTS (a.k.a. programs) Scripts files are useful for automating tasks that may need to be repeated. They have no input/output parameters They can (but probably shouldnt) share variables with the command workspace

8 ENGR-25_MATLAB_OverView-1.ppt 8 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Script & Function Files (m-files) SCRIPTS (cont.) Scripts are sequences of interactive statements stored in a file –i.e., They look liked Stored versions of Command Window Sessions FUNCTIONS (a.k.a. subroutines) Function m-files are MATLAB subprograms analogous to FORTRAN Subroutines, or C functions They communicate with the command window and other functions via a list of INPUT and OUTPUT PARAMETERS or ARGUMENTS

9 ENGR-25_MATLAB_OverView-1.ppt 9 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Script & Function Files (m-files) FUNCTIONS (cont.) Functions COMMUNICATE with the COMMAND WINDOW and other m-files via a list of input and output variables LOCAL variables are variables defined INSIDE the function –They only can be used inside the function in which they reside. The number of output parameters used when a function is called must match the number of outputs that the function is expected to return

10 ENGR-25_MATLAB_OverView-1.ppt 10 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Entering Commands & Expressions MATLAB retains your previous keystrokes. Use the up-arrow () key to scroll back through the commands. Press the key () once to see the previous entry, and so on. Use the down-arrow () key to scroll forward. Edit a line using the left () & right () arrow keys the Backspace key, and the Delete key. Press the Enter key to execute the command

11 ENGR-25_MATLAB_OverView-1.ppt 11 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Arithmetic Scalar Operations SymbolOperation MATLAB ^ exponentiation: a b a^b * multiplication: ab a*b / right division: a/b a/b \ left division: b/a a\b + addition: a + b a + b - subtraction: a - b a - b LEFT-Division A\b read from Right-to-Left as: b divided by A

12 ENGR-25_MATLAB_OverView-1.ppt 12 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Math Op Precedence (PEMDAS) Precedence Operation FirstParentheses, evaluated starting with the innermost pair. SecondExponentiation, evaluated from left to right. ThirdMultiplication and Division with EQUAL precedence, evaluated from left to right. FourthAddition and Subtraction with EQUAL precedence, evaluated from left to right.

13 ENGR-25_MATLAB_OverView-1.ppt 13 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Precedence Examples >> 8+3*5 ans = 23 >> 8 + (3*5) ans = 23 >>(8 + 3)*5 ans = 55 >> 4^2-12-8/4*2 ans = 0 >> 4^2-12-8/(4*2) ans = 3 4 1

14 ENGR-25_MATLAB_OverView-1.ppt 14 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Precedence Examples cont. >> 3*4^2 + 5 ans = 53 >>(3*4)^2 + 5 ans = 149 >>27^(1/3) + 32^0.2 ans = 5 >>27^1/3 + 32^0.2 ans =

15 ENGR-25_MATLAB_OverView-1.ppt 15 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods = Assignment Operator Typing x = 3 ASSIGNS the value 3 to the variable x. We can then type x = x + 2. This assigns the value = 5 to x. But in algebra this implies that 0 = 2. In algebra we can write x+2 = 20, but in MATLAB we cannot. In MATLAB the LEFT side of the = operator MUST be a SINGLE variable. The Right side must be a computable value

16 ENGR-25_MATLAB_OverView-1.ppt 16 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Work Session Commands CommandDescription clc Clears the Command window clear Removes all variables from memory clear v1 v2 Removes the variables v1 and v2 from memory exist(var) Determines if a file or variable exists having the name var quit Stops MATLAB

17 ENGR-25_MATLAB_OverView-1.ppt 17 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Work Session Commands cont.1 CommandDescription who Lists the variables currently in memory whos Lists the current variables and sizes, and indicates if they have imaginary parts. : (Colon) Generates an array having regularly spaced elements, (Comma) Separates elements of an array ; (Semicolon) Suppresses screen printing; also denotes a new row in an array … (Ellipsis) Continues a line

18 ENGR-25_MATLAB_OverView-1.ppt 18 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods whos on First???

19 ENGR-25_MATLAB_OverView-1.ppt 19 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Special VARS & consts CommandDescription ans Temporary variable containing the most recent answer eps Specifies the accuracy of floating point precision i,j The imaginary unit (-1) Inf Infinity (unbounded magnitude) NaN Indicates an undefined numerical result; a.k.a., Not a Number pi The number pi ( ) NaN returns the IEEE arithmetic representation for Not-a-Number (NaN). These result from operations which have undefined numerical results;. e.g., try Q = 0/0

20 ENGR-25_MATLAB_OverView-1.ppt 20 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods The Complex Plane Im (i or j) Re

21 ENGR-25_MATLAB_OverView-1.ppt 21 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Complex-Number Operations The number c1 = 1 – 2i is entered as: c1 = 1­2i or c1 = 1-2j An Asterisk is NOT needed between i or j and a NUMBER, although it is required with a VARIABLE, such as c2 = 5 - i*c1. Be careful. The expressions y = 7/2*i and x = 7/2j give two DIFFERENT results: y = (7/2)i = 3.5i and x = 7/(2j) = –3.5j

22 ENGR-25_MATLAB_OverView-1.ppt 22 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Complex Arithmetic >> Im_Pwr = Z1^3.84 Im_Pwr = e e+004i >> e_to_Z = exp(Z2) e_to_Z = e e+007i >> ln_Z = log(Z1) ln_Z = i >> Log_Z = log10(Z2) Log_Z = i

23 ENGR-25_MATLAB_OverView-1.ppt 23 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Special VARS & consts CommandDescription format short Four decimal digits (the default); format long 16 digits; format short e Five digits (four decimals) plus exponent; e+03 format long e 16 digits (15 decimals) plus exponent; e–04

24 ENGR-25_MATLAB_OverView-1.ppt 24 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Discrete Math Funtions CommandDescription factor(n) Returns a row vector containing the prime factors of n. gcd(m,n) Finds the Greatest Common Divisor/Factor of m & n lcm(m,n) Finds the Least Common Multiple for m & n factorial(n) Returns the factorial of n ; i.e., returns n ! = 1*2*3…(n-2)*(n-1)*n primes(n) Finds all prime numbers less than n isprime(n) Determines if n is a prime number

25 ENGR-25_MATLAB_OverView-1.ppt 25 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Discrete Math Examples factor777 = factor(777) factor777 = GCF = gcd(1001, 1105) GCF = 13 F7 = factorial(7) F7 = 5040 P93 = primes(93) P93 = Columns 1 through Columns 13 through

26 ENGR-25_MATLAB_OverView-1.ppt 26 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Arrays An ARRAY is an ORDERED SET of Numbers of with n DIMENSIONS A regular Number (a SCALAR) is an Array of Dimension ZERO a VECTOR is a 1-Dim Array a MATRIX is an ARRAY of Dim 2 with special properties

27 ENGR-25_MATLAB_OverView-1.ppt 27 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Arrays in MATLAB The numbers 0, 0.1, 0.2, …, 10 can be assigned to the array variable u by typing u = [0:0.1:10] To compute w = 5 sin u for u = 0, 0.1, 0.2, 0.3, 0.4,…, 10, the command session is; >>u = [0:0.1:10]; >>w = 5*sin(u); The single line, w = 5*sin(u), computed the formula, w = 5 sin(u), 101 times.

28 ENGR-25_MATLAB_OverView-1.ppt 28 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Array Index >>u(7) ans = >>w(7) ans = Use the LENGTH function to determine how many values are in an array. >>m = length(w) m = 101

29 ENGR-25_MATLAB_OverView-1.ppt 29 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Polynomial Roots MATLAB has a Way-Cool Polynomial Root Finder Find the roots of x 3 7x x 34 = 0 >>a = [1,-7,40,-34]; >>roots(a) ans = i i The roots are x = 1 and x = 3 ± 5i

30 ENGR-25_MATLAB_OverView-1.ppt 30 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 5 th Order Polynomial Find the roots of the 5 th Order function >> r5 = [1,-9,35,-65,64,-26]; >> roots(r5) ans = i i i i The roots of g(y) y 1,2 = 3 ± 2j y 3,4 = 1 ± j y 5 = 1

31 ENGR-25_MATLAB_OverView-1.ppt 31 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Common Math Functions FcnMATLABFcnMATLAB exex exp(x) sin x sin(x) x sqrt(x) tan x tan(x) ln x log(x) cos -1 x acos(x) log 10 x log10(x) sin -1 x asin(x) cos x cos(x) tan -1 x atan(x) Note that MATLAB Trig functions Operate on RADIANS Convert using Ratio: -rads per 180°

32 ENGR-25_MATLAB_OverView-1.ppt 32 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods The d Trig Comands for Degrees >> T1 = sind(77) T1 = >> T2 = cosd(19) T2 = >> T3 = tand(53) T3 = >> T4 = asind(.497) T4 = >> T5 = acosd(0.629) T5 = >> T6 = atand(1.73) T6 =

33 ENGR-25_MATLAB_OverView-1.ppt 33 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Printing From Command Window - 1 Note: MATLAB Comments Start with the % Sign Text to Print

34 ENGR-25_MATLAB_OverView-1.ppt 34 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Printing From Command Window - 2 SELECT Text to Print

35 ENGR-25_MATLAB_OverView-1.ppt 35 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Printing From Command Window - 3 Send to printer from Print Dialog Box Caveat In a COMMAND WINDOW session once you Hit Enter ( ) you can NOT Go back to Edit the Text –Can Save your command sequence as an m-file SCRIPT

36 ENGR-25_MATLAB_OverView-1.ppt 36 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Alternative Cmd Window Printing Perform MATLAB Operation Select Desired Text COPY text to the Windows Paste Buffer Open Text application MSWord, WordPad, NotePad, etc. PASTE the MATLAB Text Into the Text Processor Print from the Text Processor as Usual

37 ENGR-25_MATLAB_OverView-1.ppt 37 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods DIARY Function to Record Cmnds Keeping a Session Log The diary Function The diary function creates a copy of your session in MATLAB on a disk file, including keyboard input and system responses, but excluding graphics. You can view and edit the resulting text file using any text editor, such as the MATLAB Editor. To create a file on your disk called sept23.out that contains all the functions you enter, as well as output from MATLAB, enter –diary('sept23.out') To stop recording the session, use –diary('off') To view the file, run edit('sept23.out')

38 ENGR-25_MATLAB_OverView-1.ppt 38 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Command Execution Hierarchy When you type problem1 1.MATLAB first checks to see if problem1 is a variable and if so, displays its value. 2.If not, MATLAB then checks to see if problem1 is one of its own commands, and executes it if it is. 3.If not, MATLAB then looks in the current directory for a file named problem1.m and executes problem1 if it finds it. 4.If not, MATLAB then searches the directories in its search path, in order, for problem1.m and then executes it if found.

39 ENGR-25_MATLAB_OverView-1.ppt 39 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods System, Directory, File Cmnds CommandDescription addpath dirname Adds the directory dirname to the search path. cd dirname Changes the current directory to dirname dir Lists all files in the current directory dir dirname Lists all the files in the directory dirname path Displays the MATLAB search path pathtool Starts the Set Path tool HINT: Consider putting ALL your m-files in ONE Folder/Directory

40 ENGR-25_MATLAB_OverView-1.ppt 40 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Plotting with MATLAB Plot over 573°

41 ENGR-25_MATLAB_OverView-1.ppt 41 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods MATLAB Plotting Commands CommandDescription plot(x,y) Generates a plot of the array y versus the array x on rectilinear axes title(text) Puts text in a title at the top of the plot xlabel(text) Adds a text label to the horizontal axis (the abscissa). ylabel(text) Adds a text label to the vertical axis (the ordinate). grid Puts grid lines on the plot gtext(text) Enables placement of text with the mouse

42 ENGR-25_MATLAB_OverView-1.ppt 42 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods DeskTop Recovery to UnScramble the DeskTop

43 ENGR-25_MATLAB_OverView-1.ppt 43 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods DeskTop Recovery

44 ENGR-25_MATLAB_OverView-1.ppt 44 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Example Problem 1-21 Plot This Function Where –T Temperature (°C) –t time (minutes) For: 1 t 3

45 ENGR-25_MATLAB_OverView-1.ppt 45 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods All Done for Today Tutorial on HomeWork Construction Next Time

46 ENGR-25_MATLAB_OverView-1.ppt 46 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical & Mechanical Engineer Engr/Math/Physics 25 Appendix

47 ENGR-25_MATLAB_OverView-1.ppt 47 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Example Demo Session >> %Use MATLAB As Calculator >> 17*19 ans = 323 >> 77/ ans = >> 64^(1/3) + 32^0.2 ans = 6 >> (7+11)*2.5 ans = 45 >> L = 14.4 L = >> W = 13.3 W = >> Area = L*W Area =

48 ENGR-25_MATLAB_OverView-1.ppt 48 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Prob 1-21 Command Script From the Command Window >> t = [1:0.02:3]; >> T = 6*log(t) - 7*exp(0.2*t); >> plot(t,T), xlabel('time (min)'),ylabel('Temperature (°C)'), title('Problem 1-21'), grid

49 ENGR-25_MATLAB_OverView-1.ppt 49 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Prob 1-22 Plot

50 ENGR-25_MATLAB_OverView-1.ppt 50 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

51 ENGR-25_MATLAB_OverView-1.ppt 51 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods System, Directory, File Cmnds CommandDescription pwd Displays the current directory cd dirname Changes the current directory to dirname rmpath dirname Removes the directory dirname from the search path. what Lists the MATLAB-specificfiles found in the current working directory. Most data files and other non-MATLAB files are not listed. Use dir to get a list of all files what dirname Lists the MATLAB-specific files in directory dirname

52 ENGR-25_MATLAB_OverView-1.ppt 52 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods


Download ppt "ENGR-25_MATLAB_OverView-1.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical."

Similar presentations


Ads by Google