280 likes | 357 Views
Lecture 9. Chap. 5 5.1-5.3.4. Outline. 5.1 Concepts: Abstraction and Encapsulation 5.2 Black Box View of a Function 5.3 MATLAB Implementation 5.3.1 General Template 5.3.2 Function Definition 5.3.3 Storing and Using Functions 5.3.4 Calling Functions. 5.1 Abstraction.
E N D
Lecture 9 Chap. 5 5.1-5.3.4
Outline • 5.1 Concepts: Abstraction and Encapsulation • 5.2 Black Box View of a Function • 5.3 MATLAB Implementation • 5.3.1 General Template • 5.3.2 Function Definition • 5.3.3 Storing and Using Functions • 5.3.4 Calling Functions
5.1 Abstraction • Abstraction: with some specific entities, consider what they have in common, think of that commonality as what is essential. Capture the essential, pay less to no attention to the differences, call this the class. Represent the entities as members of the class; differences can be considered modifications with respect to the class.
Procedure Abstraction • Line or lines of instructions expressing a procedure • Idea of essentials / modifications for procedure • Building block for re-use
Encapsulation • Worth hiding within a function name, so that the implementation does not distract • Enables thought to take place with higher granularity • Protects variables inside the function from modification by external agents, and variables outside the function from modification by it.
5.2 Black Box • Seeing only the outside of the function, also called its interface. • Use the function as a building block; make functions then use them • An intermediary between top-down design and bottom-up design • Describe the black box by its interface, not by its internals.
5.3 MATLAB Implementation • The template: • function • <return info> %whatever is returned by the function • <function name> %pick a name, preferably unique • (<parameters>) %whatever is used by the function • <documentation> %comments for help, are good • <code body> %must correspond to <return info>
Function examples function healthyTime = health( initialStock, age, accumulatedInvestment) % yields healthy time %hT = health(initialStock, age, investment in health) function shadowPrice = sP( priceOfMedicalCare, age, education ) % yields shadow price of medical care %rises with age if the rate of depreciation rises %falls with education if more educated people are more efficient producers of health
More examples function qHD = quantityOfHealthDemanded(shadowPrice) %yields quantity of health demanded %qHD = quantityOfHealthDemanded(shadowPrice) function qMCD = quantityOfHealthCareDemanded(shadowPrice) %yields quantity of health care demanded %qMCD = quantityOfHealthCareDemanded(shadowPrice) function adjustHC = investInHealthCapital( medicalCare, diet, exercise, recreation, housing, levelOfEducation) %yields adjustment in health capital %adjustHC = investInHealthCapital(medicalCare, diet, %exercise, recreation, housing, levelOfEducation)
More Examples function tA = timeAvailable(health) %yields time available %tA = timeAvailable(helath) function rtiHealth = rti(timeAvailable) %yields return on investment in health %rtiHealth=rti(timeAvailable) function iu = intertemporalUtility( phi, H, Z) %yields inter-temporal utility %iu = intertemporalUtility(phi, H, Z) %phi, H, Z are vectors of length n %H0 is inherited stock, Hi is stock at time i %phii is service flow per unit stock %hi = phii*Hi is total consumption of health services %Zi is total consumption of other commodities
From statement to function H(i+1) = H(i) + I(i) – delta(i)*H(i) %delta is the rate of depreciation, exogenous %exogenous: set these variables on start up function h = health(previousHealth, investmentInHealth, depreciationRate) %yields health as investment offset by depreciation %h = health(previousHealth, investmentInHealth, % depreciationRateForHealth) h = previousHealth + investmentInHealth – depreciationRate*previousHealth end
Example switch in function Itp(i) = investInHealth(M(i), TH(i), E(i)) function iIH = investInHealth(M, TH, E) %computes investment in health %investment InHealth(medical care, time spent on health, stock of human capital) iIH = M * g(TH/M, E) %can factor out M, because %homogeneous, degree 1 end function gResult = g(t, E) %computes investment in health, with price of medical care factored out % I = g(normalized time, stock of human capital) switch(E) case() efficiencyOfProductionProcess = case() end %carry on, using efficiencyOfProductionProcess end If we store the function g in the same file as investInHealth, g can be called within the file but not from outside the file
Creating a Function • Keep it in a file, so, at the command line, type edit • When the new window appears, write the function into it, using the template shown earlier in this lecture
Save the Function • Save the function using, e.g., the save disk icon. Usually the default name offered is the correct choice. • Now the function can be invoked, from other functions and from the command window.
Lifetime of the Function • Having saved the function, as on the previous slide, you have saved it to disk. It will be there when you log in next time. • You can edit the function file if you choose. Double click the function’s row in the listing.
Pass By Value • Pass-by-value is the only technique provided by MATLAB for providing data into a function. • Assurance that the data sent as input to the function does not get changed by the function.