Download presentation
Presentation is loading. Please wait.
Published byBrook Douglas Modified over 8 years ago
1
Engr 0012 (04-1) LecNotes 05-01 script/function comparison scriptsfunctions Show program logic answer “what” questions Show program details answer “how” questions Multipurpose, e.g., get data, computation, display results Single purpose, e.g., get data only Solve a “whole” problem One step in solving a “whole” problem
2
Engr 0012 (04-1) LecNotes 05-02 scripts and workspace m-files copied from get\get12\matlab directory to c:\temp directory MATLAB started, c:\temp current directory » who » help ca05a Class Activity 05a SCRIPT: running scripts affects the workspace » ca05a » who Your variables are: a b » a,b a = 4 b = 7 executing ca05a just added variables to your workspace!!
3
Engr 0012 (04-1) LecNotes 05-03 scripts and workspace » help ca05b Class Activity 05b SCRIPT: running scripts AFFECTS the workspace!!! » ca05b » who Your variables are: a b c d executing ca05b just added more variables to your workspace!! » a,b,c,d a = 1234 b = 54321 c = 1 d = 2 executing ca05b changed the values of variables in your workspace!!
4
Engr 0012 (04-1) LecNotes 05-04 script/function comparison scriptsfunctions Show program logic answer “what” questions Show program details answer “how” questions Multipurpose, e.g., get data, computation, display results Single purpose, e.g., get data only Solve a “whole” problemOne step in solving a “whole” problem Can add variables to workspace Can change values of variables in workspace
5
Engr 0012 (04-1) LecNotes 05-05 functions and workspace » help ca05c Class Activity 05c FUNCTION: functions do not affect workspace » ca05c what did ca05c do??? » who Your variables are: a b c d ca05c did not add variables to workspace » a,b,c,d a = 1234 b = 54321 c = 1 d = 2 ca05c did not change values in workspace
6
Engr 0012 (04-1) LecNotes 05-06 script/function comparison scriptsfunctions Show program logic answer “what” questions Show program details answer “how” questions Multipurpose, e.g., get data, computation, display results Single purpose, e.g., get data only Solve a “whole” problemOne step in solving a “whole” problem Can add variables to workspace Can change variable values in workspace Do not add variables to workspace Do not change variable values in workspace “Good” paper“Scrap” paper
7
Engr 0012 (04-1) LecNotes 05-07 calling scripts and functions » ca05a “call” or execute script by name alone » a = ca05a ??? Attempt to execute SCRIPT ca05a as a function. cannot use script name on rhs of = » help ca05d Class Activity 05d FUNCTION: functions can return values » ca05d ans = -1.0000 + 1.0000i » a = ca05d a = -1.0000 + 1.0000i functions that return a single value can be used like any variable name
8
Engr 0012 (04-1) LecNotes 05-08 calling scripts and functions » clear » help ca05e Class Activity 05e FUNCTION: functions can display in command window » ca05e return1 = 3.1416 return2 = 0.8660 ans = 3.1416 calculations in functions will display if not suppressed by a semicolon » who Your variables are: ans even though displayed, function variables are not added to work space MATLAB will capture the first value returned in a default variable “ ans ”
9
Engr 0012 (04-1) LecNotes 05-09 calling scripts and functions » clear » [a,b] = ca05e return1 = 3.1416 return2 = 0.8660 a = 3.1416 b = 0.8660 » who Your variables are: a b capture of returned values is done by using square braces, [ ], that enclose the variable list names in the function and the workspace (script) need not be the same one-to-one relationship in order values assigned
10
Engr 0012 (04-1) LecNotes 05-10 calling scripts and functions » [a,b] = ca05e; return1 = 3.1416 return2 = 0.8660 semicolon suppression is local
11
Engr 0012 (04-1) LecNotes 05-11 calling scripts and functions » help ca05f Class Activity 05e FUNCTION: function parameters » [c,d] = ca05f ??? Input argument 'in1' is undefined. Error in ==> C:\temp\ca05f.m On line 21 ==> return1 = in1; functions that have inputs (needs) require an input list in ( ) after the function name when called functions that have no inputs (needs) do not use ( ) after the function name when called
12
Engr 0012 (04-1) LecNotes 05-12 calling scripts and functions » clear » [a,b] = ca05f(2,3) a = 2 b = 1.5000 inputs are values (numbers)!!! values are returned » [c,d] = ca05f(a,b) c = 2 d = 0.7500 values can be represented by a variable name
13
Engr 0012 (04-1) LecNotes 05-13 calling scripts and functions » [e,f] = ca05f(2*a,3*b-3) e = 4 f = 0.7500 values can be represented by an arithmetic expression » [g,h] = ca05f(pi,sin(3*b)) g = 3.1416 h = -0.4888 values can be represented by a function call
14
Engr 0012 (04-1) LecNotes 05-14 script/function comparison scriptsfunctions Show program logic answer “what” questions Show program details answer “how” questions Multipurpose, e.g., get data, computation, display results Single purpose, e.g., get data only Solve a “whole” problemOne step in solving a “whole” problem Can add variables to workspace Can change variable values in workspace Do not add variables to workspace Do not change variable values in workspace “Good” paper“Scrap” paper “Called” by name alone “Call” depends on function Cannot be on rhs of = Can be on rhs of =
15
Engr 0012 (04-1) LecNotes 05-15 script design strategy 1. Understand problem / requirements what information will result if successful? what information do you need to get the result? can you work a simple version by hand? 2. Write a problem statement identifying what will result and what is required 3. Outline the steps you would take to solve the problem (what would you do) focus on major tasks needed to get solution don’t worry about the details of how yet i.e., no equations needed at this time
16
Engr 0012 (04-1) LecNotes 05-16 Class Activity 05 open ca05_e12(04-1).pdf in the get\get12 directory there are two problems turn in the requested outline for each problem
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.