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

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Introduction to MATLAB 7
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Introduction to C Programming
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
EPSII 59:006 Spring Topics Using TextPad If Statements Relational Operators Nested If Statements Else and Elseif Clauses Logical Functions For Loops.
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
ENGR-25_MATLAB_OverView-1.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.
Matlab Review Exam 2 – Oct 28, Exam Reminders Open book. Only Matlab book, no notes or loose papers in book. Open book. Only Matlab book, no notes.
Structured Computer Programming EE 201 Introduction to MATLAB 7 for Engineers بسم الله الرحمن الرحيم King Abdulaziz University College of Engineering Dept.
MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
Chapter 1 An Overview Of MATLAB. The default MATLAB Desktop. Figure 1.1–1 1-2 More? See pages 6-7.
Copyright © The McGraw-Hill Companies, Inc. Introduction to MATLAB for Engineers, Third Edition William J. Palm III Chapter 1 An Overview of MATLAB.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical.
ENGR-25_MATLAB_OverView-1.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.
A Brief introduction to MatLAB ($50 - $99 for students)
Chapter 1 An Overview Of MATLAB. The default MATLAB Desktop. Figure 1.1–1 1-2 More? See pages 6-7.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
ENGR-25_Programming-3.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Introduction to Programming with RAPTOR
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
ENG College of Engineering Engineering Education Innovation Center 1 More Script Files in MATLAB Script File I/O : Chapter 4 1.Global Variables.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
ENG College of Engineering Engineering Education Innovation Center 1 Basic For Loops in MATLAB Programming in MATLAB / Chapter 6 Topics Covered:
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Digital Image Processing Introduction to M-function Programming.
Introduction to Engineering MATLAB – 4 Arrays Agenda Creating arrays of numbers  Vectors: 1-D Arrays  Arrays: 2-D Arrays Array Addressing Strings & String.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Structured Computer Programming EE 201 Introduction to MATLAB 7 for Engineers بسم الله الرحمن الرحيم King Abdulaziz University College of Engineering Dept.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
מטלב וניתוח נתונים ממערכות עצביות היכרות עם MATLAB
Chapter 6 JavaScript: Introduction to Scripting
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 2 - Introduction to C Programming
Introduction to MATLAB for Engineers, Third Edition
Basic operations in Matlab
Scripts & Functions Scripts and functions are contained in .m-files
Chapter 2 - Introduction to C Programming
MATLAB DENC 2533 ECADD LAB 9.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
WEB PROGRAMMING JavaScript.
Chapter 2 - Introduction to C Programming
T. Jumana Abu Shmais – AOU - Riyadh
Chapter 2 - Introduction to C Programming
Introduction to MATLAB 7
Chapter 2 - Introduction to C Programming
Presentation transcript:

ENGR-25_MATLAB_OverView-2.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-2

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

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

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

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 = Row Separator Left Division  Thus the Solution by MATLAB

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

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

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

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.

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

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

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

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.

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.

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

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.

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.

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

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.

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

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.

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 = >>z = (x > y) z = >>z = (x ~= y) z = >>z = ( x == y) z = >>z = (x > 8) z = 0 0 1

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 = [ ; ; ] M = >> NonZeroM = find(M) NonZeroM =

ENGR-25_MATLAB_OverView-2.ppt 24 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Another find Example >> M = [ ; ; ] M = >> NonZeroM = find(M) NonZeroM =

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

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

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

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 =

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

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.

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.

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

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

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

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

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 = e+003

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