150 likes | 293 Views
script organization. General organization for scripts. 1. Header. 2. Variable dictionary. 3. Constant definitions. 4. Input data/independent variable/parameter values. 5. Computations. 6. Output results/dependent variable values. ex: wkshp5_ac1.m. Engr 0012 (04-1) LecNotes 04-01.
E N D
script organization General organization for scripts 1. Header 2. Variable dictionary 3. Constant definitions 4. Input data/independent variable/parameter values 5. Computations 6. Output results/dependent variable values ex: wkshp5_ac1.m Engr 0012 (04-1) LecNotes 04-01
introduction to functions functions are the “workhorse” of computing script/function comparison Show program logic answer “what” questions Show program details answer “how” questions ex: wkshp7_soln.m Engr 0012 (04-1) LecNotes 04-02
generic function “definition” function [results list] = fcn_name(needs list) % % function purpose (i.e., fcn_name does ???) % needs: 1st variable in needs list % 2nd variable in needs list % results: 1st variable in results list % 2nd variable in results list % % JF Patzer % Engr 0012 % Fall Term, 2003 (04-1) % Mon 8 Sep 2003 % variable dictionary % MATLAB statements (w/comments) that fulfill purpose Engr 0012 (04-1) LecNotes 04-03
1. functions that need nothing and return a value(s) defn: function [rho, g, height, pout] = wkshp7_getdata( ) use: [rho, g, height, pout] = wkshp7_getdata; 2. functions that return nothing and need a value(s) defn: function [] = wkshp7_results(rho,g,height,pout,pabs_b) use: wkshp7_results(rho, g, height, pout, pabs_b) 3. functions that need and return a value(s) defn: defn: function [] = wkshp7_header( ) function [pabs_b] = wkshp7_pbot(rho,g,height,pout) use: use: 4. functions that need and return nothing (rare) wkshp7_header pabs_b = wkshp7_pbot(rho, g, height, pout) four types of functions Engr 0012 (04-1) LecNotes 04-04
constants have no values needed to calculate ==> needs list is “empty” ==> results list has a variable name must have one assignment statement for each name in results list function that provides a constant value function [ConvFac] = BTU_to_J( ); % Conversion factor for BTU to joules % needs: % results: ConvFac conversion factor, J/BTU % % JF Patzer % Engr 0012 % Fall Term, 2003 (04-1) % Mon 8 Sep 2003 % % variable dictionary % ConvFac conversion factor BTU to J, J/BTU ConvFac = 1055.0; Engr 0012 (04-1) LecNotes 04-05
using function that provides a constant value copyBTU_to_J.m from get to temp directory » 2 ans = 2 » BTU_to_J ans = 1055 » b = a*BTU_to_J b = 2110 » help BTU_to_J Engr 0012 (04-1) LecNotes 04-06
==> needs list is “empty” ==> results list has at least one name function that returns a single value but needs nothing function [amount] = getdollars( ); % get $ amount for financial calculations % needs: % results: amount - amount input, $ % % JF Patzer % Engr 0012 % Fall Term, 2003 (04-1) % Mon 8 Sep 2003 % % variable dictionary % amount amount input, $ amount = input('\nEnter principal amount ==> '); Engr 0012 (04-1) LecNotes 04-07
using function that returns a single value copygetdollars.m from get to temp directory » getdollars ans = 3 » dollars = getdollars dollars = 4 » principal = getdollars; » help getdollars Engr 0012 (04-1) LecNotes 04-08
==> needs list is “empty” ==> results list has more than one name function that returns more than one value but needs nothing function [time1, time2] = gettime( ); % get start and stop times for race % needs: % results: time1 start time, s % time2 end time, s % % JF Patzer % Engr 0012 % Fall Term, 2003 (04-1) % Mon 8 Sep 2003 % variable dictionary % time1 start time, s % time2 end time, s time1 = input('\nEnter start time in sec ==> '); time2 = input('\nEnter end time in sec ==> '); Engr 0012 (04-1) LecNotes 04-09
using function that returns more than one value but needs nothing copygettime.m from get to temp directory » gettime ans = 4 only first value entered displays as default » [start,stop] = gettime start = 1 stop = 4 values assigned in order » [start,stop] = gettime; » help gettime Engr 0012 (04-1) LecNotes 04-10
==> needs list has at least one name ==> results list has at least one name function that needs and returns values function [celsius] = FtoC(fahr); % convert temp in F to C % needs: fahr temp in F, F % results: celsius temp in C, C % % JF Patzer % Engr 0012 % Fall Term, 2003 (04-1) % Mon 8 Sep 2003 % variable dictionary % fahr temp in F, F % celsius temp in C, C celsius = 5.0*(fahr-32.0)/9.0; Engr 0012 (04-1) LecNotes 04-11
» BoilF = 212; » FtoC(BoilF) using function that needs and returns values copyFtoC.m from get to temp directory » FtoC ??? Input argument 'fahr' is undefined. Error in ==> D:\Workarea\engr classes\e12(03-1)\MATLAB -\FtoC.m On line 11 ==> celsius = 5.0*(fahr-32.0)/9.0; » FtoC(212) can provide “value” as a number ans = 100 ans = 100 can provide “value” as a variable name Engr 0012 (04-1) LecNotes 04-12
==> needs list has at least one name ==> results list no names function that needs values but returns nothing function [] = conv_results(fahr, celsius); % display temperature conversion, F to C % needs: fahr temp in F, F % celsius temp in C, C % results: % % JF Patzer % Engr 0012 % Fall Term, 2003 (04-1) % Mon 8 Sep 2003 % variable dictionary % fahr temp in F, F % celsius temp in C, C fprintf( '\n%.2f F is %.2f C', fahr, celsius ); Engr 0012 (04-1) LecNotes 04-13
can use values can use variable names using function that needs values but returns nothing copyconv_results.m from get to temp directory » conv_results(123,3454) 123.00 F is 3454.00 C » conv_results(boilF,boilC) 32.00 F is 0.00 C Engr 0012 (04-1) LecNotes 04-14
Class Activities 1. Workshop 06 - Scripts and Workspace 2. Workshop 07 - Intro to Functions Turn in at start of next class (Wednesday 10 September) MATLAB Workshop 07: Exercise 3. paper copy of script and screen printout of script working with h = 12.3 cm Engr 0012 (04-1) LecNotes 04-15