Engr 0012 (04-1) LecNotes 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
Engr 0012 (04-1) LecNotes 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!!
Engr 0012 (04-1) LecNotes 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 = c = 1 d = 2 executing ca05b changed the values of variables in your workspace!!
Engr 0012 (04-1) LecNotes 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
Engr 0012 (04-1) LecNotes 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 = c = 1 d = 2 ca05c did not change values in workspace
Engr 0012 (04-1) LecNotes 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
Engr 0012 (04-1) LecNotes 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 = i » a = ca05d a = i functions that return a single value can be used like any variable name
Engr 0012 (04-1) LecNotes calling scripts and functions » clear » help ca05e Class Activity 05e FUNCTION: functions can display in command window » ca05e return1 = return2 = ans = 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 ”
Engr 0012 (04-1) LecNotes calling scripts and functions » clear » [a,b] = ca05e return1 = return2 = a = b = » 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
Engr 0012 (04-1) LecNotes calling scripts and functions » [a,b] = ca05e; return1 = return2 = semicolon suppression is local
Engr 0012 (04-1) LecNotes 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
Engr 0012 (04-1) LecNotes calling scripts and functions » clear » [a,b] = ca05f(2,3) a = 2 b = inputs are values (numbers)!!! values are returned » [c,d] = ca05f(a,b) c = 2 d = values can be represented by a variable name
Engr 0012 (04-1) LecNotes calling scripts and functions » [e,f] = ca05f(2*a,3*b-3) e = 4 f = values can be represented by an arithmetic expression » [g,h] = ca05f(pi,sin(3*b)) g = h = values can be represented by a function call
Engr 0012 (04-1) LecNotes 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 =
Engr 0012 (04-1) LecNotes 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
Engr 0012 (04-1) LecNotes 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