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

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

BMayer@ChabotCollege.edu ENGR-25_Functions-4.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 Chp3 MATLAB Functions: Part4

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 2 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Learning Goals  Understand the difference Built-In and User-Defined MATLAB Functions  Write User Defined Functions  Describe Global and Local Variables  When to use SUBfunctions as opposed to NESTED-Functions  Import Data from an External Data-File As generated, for example, by an Electronic Data-Acquisition System

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 3 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Private Functions  Private functions reside in subdirectories with the special name private, and they are visible only to fcns in the parent directory  Assume the directory wmays is on the MATLAB search path. A subdirectory of wmays called private may contain functions that only the functions in wmays can call.  Because private functions are invisible outside the parent directory wmays, they can use the same names as functions in other directories.

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 4 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Data Acquisition  At Some Point in His/Her Career Virtually EVERY Engineer will need to ACQUIRE and ANALYZE data from some PHYSICAL system  The Acquisition of large amounts of Digital Data used to be quite difficult, but today modern Electronics makes this MUCH easier.  Main Steps in the design of a Data Acq Sys Determine WHAT Data to Collect (can be HARD) Obtain electronic measurement SENSORS Obtain Sensor↔Computer INTERFACE Hardware & Software to collect data in digital form

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 5 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Typical Data Acq Sys.  Items  &  → Sensor; Sensor: Power & Signal-Conditioning  Item  → USB or Serial Connection  Item  → Interface Software

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 6 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Design: Data-inputs, Sensors QtyDescription/LocationTypePwr SrcOutput ToNotes & Comments 02Immersed in coolant stream at Supply & Return connections of chiller Temp K TC n/a[Strawberry Tree Data Acq] [laptop] Need to install 3/8-Tu to 1/4- FPT branch TEE in chiller plumbing to add 083886 & 080144. TEE = SS-600-3TTF 13Temp array around the new TMB bubbler Temp K TC n/a[Strawberry Tree Data Acq] [laptop] Order 2nd DATA shuttle. See item 4932. See bubtcloc.dwg, or prs39602 03Collant flow leading into the three bubblers Fluid Flw Turbine meter 12 Vdc from NO- SHOK controllers [modifed 973680 V-divider card] [Strawberry Tree Data Acq] [laptop] 3 wires: +12Vdc from NoShok, com from NoShok, sq-wave pulse to NoShok 03MFM on output of bubblers to measure the Q v,B + Q N2 mixture output of bubblers 0-5 Vdc Output  flow WJ-1000 system[Strawberry Tree Data Acq] [laptop] Use Tylan MFC/MFM extension cards, pn 3780-280 02In & Out signal on ch2&3 TMB bubbler Hi-res card channel. Card = 80 CARD. Vin = TP8, Vout = TP16 0-5 Vdc Output  -30 °C +70 °C WJ-1000 system[TP8 & TP16] [Strawberry Tree Data Acq] [laptop] See item 5270 for Strawberry Tree over-voltage calcs OK Use Tylan MFC/MFM extension cards, pn 3780-280 01Absolute Barometric Pressure. Locate Setra- 270 sensor on top of system 0-5 Vdc Output  800 1100 mBar P A 24Vdc, bench top pwr supply from FPD handler project [Strawberry Tree Data Acq] [laptop] Bench test req’d for "088603-000 xdcr,press,0- 100 psig,atm (model 270)" 24 V pwr supply also power the NoShok controllers

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 7 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Sensor Placement Design

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 8 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Computer DataAcq DataFlow “soft” Charts C o m p u t e r I n t e r f a c e C a r d s

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 9 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Data Files  Most DataAcq Systems record the data using characters listed in the American Standard Code for Information Interchange (ASCII) The Base ASCII set contains 128 characters (hex 7F) The Extended set contains 256 characters (hex FF)

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 10 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Data Files cont  Data Files Usually have File Extensions of.dat or.txt Many others exist –in Feb10 found 3737 Different Data Files types on http://www.webopedia.com/quick_ref/fileextensionsfull.asp  Windows Wordpad.exe creates ASCII text, and is typically used to EDIT Data Files Or in MSWord you can save/read files as ‘text only’ –Change the DataFile extension to “.txt” if needed  Many Times Data Files Have “Headers” that need to be stripped OUT to Expose data-only

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 11 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Data File Examples  KLA-T “KLARFF file fragment SPACES Separate the Data FIELDS  StrawBerry Tree DasyLab-Software file DEFECTID XREL YREL XINDEX YINDEX XSIZE YSIZE DEFECTAREA DSIZE CLASSNUMBER TEST IMAGECOUNT ; DefectList 1 2.7076600000e+02 3.6191450000e+03 -4 1 0.249000 0.249000 0.032000 1.7800000000e-01 0 1 0 2 1.5445400000e+02 2.5059200000e+03 -4 -2 0.249000 0.249000 0.027500 1.6500000000e-01 0 1 0 3 1.8203200000e+02 2.3367030000e+03 -4 -3 0.499000 0.499000 0.046800 2.1600000000e-01 0 1 0 4 3.8058700000e+02 2.0016480000e+03 -4 -3 0.249000 0.998000 0.046800 2.1600000000e-01 0 1 0 5 9.2548000000e+01 4.8907490000e+03 -4 -4 0.249000 0.249000 0.032000 1.7800000000e-01 0 1 0 WorkBench PC for Windows - V 2.60.00 WORKSHEET : 960411D3 Recording Date : 4/11/96, 15:28:28 Block Length : 8 Delta : 0.500000 sec. Number of Channels : 4 Date;Time;TC14: TEOS Line (C);TC16: TEOS Line (C);TC17: Chllr Sup (C);TC18: Chllr Ret (C); 4/11/96;15:28:28.0;74.908;86.706;-22.458;-19.720; 4/11/96;15:28:28.5;74.938;86.716;-22.467;-19.716; 4/11/96;15:28:29.0;74.952;86.705;-22.499;-19.706; 4/11/96;15:28:29.5;74.980;86.702;-22.509;-19.695; SEMICOLONS Separate the Data FIELDS

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 12 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Data File Examples  FLUKE ScopeMeter.csv Data File Fragment "Title ","Input A", "ID ",1, "Type ","Envelope", "Date ",13/32/94, "Time ",19:34:51, "X Scale ",5.0E-03, "X At 0% ",-1.00E-02, "X Resolution ",2.500000E+01, "X Size ",254, "X Unit ","s", "X Label ","5 ms/Div", "Y Scale ",2.00E+00, "Y At 50% ",6.32E+00, "Y Resolution ",2.500000E+01, "Y Size ",256, "Y Unit ","V", "Y Label ","V", -1.00E-02,5.6E-01,6.4E-01 -9.8E-03,4.0E-01,5.6E-01 -9.6E-03,4.8E-01,5.6E-01 -9.4E-03,4.0E-01,5.6E-01 -9.2E-03,4.0E-01,5.6E-01 -9.0E-03,4.0E-01,5.6E-01 DataFile HEADER COMMAS Separate Data

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 13 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Import Data into MATLAB  Once the DataAcq System Generates reams of Digital Data, the Engineer or Scientist needs to ANALYZE it To discern its MEANING  MATLAB has Extremely Powerful Data-Analysis Tools, but we need to get the data INTO MATLAB first  MATLAB provides a Windows-type “wizard” to IMPORT the data; bringing data-file values into the MATLAB Environment

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 14 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods MATLAB: Data Import/Analysis  Recall the Fluke ScopeMeter.csv file  We Want to import the Data from This File and plot it it Using MATLAB ScopeMeter used for Voltage-Level Data Acquisition Fluke-199 200 MHz ScopeMeter Omega ACC-PS1 AcceleroMeter Power Supply Cables To/From AcceleroMeter

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 15 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 16 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Data Acq from H 2 Fuel Cell MotorCycle (H 2 @ 5000 psig)

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 17 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 18 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods All Done for Today Fluke ScopeMeter

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

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 20 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Import ScopeMeter.csv Data  Start the Import Wizard  Locate Directory with DataFile

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 21 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Import TP3.csv Data  Find and OPEN DataFile

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 22 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Import TP3.csv Data  MATLAB AutoMatically picks Comma for “Select Column Separator(s)”

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 23 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Import TP3.csv Data  BUT, Data does NOT Preview Properly  Suspect HEADER as cause

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 24 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Import TP3.csv Data  Edit Header with MS WordPad HEADER contains NUMBERS – Suspect this is the problem Replace with Pure TEXT

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 25 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Import TP3.csv Data  Give File NEW name to preserve original Form of Data  Replace Header w/ 3 lines of Text All No.s Preceded by Letter(s)

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 26 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Import TP3.csv Data  Use Import Wizard to Open NEW.csv file  Preview Looks MUCH better Even Picked Out Column Headers

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 27 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Import TP3.csv Data  The DATA Preview Looks Really Promising  Data Values Arrayed in SpreadSheet-like cell arrangement → click Next>

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 28 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Import TP3.csv Data  Can Chose Variable Format as either: 3-Col ARRAY or 3 individual Col-VECTORS

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 29 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Import TP3.csv Data  Do it the HARD way with 3-Col Array Change VAR name to meaningful Value  MATLAB Array Var named SM_TP3 → click Next>

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 30 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Import TP3.csv Data  Chk that SM_TP3 appears in WorkSpace SoFar, SoGood...  Now Some Physics At each Sample Time the ScopeMeter Records TWO Data Points during the sampling interval: Max & Min  Choose to Plot MAX, which is stored Col-3 c.f. Column-3 on previous slide

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 31 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Plot TP3 t vs V Data  The Command Window Plotting Session >> % make time vector, t, from Array Col-1 >> t = SM_TP3(:,1); >> % construct voltage vector, V, from Array Col-3 >> V = SM_TP3(:,3); >> plot(t,V), xlabel('Time (s)'), ylabel('TP3 E-Potential (volt)'),... title('Fluke ScopeMeter SignGen TestPt- 3'), grid

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 32 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Plot TP3 t vs V

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 33 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Nested Functions  MATLAB permits placement of the definitions of one or more functions within another function. Functions so defined are said to be nested within the main function.

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 34 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Nested Functions cont  Like any.m-file function, a nested function contains the usual components of an.m-file function.  However, a nested function must always terminate with an end statement.  In fact, if an.m-file contains at least one nested function, then all functions, including subfunctions, in the file must terminate with an end statement

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 35 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Example  Nested Function  This example assigns a function handle for a nested function and then passes the handle to the MATLAB function fminbnd to find the minimum point on a parabola.  The ConcaveUp fcn constructs and returns a function handle f for the nested function p. This handle gets passed to fminbnd. function f = ConcaveUp(a, b, c) f = @p; function y = p(x) y = a*x^2 + b*x + c; end end

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 36 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Example  Nested Function cont  Test the Nest in the Command Window >> g = ConcaveUp(7, 41, -73); >> fminbnd(g, -50, 50) ans = -2.9286  Note than the function p(x) can see the variables a, b, and c in the calling function’s workspace.

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 37 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Nested vs Sub Functions  Nested functions might seem to be the same as subfunctions, but they are not. Nested functions have two unique properties: 1.A nested function can access the workspaces of all functions inside of which it is nested. Thus, –a variable that has a value assigned to it by the primary function can be read or overwritten by a function nested at any level within the main function –A variable assigned in a nested function can be read or overwritten by any of the functions containing that function.

BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 38 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Nested vs Sub Functions cont  The second unique property: 2.If you construct a function handle for a nested function, the handle not only stores the information needed to access the nested function; it also stores the values of all variables shared between the nested function and those functions that contain it. –This means that these variables persist in memory between calls made by means of the function handle.