Presentation is loading. Please wait.

Presentation is loading. Please wait.

ENGR-25_MATLAB_OverView-2.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-2.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical."— Presentation transcript:

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

2 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.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 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 3 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Systems of Linear Equations  Consider an Electrical Circuit for Which we need to Find the OutPut Electrical Potential  Using the ENGR43 Method of Nodal Analysis we find  MTH6 Provides Methods to solve this System of Eqns

4 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 4 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Systems of Linear Equations cont  We Will Use MATLAB’s “Left” Division Solver  Write the System of Eqns (below) in Matrix/Array Form  In MATLAB need to InPut the 4x4 COEFFICIENT Matrix, A 4x1 CONSTRAINT Vector, b

5 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 5 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Systems of Linear Equations cont  Use MATLAB to Solve 4Eqns in 4Unkowns >> A = [1,0,-1,-2000; 0,1,0,0; 1,-1,1,0; 0,0,1,-1000]; >> b = [0;12;0;0]; >> Soln = A\b Soln = 9.0000 12.0000 3.0000 0.0030 Row Separator Left Division  Thus the Solution by MATLAB

6 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 6 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Left Division Syntax  “Normal”; i.e., RIGHT Division: P/Q Read as “P divided by Q”  LEFT (a.k.a. “Back”), RIGHT Division: S\R Read as “R divided by S” READ

7 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 7 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Left Div & Matrix Inverse  Use MATLAB to Solve 4Eqns in 4Unkowns >> A = [1,0,-1,-2000; 0,1,0,0; 1,-1,1,0; 0,0,1,-1000]; >> b = [0;12;0;0]; >> Soln = A\b The Matrix “Inverse” (More on This Later)  By MTH6 x is the Solution Vector

8 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 8 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Performing MATLAB Ops  You can perform operations in MATLAB in two ways: 1.In the interactive mode, in which all commands are entered directly in the Command window 2.By running a MATLAB program stored in script “m” file. –A Script file contains MATLAB commands, so running it is equivalent to typing all the commands - one at a time - at the Command window prompt. –Run the file by typing its name at the Command window prompt

9 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 9 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods % Comments  COMMENTS are NONexecutable Statements that help Document or Explain Script Files  The comment symbol (%) may be put anywhere in the line. MATLAB ignores everything to the right of the % symbol. >>% This is a comment. >>x = 2+3 % So is this. x = 5  Note that the portion of the line before the % sign is executed to compute x.

10 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 10 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods MATLAB Editor/Debugger

11 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 11 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Script File Usage  The name of a script file must begin with a letter, and may include digits and the underscore character, up to 63 characters.  Do not give a script file the same name as a variable  Do not give a script file the same name as a MATLAB command or function. You can check to see if a command, function or file name already exists by using the exist command

12 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 12 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods DeBugging Script Files  Program errors usually fall into one of the following categories. 1.Syntax errors such as omitting a parenthesis or comma, or spelling a command name incorrectly. MATLAB usually detects the more obvious errors and displays a message describing the error and its location. 2.Errors due to an incorrect mathematical procedure are called runtime errors. Their occurrence often depends on the particular input data. –A common example is division by zero

13 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 13 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Locating Program Errors  To locate program errors, try: 1.Use a simple version of the problem which can be checked by hand to Test your program 2.Display any intermediate calculations by removing semicolons at the end of statements. 3.Use the debugging features of the Editor/Debugger.

14 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 14 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Programming Style 1.Comments section a.The name of the program and any key words in the first line. b.The date created, and the creators' names in the second line. c.The definitions of the variable names for every input and output variable. Include definitions of variables used in the calculations and units of measurement for all input and all output variables! d.The (file)name of every user-defined function called by the program.

15 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 15 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Programming Style cont 1.Input section Include input data and/or the input functions and comments for documentation. 2.Calculation section 3.Output section This section might contain functions for displaying the output on the screen or creating a plot

16 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 16 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods InPut/OutPut Commands CommandDescription disp(A) Displays the contents, but not the name, of the array A. disp(’text’) Displays the text string enclosed within quotes. x = input(’text’) Displays the text in quotes, waits for user input from the keyboard, and stores the value in x. x = input(’text’,’s’) Displays the text in quotes, waits for user input from the keyboard, and stores the input as a TEXT STRING in x.

17 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 17 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Script File Example  Problem: The speed v of a falling object dropped with no initial velocity is given as a function of time t by v = gt. Where g is the Acceleration of Gravity; a CONSTANT = 32.2 ft/s 2  Use MATLAB to Plot v as a function of t for 0 ≤ t ≤ t f Where t f is the final time entered by the user.

18 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 18 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods FallSpeed Plot for t f = 7 sec

19 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 19 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Exploit the TextBook  Throughout each chapter margin notes identify where key terms are introduced.  Each chapter contains tables summarizing the MATLAB commands introduced in that chapter.  At the end of each chapter is a summary guide to the commands covered in that chapter.  Appendix A contains tables of MATLAB commands, grouped by category, with the appropriate page references.  There are three indexes. 1.lists MATLAB commands and symbols, 2.lists SimuLink blocks 3.lists topics.

20 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 20 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods MATLAB Help → Hidden Tab

21 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 21 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Command Window Help-Fcns  help funcname: Displays in the Command window a description of the specified function funcname.  lookfor topic: Displays in the Command window a brief description for all functions whose description includes the specified key word topic.  doc funcname: Opens the Help Browser to the reference page for the specified function funcname, providing a description, additional remarks, and examples.

