+ All Categories
Home > Documents > [email protected] ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25:...

[email protected] ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25:...

Date post: 02-Apr-2015
Category:
Upload: davin-rutty
View: 220 times
Download: 1 times
Share this document with a friend
38
[email protected] • ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical & Mechanical Engineer [email protected] Engr/Math/Physics 25 Chp3 MATLAB Functions: Part4
Transcript
Page 1: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt1

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Bruce Mayer, PELicensed Electrical & Mechanical Engineer

[email protected]

Engr/Math/Physics 25

Chp3 MATLABFunctions:

Part4

Page 2: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt2

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

Page 3: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt3

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.

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

[email protected] • ENGR-25_Functions-4.ppt4

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

Page 5: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt5

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

Page 6: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt6

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Design: Data-inputs, SensorsQty Description/Location Type Pwr Src Output To Notes & Comments

02 Immersed in coolant stream at Supply & Return connections of chiller

TempK 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

13 Temp array around the new TMB bubbler

TempK TC

n/a [Strawberry Tree Data Acq] [laptop]

Order 2nd DATA shuttle. See item 4932. See bubtcloc.dwg, or prs39602

03 Collant flow leading into the three bubblers

Fluid FlwTurbine 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

03 MFM on output of bubblers to measure the Qv,B+QN2 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

02 In & 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

01 Absolute Barometric Pressure. Locate Setra-270 sensor on top of system

0-5 Vdc Output 800 1100 mBar PA

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

Page 7: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt7

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Sensor Placement Design

Page 8: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt8

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Computer DataAcq DataFlowBoard 1:1194

Board 2:1159

Board 3: 160

W rite 1194a

W rite 1159a

W rite 160a

Bub T Cs

W rite1194b

W rite1159b

W rite160b

TEO S Line TC

Chiller T Cs

Bub N 2 M FC s

MF Ms

0-5V -> Torr A tm Pres s

TP1: V -> C

TP9: V->C

Ch2 &3 Bub T

Ch2 &3 Ts ens

Ch2 &3 Thires

“soft” Charts

Com

pute

r In

terf

ace

Car

ds

Page 9: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt9

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)

Page 10: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt10

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

Page 11: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt11

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.00WORKSHEET : 960411D3Recording Date : 4/11/96, 15:28:28Block Length : 8Delta : 0.500000 sec.Number of Channels : 4Date;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

Page 12: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt12

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

Test-Point-3 SCOPEMETER DEMO BOARD 4022 245 0422.2

0.0

1.0

2.0

3.0

4.0

5.0

6.0

7.0

8.0

9.0

-0.01 0.00 0.01 0.02 0.03 0.04

Time (s).

Sco

peM

eter

Sig

nal

(V

dc)

Signal (Vdc)

file = SM_Demo_Board_0210.xls

DataFile HEADER

COMMAS Separate Data

Page 13: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt13

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

Page 14: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt14

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 MATLABScopeMeter used for Voltage-Level Data Acquisition

Fluke-199 200 MHz ScopeMeter

Omega ACC-PS1AcceleroMeterPower Supply

Cables To/FromAcceleroMeter

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

[email protected] • ENGR-25_Functions-4.ppt15

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Page 16: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt16

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Data Acq from H2 Fuel CellMotorCycle (H2 @ 5000 psig)

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

[email protected] • ENGR-25_Functions-4.ppt17

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Page 18: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt18

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

All Done for Today

FlukeScopeMeter

Page 19: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt19

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Bruce Mayer, PELicensed Electrical & Mechanical Engineer

[email protected]

Engr/Math/Physics 25

Appendix 6972 23 xxxxf

Page 20: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt20

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Import ScopeMeter .csv Data

Start the Import Wizard

Locate Directory with DataFile

Page 21: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt21

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Import TP3 .csv Data

Find and OPEN DataFile

Page 22: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt22

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Import TP3 .csv Data

MATLAB AutoMatically picks Comma for “Select Column Separator(s)”

Page 23: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt23

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Import TP3 .csv Data

BUT, Data does NOT Preview Properly

Suspect HEADER as cause

Page 24: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt24

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

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

[email protected] • ENGR-25_Functions-4.ppt25

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)

Page 26: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt26

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

Page 27: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt27

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>

Page 28: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt28

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

Page 29: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt29

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>

Page 30: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt30

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 ScopeMeterRecords TWO DataPoints during the sampling interval: Max & Min

Choose to Plot MAX, which is stored Col-3• c.f. Column-3 on previous slide

Page 31: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt31

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

Page 32: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt32

Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods

Plot TP3 t vs V

Page 33: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt33

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.

Page 34: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt34

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

Page 35: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt35

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;

endend

Page 36: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt36

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.

Page 37: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt37

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.

Page 38: BMayer@ChabotCollege.edu ENGR-25_Functions-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

[email protected] • ENGR-25_Functions-4.ppt38

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.


Recommended