1 / 14

ME 2016: Computing Techniques

Learning Objectives Learn how to write correct and reliable Matlab code Learn how to debug Matlab code. ME 2016: Computing Techniques. Designing and Debugging Matlab Code Understand The Matlab graphical Debugger Defensive coding practice Error trapping. How to Design a Program?.

gala
Télécharger la présentation

ME 2016: Computing Techniques

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. Learning Objectives Learn how to write correct and reliable Matlab code Learn how to debug Matlab code ME 2016: Computing Techniques • Designing and Debugging Matlab Code • Understand • The Matlab graphical Debugger • Defensive coding practice • Error trapping

  2. How to Design a Program? • Five Steps • State the problem clearly • Describe the input and output information • Work the problem by hand (or with a calculator) for a simple set of data • Develop a MATLAB solution • Test the code for a variety of data • Apply these 5 steps in a hierarchical fashion • 1) to top-level script • 2) to function called in script • 3) to functions called in these functions, etc.

  3. Steps for Achieving Bug-Free Code • Study the Matlab Syntax • Make flash cards for each Matlab command / function and a description of what this command does • Test your code extensively • Test every command • Instead of writing an entire function, test every command at the command prompt before you add it to your m-file • This is particularly useful when using commands that you are not yet 100% familiar with. • Test every function • Before you use your function (in a script or other function), make sure to test it extensively.Try to make it break!

  4. Defensive Programming • Check your code. Have you programmed defensively? • Initialized variables • Indented code and commented appropriately • Used descriptive variable names • Used parentheses if the precedence order may be ambiguous • Implemented reasonable error trapping • Named m-files correctly

  5. Debugging • So what if we have programmed defensively and our code still doesn't run? • Debug!! • Debugging is the process of locating and correcting errors in a computer program code. • Matlab includes several tools and techniques for debugging

  6. Debugging • How to find the value of a variable at the time of error? • Use fprintf or disp commands, or remove some semi-colons • Not always a good idea: creates lots of output on the screen; cumbersome to add and remove to your program • Use ">> dbstop if error" (my favorite) • At the command prompt, type "dbstop if error" (remains active for the entire Matlab session) • Matlab will jump into debugger as soon as an error occurs (as if you had use the "keyboard" command; or put in a breakpoint) • Advantage: you can look inside the function at the values of variables to see what went wrong • "help debug" for more information • Use the graphical debugger • Add some breakpoints, and float the mouse over the variables you want to investigate

  7. Clear all breakpoints Insert Breakpoint Step Step Out Exit debug mode PotentialBreakpoints Step In Continue Breakpoint Debugging - Using the Graphical Debugger

  8. Debugging • Read the error messages!!! • They contain very valuable information. • Example: What does the following message tell me? • ??? Error using ==> ^ • Matrix must be square. • Error in ==> C:\MATLAB6p1\work\load_torque.m • On line 32 ==> wind_resistance = 0.5*drag_coefficient*frontal_area*air_density * v^2; • Error in ==> C:\MATLAB6p1\work\torque_plot.m • On line 8 ==> lt(i,:) = load_torque(rpm,gear_ratios(i),10);

  9. Example: Find the bugs… • function torque = load_torque (rpm, gear_ratio, slope) • wheel_radius = 16 * 0.0254 / 2; % [m] (16" wheels) • drag_coefficient = 0.29; % dimensionless • frontal_area = 1.9; % [m2] • air_density = 1.29; % [kg/m3] • rolling_coefficient = 14.3; % [Ns/m] • mass = 1800; % [kg] includes driver and 3 passengers • g = 9.81; % in [m/s2] • v = rpm * 2 * pi * wheel_radius /( 60 * gear_ratio ); % in [m/s] • wind_resistance = 0.5*drag_coefficient*frontal_area*air_density * v^2; % in [N] • rolling_resistance = roling_coefficient * v; % in [N] • drag_force = wind_resistance + rolling_resistance; % in [N] • alpha = atang(slope/100); % in [radians] • gravitational_force = mass * g * sin(alpha); % in [N] • load_force = drag_force + gravitational_force; % in [N] • torque = load_force * wheel_radius / gear_ratio; % in [Nm] Warning: the code above contains bugs!

  10. Debugging • More Examples: • What do the following messages tell me? • ??? Undefined function or variable 'roling_coefficient'. • Error in ==> C:\MATLAB6p1\work\load_torque.m • On line 33 ==> rolling_resistance = roling_coefficient * v; • Error in ==> C:\MATLAB6p1\work\torque_plot.m • On line 8 ==> lt(i,:) = load_torque(rpm,gear_ratios(i),10); • ??? Undefined function or variable 'atang'. • Error in ==> C:\MATLAB6p1\work\load_torque.m • On line 37 ==> alpha = atang(slope/100); % in [radians] • Error in ==> C:\MATLAB6p1\work\torque_plot.m • On line 8 ==> lt(i,:) = load_torque(rpm,gear_ratios(i),10);

  11. Debugging • One more example: • What does the following message tell me? • Warning: Divide by zero. • > In C:\MATLAB6p1\work\load_torque.m at line 29 • In C:\MATLAB6p1\work\torque_plot.m at line 8 • Warning: Divide by zero. • > In C:\MATLAB6p1\work\load_torque.m at line 44 • In C:\MATLAB6p1\work\torque_plot.m at line 8

  12. Error Trapping • Now that we’ve finished coding it is important to make sure that we are trapping common errors. • Testing (Step 5) will help us catch errors that we may need to trap or address also. • Example Error: What happens if the gear_ratio is zero? … Try it. if gear_ratio == 0 error('The gear_ratio should be non-zero.'); end

  13. Error Trapping • The previous statement caught the error, but how • can we make it show the error without stopping? • Use the try/catch construct • Example: gear_ratio = 0; % for illustration purposes only… try load_torque = load_torque (rpm, gear_ratio, slope); disp(torque); catch fprintf('Something went wrong while computing the load torque'); load_torque = 0; end

  14. Summary • Designing and Debugging Matlab Programs • Learn how to write correct and reliable Matlab code • Learn how to debug Matlab code • The Matlab graphical Debugger • Defensive coding practice • Error trapping

More Related