Learning Objectives

At the end of this class you should be able to

Reading in 5th edition of textbook

Reading in 6th edition of textbook

Download

The MATLAB Debugger

The MATLAB debugger is a powerful tool for finding errors in your code. Rather than spend much time on it during class, please watch an excellent screencast by Illya Michaelson. Michaelson has full set of MATLAB tutorials on YouTube.

Another good resource is Robert Talbert's MATLAB Tutorials on YouTube.

Comments on lecture content

if constructs are essential for writing code that can alter the path of execution based on conditions that may change while the code is executing. Here is a simple answer to print a warning if x becomes negative

if x<0
  fprinf('Warning: x = %g is negative\n',x)
end

We have also used several MATLAB functions that run with a variable number of input arguments. For example, the drawCircle function can be used with 1, 2, 3 or 4 input arguments

function drawCircle(r,x0,y0,line_style)
% drawCircle  Draw a circle in the (x,y) plane
%
% Synopsis:  drawCircle(r)
%            drawCircle(r,x0)
%            drawCircle(r,x0,y0)
%            drawCircle(r,x0,y0,line_style)
%
% Input:  r = radius of the circle
%         x0,y0 = (optional) x and y coordinates of center of the circle
%                 Default:  x0 = 0,  y0 = 0;
%         line_style = (optional) string used to specify the line style
%                      of the circle.  Default: line_style = '-' draw
%                      the circle with a solid line

if nargin<2, x0 = 0;  end
if nargin<3, y0 = 0;  end
if nargin<4, line_style = '-';  end

t = linspace(0,2*pi);
x = x0 + r*cos(t);
y = y0 + r*sin(t);
plot(x,y,line_style)

end

In the preceding code, three one-line if constructs determine whether default values need to be provided for x0, y0 and line_style.

if nargin<2, x0 = 0;  end
if nargin<3, y0 = 0;  end
if nargin<4, line_style = '-';  end

Both the for loop structure and while loop structures are used to repeat a sequence of calculations. After a brief review of for loops, we focus on while loops.