22 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 22 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Relational Operators SymbolMeaning <Less than <=Less than or equal to >Greater than >=Greater than or equal to ==Equal to ~=Not equal to  0 if FALSE  1 if TRUE >> x = [6,3,9]; y = [14,2,9]; >> z = (x < y) z = 1 0 0 >>z = (x > y) z = 0 1 0 >>z = (x ~= y) z = 1 1 0 >>z = ( x == y) z = 0 0 1 >>z = (x > 8) z = 0 0 1

23 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 23 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods The Find Function  find(x) computes an array containing the INDICES of the NONzero elements of the numeric array x. For example  The resulting array y = [1, 3] indicates that the first and third elements of array x are nonzero. >>x = [-2, 0, 4]; >>y = find(x) y = 1 3 >> M = [4 -9 23; 0 78 -11; 32 0 0 ] M = 4 -9 23 0 78 -11 32 0 0 >> NonZeroM = find(M) NonZeroM = 1 3 4 5 7 8

24 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 24 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Another find Example >> M = [4 -9 23; 0 78 -11; 32 0 0 ] M = 4 -9 23 0 78 -11 32 0 0 >> NonZeroM = find(M) NonZeroM = 1 3 4 5 7 8

25 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 25 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods The Find Distinction  Note the difference between the result obtained by x(x<y) and the result obtained by find(x<y). >>x = [6,3,9,11]; y = [14,2,9,13]; >>values = x(x<y) values = 6 11 >>how_many = length(values) how_many = 2 >>indices = find(x<y) indices = 1 4

26 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 26 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods The if Statement  The general form of the if statement  The elseif statement can be REPEATED if Needed  The else and elseif statements may be OMITTED if not required if expression commands elseif expression commands else commands end

27 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 27 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods if Example  Use MATLAB to Evaluate the Piecewise Function  The Script File

28 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 28 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods if Example OutPut >> x = -11; >> if_Test_0506 y = 10 >> x = 7.3; >> if_Test_0506 y = 83 >> x = 13; >> if_Test_0506 y = 118.1665

29 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 29 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Loops  There are two types of EXPLICIT Loops in MATLAB 1.The for loop, used when the number of passes is known ahead of time 2.The while loop, used when the looping process must terminate when a specified condition is satisfied –In this case the the number of passes is not known in advance.  i.e., WHILEs use Dynamic Termination

30 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 30 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods for Loop Example m = 1; % Array-Building index x(1) = 10; % 1st element of x = 10 for k = 2:3:11 m = m+1; x(m+1) = x(m) + k^2; end  k takes on the values 2, 5, 8, 11. The variable m indicates the index of the array x. When the loop is finished the array x will have the values x(1)=10, x(2)=14, x(3)=39, x(4)=103, x(5)=224.

31 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 31 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods while Loop Example x = 5;k = 0; while x < 25 k = k + 1; y(k) = 3*x; x = 2*x-1; end  The loop variable x is initially assigned the value 5, and it keeps this value until the statement x = 2*x - 1 is encountered the first time. Its value then changes to 9. Before each pass through the loop, x is checked to see if its value is less than 25. If so, the pass is made. If not, the loop is skipped.

32 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 32 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods CAVEAT  Beware MATLAB Script file (“.m file) naming conventions The name of a script file MUST begin with a letter, and May include digits and the underscore character, up to 63 chars. May NOT include the DASH (-) ca·ve·at ( P ) Pronunciation Key (kv-t, kv-, käv-ät), n. A WARNING or CAUTION: “A final caveat: Most experts feel that clients get unsatisfactory results when they don't specify clearly what they want” (Savvy).

33 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 33 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Demos: for & while  Prob 1-39 → Evaluate with for Also list the value of the individual Terms  Prob 1-40 → Use while to find the number of terms, q max, such that

34 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 34 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu Engr/Math/Physics 25 Appendix

35 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 35 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Problem 1-39 >> run Prob1dash39 term = 5 term = 40 term = 135 term = 320 term = 625 term = 1080 term = 1715 term = 2560 term = 3645 term = 5000 sum = 15125

36 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 36 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Problem 1-40 >> run Prob1dash40 min no. terms = 15 Sum-Total for max-terms = 8.8165e+003

37 BMayer@ChabotCollege.edu ENGR-25_MATLAB_OverView-2.ppt 37 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods FallSpeed Code % Bruce Mayer, PE % ENGR25 * 26Jan10 % Program fall_speed_1001.m % Plots speed of a falling object in USA units % % Input Var: % tf = final time (in seconds) % % Output Var: % t = array of times at which speed is computed (in sec) % s = array of speeds (feet/sec converted to mph) % % Parameter Value: g = 32.2; % Acceleration in USA customary units % % Input section: tf = input('Enter final time in seconds = '); % % Calculation section: dt = tf/500; % time step for 501 values % Create an array of 501 time values. t = [0:dt:tf]; % Compute speed values in mph. s = (g*t)*(60/88); % 88fps = 60mph % % Output section: disp('fall speed at t-final'); disp(s(length(s))); disp('mph') plot(t,s), xlabel('t (sec)'),ylabel('s (mph)'), title('Falling Speed vs Time'), grid


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

Similar presentations


Ads by Google