Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structured Programming: Debugging and Practice by the end of this class you should be able to: debug a program using echo printing debug a program using.

Similar presentations


Presentation on theme: "Structured Programming: Debugging and Practice by the end of this class you should be able to: debug a program using echo printing debug a program using."— Presentation transcript:

1 Structured Programming: Debugging and Practice by the end of this class you should be able to: debug a program using echo printing debug a program using MATLAB’s debugger Text Section: 4.7 Please Download: fun1.m, fun2.m, and invest.m from class website

2 Exercise: Develop the following guessing game program. 1.Player starts with 10 points 2.The program randomly picks a number from 1 to 100. N.B. code for picking this number is : n = ceil(rand*100) 3.The program asks player to guess a value – player enters a number 4.Program tells player they are high, low or correct. If correct game is over and score is displayed If incorrect they lose one point and go back to #3 Print out a copy of your program and a copy of some game play including validation (how can you be sure this program is working correctly?)

3 Optimization Logical Errors Syntax Errors Debugging

4 Syntax Errors: Respond to flags in editor and error messages during execution (you have been doing this for a while.) Logical Errors: 1.Validation: Check Logic Compare final result to reference results (hand calc.) & identify problems. 2.Modular Programming: Build and test small pieces 3.Intermediate Results: make intermediate results visible and compare to expected intermediate results (usually a hand calc.) Optimization: will leave for now. We have made occasional referrals to optimal code

5 Debugging Logical Errors Key tool  make intermediate results visible and compare to expected intermediate results Remove ; or use disp() run functions as scripts by: –commenting out the function definition line –make sure inputs are in the workspace –run as script –check variables in workspace Use debugger

6 MATLAB editor debugger We will work through example starting on pg 231 When reading the debugger section it is best to start here, then go back and read the first part of the section 4.7. –we will use these two functions (available on the website) fun1.m function y=fun1(x) avg=sum(x)/length(x); y=fun2(avg,x); fun2.m function above=fun2(x,avg) above=length(find(x>avg)); –These functions are intended to determine how many values are greater than the average in a vector.

7 Test case x =[1,2,3,4,10] What is the average? = 20/5 = 4 How many values are greater than the average = 1 Run with these functions >> fun1([1,2,3,4,10]) >> ans = 3  not the correct answer Using the debugger –with both files open select Desktop  Dock –Then select a tile arrangement from far right menu bar –set a breakpoint (stopping point) at function call line and at calculation in second program

8 re run, notice what happens –program stops –look at workspace windows –use “stack” to switch workplace windows –(notice separate workspaces) –notice average is correct = 4 can also print value in command window –can be changed at this point avg = 3 not a problem  continue to finish program remove break and read at line 2 of program 2 –notice work space –x is not a vector, avg is –x and avg reversed in function call Can fix and continue should get the correct answer

9 Can also use on loops (example pg. 233) Supplementary – not covered in lecture Enter program: invest.m (available on website) function z=invest(x,r) z = 0; y = 1 + 0.01*r; for k = 1:length(y) z = z*y + x(k); end Program is intended to look at an investment pattern after a number of years. x is a vector of annual dollar amounts invested. r is the % interest rate.

10 Check with values in table below Run Program Gives the wrong answer Set breakpoint before statement in loop Check values, step, check values, step … YearInvestmentBalance CalculationBalance 1 1000 2 15001000*(1.1) + 15002600 3 20002600*(1.1) + 20004860

11 Practice Problem: Fibonacci sequence A Fibonacci sequence is one where each successive number is the sum of the previous two numbers. E.g. if a sequences starts with 1, 1 the Fibonacci sequence would be: 1, 1, 2, 3, 5, 8, 13, 21 … Fibonacci sequences can begin with any two numbers. Develop a function that produces a Fibonacci sequence given the first two numbers and the number of elements in the sequence.

12 Problem Goal (brief): Develop a program that will produce a Fibonacci sequence given two starting numbers and the length of the series. Inputs: NameDescriptionUnits or ValuesInput Source * N1First number in seriesIntegerCommand line N2Second number in seriesIntegerCommand line NNumber of terms in seriesIntegerCommand line Outputs: NameDescriptionUnits or ValuesOutput type * FSFibonacci SeriesNACommand line Test Case: see example case from slide Program Logic : Can you develop a flow chart for this problem?

13 function FS = Fib(n1, n2, N) % function FS = Fib(n1, n2, N) % This function will create a Fibonacci Series. Fibonacci Sequences are % series where each element is the sum of the previous two elements % S. Scott Moor April 2008 % % Input Variables: n1 = the first number in the series (integer) % n2 = the second number in the series (integer) % N = number of terms in the series (integer) % Output Variable: FS = Resulting Fibonacci Series % Other Variables k = loop index and current term in series % Preallocate the series and place the first two terms in the series. FS = zeros(1,N); FS([1,2]) = [n1, n2]; % Each iteration of this loop creates the next term in the series for % terms 3 through N. for k = 3:N FS(k) = FS(k-1)+FS(k-2); end Validation of function on test case: >> Fib(1,1,8) ans = 1 1 2 3 5 8 13 21


Download ppt "Structured Programming: Debugging and Practice by the end of this class you should be able to: debug a program using echo printing debug a program using."

Similar presentations


Ads by Google