1 / 28

Lecture 9

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.

Download Presentation

Lecture 9

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. Lecture 9 Chap. 5 5.1-5.3.4

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

  3. 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.

  4. Procedure Abstraction • Line or lines of instructions expressing a procedure • Idea of essentials / modifications for procedure • Building block for re-use

  5. 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.

  6. 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.

  7. 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>

  8. 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

  9. 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)

  10. 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

  11. 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

  12. 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

  13. 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

  14. Edit the Function

  15. 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.

  16. Default Name is Probably Right

  17. See Name In Window Title

  18. See Function In List

  19. 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.

  20. Help on Use of the Function

  21. Use of the Function

  22. 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.

  23. Try to Change Inside Function

  24. Protects Variable From Change

  25. Using Nargin

  26. Invoking with Variable Number of Input Parameters

  27. Invoking With Variable Number of Output Parameters

  28. Variable Numbers of Outputs

More Related