Download presentation
Presentation is loading. Please wait.
Published byMolly Foster Modified over 8 years ago
1
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING
2
Software Engineering Goals: Understand difficulties in solving large programming problems Develop effective methods for writing large programs How do we define a good program? What is a bug? Why is SW Engineering important?
3
Top-Down Design Should we start solving a problem by coding the solution immediately? CASE: Computer Aided SW Engineering Decomposition: divide problem to sub problems Stepwise refinement: repeated application of decomposition
4
Top-Down Design 1. State the problem 2. Define the inputs and outputs 3. Design the algorithm a) Decompose into subproblems b) Perform stepwise refinement 4. Convert the algorithm into a Matlab program 5. Test the program Which step is the most difficult and why? What is an iterative process?
5
Script Files Script files text files (created with a text editor) contain many lines of MATLAB expressions and assignments. You can execute all of the lines of the file (sequentially) by typing the filename at the MATLAB prompt. If filename is computetraj.m, then >> computetraj causes the instructions (ie., the MATLAB commands) in the file to be run.
6
Debugging your programs Is it possible to have errors in your program? Three different types of errors are Syntax errors: you simply have typed an illegal expression, independent of the values of the variables in the expression. Run-time errors: logic errors that result in an illegal expression for specific values of the data (harder to fix). Logic errors that result in the program executing completely, but the answer that they return is incorrect (hardest to fix).
7
Finding run-time errors (bugs) The most primitive way to debug a program is to Work out (by “hand”) Remove the trailing semicolons ; Call your program, and compare the printouts (your program) to your hand-calculations
8
Finding run-time errors (bugs) A better (but ultimately the same) way is to Work out (by “hand”) Insert the command keyboard at some point in your program. This is called “setting a breakpoint” Run the program When execution gets to the keyboard line program execution is temporarily stopped the Command window appears, the prompt is K>> control it at the keyboard (ie., you can type) Type expressions at the K>> prompt You are a detective at this point. Type K>> return (resume execution) or K>> dbquit (exit)
9
Single-line stepping through an m-file Use dbstep to step through an M-file a line-at-a-time To execute the next line of code, type K>> dbstep To execute the next 6 lines of code, type K>> dbstep 6 To execute code until another m-file function is called, and then to “stop inside” that function at its first line, type K>> dbstep in To execute code until the m-file function exits, and then to stop inside the m-file function that called it, type K>> dbstep out After any of these, the “instruction pointer” moves to the next line of code which has not executed.
10
Stopping in an M-file Use dbstop to stop in an M-file at a certain line if a condition is true. To stop in loancalc at line 11 if the variable G is less than 1 K>> dbstop in loancalc at 11 if G<1 This sets a conditional breakpoint.
11
Clearing breakpoints All breakpoints can be cleared with the command >> dbclear all You can also selectively clear breakpoints that were created with dbstop. See >> help dbstop >> help dbclear for more information.
12
Using dbup and dbdown Normally at a breakpoint execution is temporarily stopped the K>> prompt appears in the command window the variables in the function’s workspace can be examined. The command dbup changes the scope of visible variables to the caller’s workspace. K>> % examine variables in function’s workspace K>> dbup K>> % examine variables in caller’s workspace K>> dbdown K>> % examine variables in function’s workspace
13
Seeing all of the function instances Use dbstack to see the list of all function instances that are running. The top line gives the function instance where the dbstack command occurred. The (n+1)’th line is the function instance of the function which called the function instance listed on the n’th line.
14
Profiling your code When running a program, Matlab can keep track of How many times a single line is executed How many times a function is called How much total time is spent executing a specific line of code How much time is spent running a specific function Why is this information useful? >> profile on >> myprogram >> profile report
15
QUESTIONS??
16
Resources “Introduction to Programming with Matlab”, J. Michael Fitzpatrick and John D. Crocetti Lecture slides E77, Andy Packard, http://jagger.me.berkeley.edu/~pack/e77 http://jagger.me.berkeley.edu/~pack/e77
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.