organizing and debugging matlab programs
play

Organizing and Debugging Matlab Programs Rationale Gerald - PDF document

Overview Organizing and Debugging Matlab Programs Rationale Gerald Recktenwald Programming Style Portland State University Why and How of Modular Code Department of Mechanical Engineering Top down program design Basic


  1. Overview Organizing and Debugging Matlab Programs • Rationale Gerald Recktenwald • Programming Style Portland State University • Why and How of Modular Code Department of Mechanical Engineering • Top down program design • Basic Debugging These slides are a supplement to the book Numerical Methods with Matlab : Implementations and Applications , by Gerald W. Recktenwald, � 2000, Prentice-Hall, Upper Saddle River, NJ. These slides are c � c 2000 Gerald W. Recktenwald. The PDF version of these slides may be downloaded or stored or printed only for noncommercial, educational use. The repackaging or sale of these slides in any form, without written consent of the author, is prohibited. The latest version of this PDF file, along with other supplemental material for the book, can be found at www.prenhall.com/recktenwald . Version 0.9 October 10, 2000 NMM: Organizing and Debugging Matlab Programs page 1 Rationale Programming Style ( 1 ) Organized programs are. . . A consistent programming style gives your programs a visual familiarity that helps the reader quickly comprehend the • easier to maintain intention of the code. • easier to debug A programming style consists of • not much harder to write • Visual appearance of the code • Conventions used for variable names Debugging. . . • Documentation with comment statements • is inevitable • can be anticipated with good program design • can be done interactively with Matlab 5.x NMM: Organizing and Debugging Matlab Programs page 2 NMM: Organizing and Debugging Matlab Programs page 3

  2. Programming Style ( 2 ) Programming Style ( 3 ) Use visual layout to suggest organization Use meaningful variable names • Indent if...end and for...end blocks d = 5; d_in = 5; t = 0.02; thick = 0.02; • Blank lines separate major blocks of code r = d/2; r_in = d_in/2; r2 = r + t; r_out = r_in + thick; Example: Indent code for conditional structures and loops if condition 1 is true Follow Programming and Mathematical Conventions Block 1 Variable Typical usage names elseif condition 2 is true i , j , k Array subscripts, loop counters √− 1 with complex arithmetic Block 2 i , j m , n End of a sequence, i = 1 , . . . , n , end number of rows ( m ) and columns ( n ) in a matrix A , B generic matrix for i=1:length(x) x , y , z generic vectors Body of loop end Note: Consistency is more important than convention. NMM: Organizing and Debugging Matlab Programs page 4 NMM: Organizing and Debugging Matlab Programs page 5 Programming Style ( 4 ) Programming Style ( 5 ) Document code with comment statements Note: I prefer to avoid use of lower case “L” as a variable • Write comments as you write code, not after name. It looks a lot like the number “1”. Which of • Include a prologue that supports “help” the following statements assigns the value “1” to the lower case version of the variable “L”? • Assume that the code is going to be used more than once • Comments should be short notes that augment the meaning of the program statements: Do not parrot the code. l = 1; (or) 1 = l; • Comments alone do not create good code. ⊲ You cannot fix a bug by changing the comments NMM: Organizing and Debugging Matlab Programs page 6 NMM: Organizing and Debugging Matlab Programs page 7

  3. Example: Example: NMM: Organizing and Debugging Matlab Programs Output: Describes each output variable. Input: Describes each input variable. Synopsis: Lists the various ways in which the function can be Summary: One line description of what the function does. NMM: Organizing and Debugging Matlab Programs Tm = 65.4; L = 251e3; k = 0.23; rho = 810; cp = 2050; legend(’Data’,’Polynomial Curve Fit’); xlabel(’Temperature (C)’); plot(t,p,’o’,tfit,pfit,’--’); pfit = polyval(c,tfit); tfit = linspace(min(t),max(t)); % --- Evaluate curve fit and plot it along with original data Prologue Used in the NMM Toolbox called. Short comments at side of statements Comments at beginning of a block % % % % % Programming Style ( 6 ) melting temperature latent heat thermal conductivity, density of liquid or solid paraffin specific heat of solid and liquid paraffin ylabel(’Pressure (J/kg) (C) (W/m/C) (MPa)’); (kg/m^3) (J/kg/K) page 10 page 8 NMM: Organizing and Debugging Matlab Programs • All text from the second line up to the first non-comment is • Second line must be a comment statement • First line of a function is the definition printed in response to Function Prologue help functionName No blank lines between function definition and first comment Supporting On-line Help First line of the file must statement in the prologue be the function definition. function rho = H2Odensity(T,units) First line of the prologue % H2Odensity Density of saturated liquid water is a terse but complete % % Synopsis: rho = H2Odensity description of the function. % rho = H2Odensity(T) % rho = H2Odensity(T,units) % % Input: T = (optional) temperature at which density is evaluated T and units are optional % Default: T = 20C. If units='F' then T is degrees F input variables as indicated % units = (optional) units for input temperature, Default = 'C' % units = 'C' for Celsius, units = 'F' for Fahrenheit by the synopsis. % % Output: rho = density, kg/m^3 if units = 'C', or lbm/ft^3 if units = 'F' % Notes: Use 4th order polynomial curve fit of data in Table B.2 % (Appendix B) of "Fundamentals of Fluid Mechanics", % B.R. Munson, et al., 2nd edition, 1994, Wiley and Sons, NY This comment will not be printed when the user types “ help H2Odensity ” because it is separated from the prologue by a blank line. page 9 NMM: Organizing and Debugging Matlab Programs page 11

  4. Modular Code ( 1 ) Modular Code ( 2 ) A module should be dedicated to one task Reuse modules • Flexibility is provided by input/output parameters • Debug once, use again • Minimize duplication of code • Any improvements are available to all programs using that module General purpose modules need. . . • Error messages must be meaningful so that user of general • Description of input/output parameters purpose routine understands the problem • Meaningful error messages so that user understands the problem Organization takes experience • Goal is not to maximize the number of m-files • Organization will evolve on complex projects NMM: Organizing and Debugging Matlab Programs page 12 NMM: Organizing and Debugging Matlab Programs page 13 Example: Built-in Bessel functions ( 1 ) Example: Built-in Bessel functions ( 2 ) The Bessel functions are solutions to Rather than repeat the code that computes J ν ( z ) and Γ( z ) , these fundamental functions are part of a core routine that gets z 2 d 2 y dz 2 + zdy dz − ( z 2 + ν 2 ) y = 0 evaluated via an interface function. >> lookfor bessel The Bessel function of the first kind is BESSCHK Check arguments to bessel functions. BESSEL Bessel functions of various kinds. � z 2 BESSELA Obsolete Bessel function. � k ∞ BESSELH Bessel function of the third kind (Hankel function). � z 4 � ν � J ν ( z ) = BESSELI Modified Bessel function of the first kind. 2 k ! Γ( ν + k + 1) BESSELJ Bessel function of the first kind. k =0 BESSELK Modified Bessel function of the second kind. BESSELY Bessel function of the second kind. where ν is a real number, z is complex, i = √− 1 and BESSLDEM Driver function for Bessel zero finding. BESSLODE Bessel’s equation of order 0 used by BESSLDEM. ∞ � e − t t z − 1 dt Γ( z ) = 0 Other Bessel functions (which are also solutions to the ODE) are defined in terms of J ν ( z ) . NMM: Organizing and Debugging Matlab Programs page 14 NMM: Organizing and Debugging Matlab Programs page 15

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend