1 / 16

Engr 0012 (04-1) LecNotes 05-01

script/function comparison. Show program logic answer “ what ” questions. Show program details answer “ how ” questions. Single purpose , e.g., get data only. Multipurpose , e.g., get data, computation, display results. One step in solving a “whole” problem. Solve a “whole” problem.

andres
Download Presentation

Engr 0012 (04-1) LecNotes 05-01

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. script/function comparison Show program logic answer “what” questions Show program details answer “how” questions Single purpose, e.g., get data only Multipurpose, e.g., get data, computation, display results One step in solving a “whole” problem Solve a “whole” problem Engr 0012 (04-1) LecNotes 05-01

  2. 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 05-02

  3. scripts and workspace » help ca05b Class Activity 05b SCRIPT: running scripts AFFECTS the workspace!!! » ca05b executing ca05b just added more variables to your workspace!! » who Your variables are: a b c d » a,b,c,d a = 1234 b = 54321 c = 1 d = 2 executing ca05bchanged the values of variables in your workspace!! Engr 0012 (04-1) LecNotes 05-03

  4. script/function comparison Show program details answer “how” questions Show program logic answer “what” questions Single purpose, e.g., get data only Multipurpose, e.g., get data, computation, display results Solve a “whole” problem One step in solving a “whole” problem Can add variables to workspace Can change values of variables in workspace Engr 0012 (04-1) LecNotes 05-04

  5. functions and workspace » help ca05c Class Activity 05c FUNCTION: functions do not affect workspace what did ca05c do??? » ca05c » who Your variables are: a b c d ca05cdid not add variables to workspace » a,b,c,d a = 1234 b = 54321 c = 1 d = 2 ca05cdid not change values in workspace Engr 0012 (04-1) LecNotes 05-05

  6. script/function comparison Show program details answer “how” questions Show program logic answer “what” questions Single purpose, e.g., get data only Multipurpose, e.g., get data, computation, display results Solve a “whole” problem One step in solving a “whole” problem Can add variables to workspace Do not add variables to workspace Can change variable values in workspace Do not change variable values in workspace “Good” paper “Scrap” paper Engr 0012 (04-1) LecNotes 05-06

  7. calling scripts and functions “call” or execute script by name alone » ca05a » 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 functions that return a single value can be used like any variable name » a = ca05d a = -1.0000 + 1.0000i Engr 0012 (04-1) LecNotes 05-07

  8. even though displayed, function variables are not added to work space MATLAB will capture the first value returned in a default variable “ans” 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 Engr 0012 (04-1) LecNotes 05-08

  9. calling scripts and functions capture of returned values is done by using square braces, [ ], that enclose the variable list » clear » [a,b] = ca05e return1 = 3.1416 return2 = 0.8660 a = 3.1416 b = 0.8660 names in the function and the workspace (script) need not be the same one-to-one relationship in order values assigned » who Your variables are: a b Engr 0012 (04-1) LecNotes 05-09

  10. calling scripts and functions semicolon suppression is local » [a,b] = ca05e; return1 = 3.1416 return2 = 0.8660 Engr 0012 (04-1) LecNotes 05-10

  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 Engr 0012 (04-1) LecNotes 05-11

  12. calling scripts and functions » clear » [a,b] = ca05f(2,3) a = 2 b = 1.5000 inputs are values (numbers)!!! values are returned values can be represented by a variable name » [c,d] = ca05f(a,b) c = 2 d = 0.7500 Engr 0012 (04-1) LecNotes 05-12

  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 Engr 0012 (04-1) LecNotes 05-13

  14. script/function comparison Show program details answer “how” questions Show program logic answer “what” questions Single purpose, e.g., get data only Multipurpose, e.g., get data, computation, display results Solve a “whole” problem One step in solving a “whole” problem Can add variables to workspace Do not add variables to workspace Can change variable values in 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 05-14

  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 Engr 0012 (04-1) LecNotes 05-15

  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 Engr 0012 (04-1) LecNotes 05-16

More Related