egr 115 introduction to computing for engineers matlab basics 6: debugging in matlab monday 15 sept...

13
EGR 115 Introduction to Computing for Engineers MATLAB Basics 6: Debugging in MATLAB Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers

Upload: jocelyn-watts

Post on 19-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: EGR 115 Introduction to Computing for Engineers MATLAB Basics 6: Debugging in MATLAB Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers

EGR 115 Introduction to Computing for Engineers

MATLAB Basics 6: Debugging in MATLAB

Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers

Page 2: EGR 115 Introduction to Computing for Engineers MATLAB Basics 6: Debugging in MATLAB Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers

Lecture Outline

Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers

• Debugging in MATLAB• Symbolic calculations in MATLAB• Publishing a file in MATLAB

Slide 2 of 13

Page 3: EGR 115 Introduction to Computing for Engineers MATLAB Basics 6: Debugging in MATLAB Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers

MATLAB basicsDebugging in MATLAB

Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers

• Most human beings occasionally make errors

• Program errors are often referred to as “bugs”

• Debugging refers to the process of eradicating “bugs”

• There are three general classes of programming errors

Slide 3 of 13

Page 4: EGR 115 Introduction to Computing for Engineers MATLAB Basics 6: Debugging in MATLAB Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers

MATLAB basicsDebugging in MATLAB – Syntax Errors

Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers

• Types of programming errors Syntax Error – What students tend to think of when you

mention bugs. Found (& reported) by command window or compiler. Frequently typos!!

Examples:

>> Cos(pi)

The name of the MATAB function for Cosine is “cos(…)” NOT “Cos(…)”

o A(1)) = 5;

Slide 4 of 13

Page 5: EGR 115 Introduction to Computing for Engineers MATLAB Basics 6: Debugging in MATLAB Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers

MATLAB basicsDebugging in MATLAB – Syntax Errors

Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers

Syntax errors can typically be resolved by carefully reviewing the error messageo Sometimes the interpreter will make suggestions on how to

fix the problem!!>> A = 1 + 3 +

o The response is not always precise

Slide 5 of 13

Page 6: EGR 115 Introduction to Computing for Engineers MATLAB Basics 6: Debugging in MATLAB Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers

MATLAB basicsDebugging in MATLAB – Run-time Errors

Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers

Run-time Error – Error which is reported when the program is executed. Divide by zero, or multiply by ‘string’, …o Arithmetic type Run-Time Error:

>> log10(0)

o Indexing type Run-Time Error:

>> a = [ 1 2 3 ];

>> b = a(4)

Slide 6 of 13

Page 7: EGR 115 Introduction to Computing for Engineers MATLAB Basics 6: Debugging in MATLAB Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers

MATLAB basicsDebugging in MATLAB – Run-time Errors

Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers

o Assignment type Run-Time Error:>> a = [ 1 2 3 ];>> b = ones(3, 1);>> c = a + b;

“a” is a 1x3 row vector and “b” is a 3x1 column vector, hence, they are not compatible under addition!!

Slide 7 of 13

Page 8: EGR 115 Introduction to Computing for Engineers MATLAB Basics 6: Debugging in MATLAB Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers

MATLAB basicsDebugging in MATLAB – Logical Errors

Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers

Logical Error – Hardest error to find, not reported, program runs without reported error, but produces erroneous results.o We wish to test if the class should be over:

Check to see if the time is greater or equal to 4 pm.

>> Time = clock; % Obtain current year, month, date, hr, min, sec>> if (Time(4) <= 4 ) % Check to see if after 4pm>> disp('Class should be over'); % If after 4pm display the statement>> end

• We made another logical error!! • The hour variable is in 24 hr format => 4pm = 16

• We made a logical error!! • Should be >= not <= !!!

Slide 8 of 13

Page 9: EGR 115 Introduction to Computing for Engineers MATLAB Basics 6: Debugging in MATLAB Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers

MATLAB basicsDebugging in MATLAB

Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers

• MATLAB Debugger Breakpoints

o Allows the controlled step-by-step execution of code

Examining contents of variables

Escaping a “run-away” programo CTRL-C

Use example of “Hwk1_Soln.m”

Slide 9 of 13

Page 10: EGR 115 Introduction to Computing for Engineers MATLAB Basics 6: Debugging in MATLAB Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers

MATLAB basicsSymbolic Calculations in MATLAB

Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers

• MATLAB can perform symbolic calculations The Symbolic toolbox

>> syms t

>> y = sin(t)

>> diff(y, t)

• What is the symbolic result of ?

Define “t” as a symbolic variable

Slide 10 of 13

Page 11: EGR 115 Introduction to Computing for Engineers MATLAB Basics 6: Debugging in MATLAB Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers

MATLAB basicsPublishing a file in MATLAB

Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers

• MATLAB can “publish” the results of executing a *.m file Will include plots and results of calculations

Slide 11 of 13

Page 12: EGR 115 Introduction to Computing for Engineers MATLAB Basics 6: Debugging in MATLAB Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers

MATLAB basicsPublishing a file in MATLAB

Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers

Set the “Output file format” to “doc” before publishingo Input commands will not be processed

Comment “input(…)” related lines or hardcode the input

Use example of “Hwk2_Soln.m”

Slide 12 of 13

Page 13: EGR 115 Introduction to Computing for Engineers MATLAB Basics 6: Debugging in MATLAB Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers

Next Lecture

Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers

• 2D Plotting Additional features

Slide 13 of 13