Lessons from CST exercise - PowerPoint PPT Presentation

About This Presentation
Title:

Lessons from CST exercise

Description:

One step in solving a 'whole' problem. Engr 0012 (04-1) LecNotes 05-02. scripts and workspace ... One step in solving a 'whole' problem. Can add variables to workspace ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 17
Provided by: jackp3
Learn more at: https://sites.pitt.edu
Category:

less

Transcript and Presenter's Notes

Title: Lessons from CST exercise


1
script/function comparison
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 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 ca05b changed 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
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
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
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
Engr 0012 (04-1) LecNotes 05-05
6
script/function comparison
Show program details answer how questions
Show program logic answer what 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
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
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 gt C\temp\ca05f.m On line
21 gt 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(2a,3b-3) e 4 f
0.7500
values can be represented by an arithmetic
expression
g,h ca05f(pi,sin(3b)) 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
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
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
dont 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
Write a Comment
User Comments (0)
About PowerShow.com