This document summarizes key points from lecture 2 on chapters 2 and 3 of the textbook. It discusses plotting in MATLAB, including plotting arrays and functions, formatting plots, and using multiple axes and figures. It also covers m-files, describing scripts and functions, and control structures like for loops and if/else statements.
1. Lecture 2—Chapters 2 & 3
Outline
1 Making plots
Plotting Arrays
Plotting Functions
Formatting Plots
2 The m-file
Scripts
Functions
3 Control Structures
for loops
Boolean Expressions
if…then…else…end structures
while loops
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 1 / 49
2. The plot command
The most basic way to make a plot in Matlab is the command plot
>> x = linspace(1,10,10);
>> y = x .^ 3 - x .^ 2 + x - 1;
>> plot(x,y);
plot plots individual data points
contained in arrays x and y
The default is that all data are
connected by thin blue lines.
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 2 / 49
3. Basic plot formatting
You can control how the data appear by changing how you use
the plot command:
>> plot(x,y,’ko’)
1 2 3 4 5 6 7 8 9 10
0
100
200
300
400
500
600
700
800
900
1000
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 3 / 49
4. Basic plot formatting
You can control how the data appear by changing how you use
the plot command:
>> plot(x,y,’ko’)
1 2 3 4 5 6 7 8 9 10
0
100
200
300
400
500
600
700
800
900
1000 This example uses a format specifier
string
Color Line style Symbol
k black - o ◦
r red – s
g green : d ♦
b blue -. ^,v,<,> , , ,
See doc linespec for full list.
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 3 / 49
5. More plot formatting
Other things you can control:
>> plot(x,y,’kd-’,’linewidth’,2,’markersize’,12,...
’markerfacecolor’,’b’,’markeredgecolor’,’k’);
1 2 3 4 5 6 7 8 9 10
0
100
200
300
400
500
600
700
800
900
1000
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 4 / 49
6. Outline
1 Making plots
Plotting Arrays
Plotting Functions
Formatting Plots
2 The m-file
Scripts
Functions
3 Control Structures
for loops
Boolean Expressions
if…then…else…end structures
while loops
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 5 / 49
7. Limitations of plot
plot is best for plotting arrays of data.
To plot functions with plot, you must create an x array and then use
your function to create a y array.
>> x = [0:6];
>> y = cos(x);
>> plot(x,y);
Command line:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 6 / 49
8. Limitations of plot
plot is best for plotting arrays of data.
To plot functions with plot, you must create an x array and then use
your function to create a y array.
>> x = [0:6];
>> y = cos(x);
>> plot(x,y);
Command line:
0 1 2 3 4 5 6
−1
−0.8
−0.6
−0.4
−0.2
0
0.2
0.4
0.6
0.8
1
Smooth functions can look
rough without enough data
If you change your x-values,
you will have to update the
y-values
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 6 / 49
9. fplot
fplot will plot a function directly
Use a function handle:
>> myfunc = @(x) x.^2 * cos(x);
>> fplot( myfunc, [-pi pi], ’k--’);
Command line:
−3 −2 −1 0 1 2 3
−10
−8
−6
−4
−2
0
2
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 7 / 49
10. fplot
Shortcut — type the function in as a string
>> fplot( ’cos(x) * x’, [-pi pi], ’k--’);
Command line:
−3 −2 −1 0 1 2 3
−4
−3
−2
−1
0
1
2
3
4
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 8 / 49
11. Outline
1 Making plots
Plotting Arrays
Plotting Functions
Formatting Plots
2 The m-file
Scripts
Functions
3 Control Structures
for loops
Boolean Expressions
if…then…else…end structures
while loops
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 9 / 49
12. Adding labels
xlabel(’text’) Label the x-axis with text
ylabel(’text’) Label the y-axis with text
title (’text’) Give the axis the title text
text(x,y,’text’) Place text at the coordinates (x,y)
legend(’plot 1’,’plot 2’,…) Adds a legend with descriptive text for
each plot on an axis.
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 10 / 49
13. More than one plot on an axis
By default, when you make a plot, the last plot you made disappears.
>> plot(linspace(-10,10),linspace(-10,10).*linspace(-10,10));
Command line:
After 1st
plot command:
−10 −8 −6 −4 −2 0 2 4 6 8 10
0
10
20
30
40
50
60
70
80
90
100
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 11 / 49
14. More than one plot on an axis
By default, when you make a plot, the last plot you made disappears.
>> plot(linspace(-10,10),linspace(-10,10).*linspace(-10,10));
>> plot(linspace(-10,10),linspace(-10,10).*-linspace(-10,10));
Command line:
After 2nd
plot command:
−10 −8 −6 −4 −2 0 2 4 6 8 10
−100
−90
−80
−70
−60
−50
−40
−30
−20
−10
0
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 11 / 49
15. More than one plot on an axis
hold on causes plots to accumulate on an axis
>> plot(linspace(-10,10),linspace(-10,10).*linspace(-10,10));
>> hold on
Command line:
After 1st plot command:
−10 −8 −6 −4 −2 0 2 4 6 8 10
0
10
20
30
40
50
60
70
80
90
100
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 12 / 49
16. More than one plot on an axis
hold on causes plots to accumulate on an axis
>> plot(linspace(-10,10),linspace(-10,10).*linspace(-10,10));
>> hold on
>> plot(linspace(-10,10),linspace(-10,10).*-linspace(-10,10),’r’);
>> legend(’y = x^2’,’y = -x^2’)
Command line:
After 2nd plot command:
−10 −8 −6 −4 −2 0 2 4 6 8 10
−100
−80
−60
−40
−20
0
20
40
60
80
100
y = x2
y = −x2
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 12 / 49
17. More than one axis on a figure
subplot is used to make multiple axes in one figure window.
>> subplot(2,3,1)
Command line:
1 2 3
4 5 6
Axis 1 is active (will be plotted on)
The first two numbers indicate how many rows/columns of axes to
make. The last number makes one axis active.
>> subplot(2,3,5)
Command line:
1 2 3
4 5 6
Axis 5 is active (will be plotted on)
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 13 / 49
18. More than one axis on a figure
>> subplot(1,3,1)
>> fplot(@(x) sin(x) .* sin(x),[-pi,pi],’r’);
>> title(’sin^2’);
>> subplot(1,3,2)
5 >> fplot(@(x) sin(x) .* cos(x),[-pi,pi],’g’);
>> title(’sin * cos’);
>> subplot(1,3,3)
>> fplot(@(x) cos(x) .* cos(x),[-pi,pi],’b’);
>> title(’cos^2’);
Command line:
−2 0 2
0
0.2
0.4
0.6
0.8
1
sin2
−2 0 2
−0.5
0
0.5
sin * cos
−2 0 2
0
0.2
0.4
0.6
0.8
1
cos2
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 14 / 49
19. Outline
1 Making plots
Plotting Arrays
Plotting Functions
Formatting Plots
2 The m-file
Scripts
Functions
3 Control Structures
for loops
Boolean Expressions
if…then…else…end structures
while loops
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 15 / 49
20. What are m-files?
An m-file is a text file that Matlab recognizes as a command.
There are two varieties of m-file, the script and the function
Begin by opening the Editor window:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 16 / 49
21. The Editor window
This is where you make an m-file
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 17 / 49
22. The Editor window
This is where you enter Matlab commands
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 17 / 49
23. The Editor window
These buttons can help you debug an m-file
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 17 / 49
24. The Editor window
These buttons help you navigate large files
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 17 / 49
25. The Editor window
These buttons help you write readable code. Comment, uncomment,
indent, and insert templates.
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 17 / 49
26. Scripts
The basic task of an m-file is to operate as a script
A script executes its contents, line-by-line
A script has Base scope. That is, it modifies and can access any
variable showing in the Workspace window.
Remember, the Workspace window shows a
list of variables Matlab currently knows
about. At the start, the Workspace window is
empty.
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 18 / 49
27. Scripts
The basic task of an m-file is to operate as a script
A script executes its contents, line-by-line
A script has Base scope. That is, it modifies and can access any
variable showing in the Workspace window.
Remember, the Workspace window shows a
list of variables Matlab currently knows
about. At the start, the Workspace window is
empty.
Now, write and execute basic.m
a = 1;
b = 2;
c = 3;
Script-file: basic.m
>> basic
Command line:
The effect of basic.m is to create a, b, and c
in the Workspace
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 18 / 49
28. Scripts
The basic task of an m-file is to operate as a script
A script executes its contents, line-by-line
A script has Base scope. That is, it modifies and can access any
variable showing in the Workspace window.
Remember, the Workspace window shows a
list of variables Matlab currently knows
about. At the start, the Workspace window is
empty.
Now, write and execute basic.m
a = 1;
b = 2;
c = 3;
Script-file: basic.m
>> basic
Command line:
The effect of basic.m is to create a, b, and c
in the Workspace
Script variables affect the Workspace (or
Base) environment
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 18 / 49
29. Scripts—scope
Scripts can create and use variables in the Workspace window.
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 19 / 49
30. Scripts—scope
Scripts can create and use variables in the Workspace window.
Write and execute simplecalc.m
result = a * b + c
Script-file: simplecalc.m
>> simplecalc
result = 5
Command line: Running simplecalc.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 19 / 49
31. Scripts—scope
Scripts can create and use variables in the Workspace window.
Write and execute simplecalc.m
result = a * b + c
Script-file: simplecalc.m
>> simplecalc
result = 5
Command line: Running simplecalc.m
Now change a, b, and c
>> a = 10; b = 20; c = 30;
Command line: Change a, b, & c
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 19 / 49
32. Scripts—scope
Scripts can create and use variables in the Workspace window.
Write and execute simplecalc.m
result = a * b + c
Script-file: simplecalc.m
>> simplecalc
result = 5
Command line: Running simplecalc.m
Now change a, b, and c
>> a = 10; b = 20; c = 30;
Command line: Change a, b, & c
Now re-run simplecalc
>> simplecalc
result = 230
Command line: Running simplecalc
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 19 / 49
33. Outline
1 Making plots
Plotting Arrays
Plotting Functions
Formatting Plots
2 The m-file
Scripts
Functions
3 Control Structures
for loops
Boolean Expressions
if…then…else…end structures
while loops
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 20 / 49
34. What is a function?
Functions
A set of commands, executed
1 at a time
Scripts
A set of commands, executed
1 at a time
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 21 / 49
35. What is a function?
Functions
A set of commands, executed
1 at a time
Cannot use variables already
defined in the Workspace
Scripts
A set of commands, executed
1 at a time
Can use variables already
defined in the Workspace
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 21 / 49
36. What is a function?
Functions
A set of commands, executed
1 at a time
Cannot use variables already
defined in the Workspace
Cannot change Workspace
variables
Scripts
A set of commands, executed
1 at a time
Can use variables already
defined in the Workspace
Changes to script variables
effect Workspace variables
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 21 / 49
37. What is a function?
Functions
A set of commands, executed
1 at a time
Cannot use variables already
defined in the Workspace
Cannot change Workspace
variables
Accepts arguments that
change its behavior
Scripts
A set of commands, executed
1 at a time
Can use variables already
defined in the Workspace
Changes to script variables
effect Workspace variables
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 21 / 49
38. What is a function?
Functions
A set of commands, executed
1 at a time
Cannot use variables already
defined in the Workspace
Cannot change Workspace
variables
Accepts arguments that
change its behavior
Produces outputs intended for
later use
Scripts
A set of commands, executed
1 at a time
Can use variables already
defined in the Workspace
Changes to script variables
effect Workspace variables
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 21 / 49
39. How to make a function
function
First line of m-file
function_name
Name of your function
Functions need their own variables:
function basicfunc
d = a * b + c
Function-file: basicfunc.m
>> basicfunc
??? Undefined function or variable ’a’.
Command line: Using basicfunc.m incorrectly
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 22 / 49
40. How to make a function
function
First line of m-file
function_name
Name of your function
Variables within a function have local scope:
function basicfunc2
a = 3; b = 1; c = 2;
d = a * b + c
Function-file: basicfunc2.m
>> basicfunc2
d = 5
>> [a b c ]
ans = 1 2 3
5 >> d
??? Undefined function or variable ’d’.
Command line: Using basicfunc.m correctly. Pay attention to scope!
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 22 / 49
41. Naming functions
Good programming practice
Functions are used by their m-file name
NOT the name in the m-file
function badname
disp(’I am mis-named’);
Function-file: misnamed.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 23 / 49
42. Naming functions
Good programming practice
Functions are used by their m-file name
NOT the name in the m-file
function badname
disp(’I am mis-named’);
Function-file: misnamed.m
>> badname
??? Undefined function or variable ’badname’.
>> misnamed
I am mis-named
Command line:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 23 / 49
43. Functions & Input Arguments
Functions can accept input arguments
function inputfunc (input1, input2)
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 24 / 49
44. Functions & Input Arguments
Functions can accept input arguments
function inputfunc (input1, input2)
function inputfunc(input1,input2)
result = input1 + input2
Function-file: inputfunc.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 24 / 49
45. Functions & Input Arguments
Functions can accept input arguments
function inputfunc (input1, input2)
function inputfunc(input1,input2)
result = input1 + input2
Function-file: inputfunc.m
Unless you do something special, a function with inputs will fail at
the command line without its arguments
>> inputfunc
??? Input argument ”input1” is undefined.
Error in ==> inputfunc at 2
result = input1 + input2
5 >> inputfunc(1,2)
>>
Command line: Using input_func
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 24 / 49
46. Functions & Input Arguments
function inputfunc(input1,input2)
result = input1 + input2
Function-file: inputfunc.m
5 >> inputfunc(1,2)
>>
Command line: Using input_func
1
2
inputfunc.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 25 / 49
47. Functions & Input Arguments
function inputfunc(input1,input2)
result = input1 + input2
Function-file: inputfunc.m
5 >> inputfunc(1,2)
>>
Command line: Using input_func
1
2
inputfunc.m arguments get copied into the function
workspace
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 25 / 49
48. Functions & Input Arguments
function inputfunc(input1,input2)
result = input1 + input2
Function-file: inputfunc.m
5 >> inputfunc(1,2)
>>
Command line: Using input_func
1
2
inputfunc.m
input1
1
input2
2
arguments get copied into the function
workspace
This workspace is separate from the
Workspace window
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 25 / 49
49. Functions & Input Arguments
function inputfunc(input1,input2)
result = input1 + input2
Function-file: inputfunc.m
5 >> inputfunc(1,2)
>>
Command line: Using input_func
1
2
inputfunc.m
input1
1
input2
2
result
3
arguments get copied into the function
workspace
This workspace is separate from the
Workspace window
The result is trapped inside of the
function.
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 25 / 49
50. Functions & Input Arguments
function inputfunc(input1,input2)
result = input1 + input2
Function-file: inputfunc.m
5 >> inputfunc(1,2)
>>
Command line: Using input_func
1
2
inputfunc.m
input1
1
input2
2
result
3
arguments get copied into the function
workspace
This workspace is separate from the
Workspace window
The result is trapped inside of the
function.
We can’t use it yet outside of the function!
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 25 / 49
51. Adding outputs
For a function to communicate with the outside world, you must
provide an output
function result = outputfunc (input1,input2)
input1
1
input2
2
result1
3
outputfunc.m
1
2
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 26 / 49
52. Adding outputs
For a function to communicate with the outside world, you must
provide an output
function result = outputfunc (input1,input2)
Inputs go in
input1
1
input2
2
result1
3
outputfunc.m
1
2
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 26 / 49
53. Adding outputs
For a function to communicate with the outside world, you must
provide an output
function result =
Outputs come out
outputfunc (input1,input2)
input1
1
input2
2
result1
3
outputfunc.m
1
2
ans
3
function result = ...
outputfunc(input1,input2)
result = input1 + input2;
Function-file: outputfunc.m
>> outputfunc(1,2)
ans =
3
Command line:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 26 / 49
54. Adding outputs — Advanced
Multiple outputs
A function can create more than one output
function [ result1 result2 ] = many_output_func(input1,input2)
function [ result1 result2 ] = ...
many_output_func(input1,input2)
result1 = input1 + input2;
result2 = input1 .* ones(input2) + ...
5 input2 .* eye(input2);
end
Function-file: many_output_func.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 27 / 49
55. Adding outputs — Advanced
Multiple outputs
A function can create more than one output
function [ result1 result2 ] = many_output_func(input1,input2)
input1
1
input2
2
result1
3
result2
3 1
1 3
many_output_func.m
1
2
ans
3
function [ result1 result2 ] = ...
many_output_func(input1,input2)
result1 = input1 + input2;
result2 = input1 .* ones(input2) + ...
5 input2 .* eye(input2);
end
Function-file: many_output_func.m
Only the first output is ‘‘captured’’ by
default
>> many_output_func(1,2)
ans =
3
Command line:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 27 / 49
56. Adding outputs — Advanced
Multiple outputs
A function can create more than one output
function [ result1 result2 ] = many_output_func(input1,input2)
input1
1
input2
2
result1
3
result2
3 1
1 3
many_output_func.m
1
2
first
3
second
3 1
1 3
function [ result1 result2 ] = ...
many_output_func(input1,input2)
result1 = input1 + input2;
result2 = input1 .* ones(input2) + ...
5 input2 .* eye(input2);
end
Function-file: many_output_func.m
Now result1 ⇒ first and result2 ⇒
second
>> [first second] = many_output_func(1,2)
first =
3
second =
5 3 1
1 3
Command line:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 27 / 49
57. Delimiter Review
()
Parentheses are used for…
Grouping portions of expressions
a = 1 ./ (b .* c .* (d + e))
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 28 / 49
58. Delimiter Review
()
Parentheses are used for…
Grouping portions of expressions
a = 1 ./ (b .* c .* (d + e))
Assigning values to parts of arrays
A(1,2) = 3
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 28 / 49
59. Delimiter Review
()
Parentheses are used for…
Grouping portions of expressions
a = 1 ./ (b .* c .* (d + e))
Assigning values to parts of arrays
A(1,2) = 3
Accessing values from parts of arrays
b = A(3,4)
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 28 / 49
60. Delimiter Review
()
Parentheses are used for…
Grouping portions of expressions
a = 1 ./ (b .* c .* (d + e))
Assigning values to parts of arrays
A(1,2) = 3
Accessing values from parts of arrays
b = A(3,4)
Passing arguments to functions
result = sin(pi)
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 28 / 49
61. Delimiter Review
()
Parentheses are used for…
Grouping portions of expressions
a = 1 ./ (b .* c .* (d + e))
Assigning values to parts of arrays
A(1,2) = 3
Accessing values from parts of arrays
b = A(3,4)
Passing arguments to functions
result = sin(pi)
Forming the argument list for functions in an m-file
function output = myfunc(input1,input2)
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 28 / 49
62. Delimiter Review
[]
Square brackets are used for…
Creating arrays
A = [ 1 2 3 ];
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 29 / 49
63. Delimiter Review
[]
Square brackets are used for…
Creating arrays
A = [ 1 2 3 ];
Defining multiple function outputs
function [output1 output2] = multi_output_function(input1,input2)
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 29 / 49
64. Delimiter Review
[]
Square brackets are used for…
Creating arrays
A = [ 1 2 3 ];
Defining multiple function outputs
function [output1 output2] = multi_output_function(input1,input2)
Using multiple function outputs
>> [ A B ] = calc_a_and_b()
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 29 / 49
65. Delimiter Review
’ ’
Single quotes are used to
Start and stop strings
my_string = ’String’;
The double quote ” does not work.
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 30 / 49
66. Delimiter Review
{}
The curly braces are used for…
Creating cell arrays
cell_array = {’String 1’, [1;2;3;4], @(x) x.^2 };
Note — As above, cell arrays can store mixtures of any data type
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 31 / 49
67. Delimiter Review
{}
The curly braces are used for…
Creating cell arrays
cell_array = {’String 1’, [1;2;3;4], @(x) x.^2 };
Note — As above, cell arrays can store mixtures of any data type
Accessing cell arrays
>> my_string = cell_array{1}
my_string =
String 1
>> cell_array{3}(3)
5 ans =
9
Command line:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 31 / 49
68. Delimiter Review
…
The ellipsis (…) are used to:
Continue a line
function result = ...
outputfunc(input1,input2)
result = input1 + input2;
Function-file: outputfunc.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 32 / 49
69. Delimiter Review
;
The semicolon…
Supresses output
function result = ...
outputfunc(input1,input2)
result = input1 + input2;
Function-file: outputfunc.m
>> outputfunc(1,2)
ans =
3
>> outputfunc(1,2);
5 >>
Command line:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 33 / 49
70. Delimiter Review
;
The semicolon…
Supresses output
function result = ...
outputfunc(input1,input2)
result = input1 + input2;
Function-file: outputfunc.m
>> outputfunc(1,2)
ans =
3
>> outputfunc(1,2);
5 >>
Command line:
Makes a new row in an array
>> A = [ 1 2 3; 4 5 6 ];
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 33 / 49
71. Delimiter Review
;
The semicolon…
Supresses output
function result = ...
outputfunc(input1,input2)
result = input1 + input2;
Function-file: outputfunc.m
>> outputfunc(1,2)
ans =
3
>> outputfunc(1,2);
5 >>
Command line:
Makes a new row in an array
>> A = [ 1 2 3; 4 5 6 ];
Allows more than 1 command on a line
>> A = 2:5; B = 2.*A.^2; plot(A,B);
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 33 / 49
72. Delimiter Review
,
Separates arguments in functions (required)
function a = my_function(b,c,d)
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 34 / 49
73. Delimiter Review
,
Separates arguments in functions (required)
function a = my_function(b,c,d)
Can be used to separate multiple outputs in functions (optional)
function [ output1, output2 ] = multi_out(input1, input2)
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 34 / 49
74. Delimiter Review
,
Separates arguments in functions (required)
function a = my_function(b,c,d)
Can be used to separate multiple outputs in functions (optional)
function [ output1, output2 ] = multi_out(input1, input2)
Can be used to separate columns in arrays (optional)
>> A = [ 1, 2, 3; 4 5 6]
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 34 / 49
75. Outline
1 Making plots
Plotting Arrays
Plotting Functions
Formatting Plots
2 The m-file
Scripts
Functions
3 Control Structures
for loops
Boolean Expressions
if…then…else…end structures
while loops
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 35 / 49
76. for loops
Sometimes we know (or can calcuate) exactly how many times
through a loop we need.
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 36 / 49
77. for loops
Sometimes we know (or can calcuate) exactly how many times
through a loop we need.
We want to do something element-by-element to an array
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 36 / 49
78. for loops
Sometimes we know (or can calcuate) exactly how many times
through a loop we need.
We want to do something element-by-element to an array
We want exactly n iterations of some algorithm
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 36 / 49
79. for loops
Sometimes we know (or can calcuate) exactly how many times
through a loop we need.
We want to do something element-by-element to an array
We want exactly n iterations of some algorithm
We want to do something over a specific range of numbers.
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 36 / 49
80. for loops
Sometimes we know (or can calcuate) exactly how many times
through a loop we need.
We want to do something element-by-element to an array
We want exactly n iterations of some algorithm
We want to do something over a specific range of numbers.
These cases are easily coded using a for loop
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 36 / 49
81. for loops
Sometimes we know (or can calcuate) exactly how many times
through a loop we need.
We want to do something element-by-element to an array
We want exactly n iterations of some algorithm
We want to do something over a specific range of numbers.
These cases are easily coded using a for loop
Syntax:
for ii
iteration variable
= array_variable
...
Code
...
More Code
...
end
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 36 / 49
82. for loops
Sometimes we know (or can calcuate) exactly how many times
through a loop we need.
We want to do something element-by-element to an array
We want exactly n iterations of some algorithm
We want to do something over a specific range of numbers.
These cases are easily coded using a for loop
Syntax:
for ii
iteration variable
= array_variable
...
Code
...
More Code
...
end
Why ii rather than i?
Matlab uses the variables i and j to mean
√
−1
It is therefore good practice in Matlab to avoid
the use of i and j to avoid overlap with these
built-in variables.
Use ii/jj or I/J instead.
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 36 / 49
83. for loops — Example
Finite summations are the mathematical equivalent of for loops
3
i=1
i2
= 1 + 22
+ 32
= 14
>> sum_squares(3)
ans =
14
Command line:
function s = sum_squares(n)
% Check that the input argument is positive
if n < 0
error(’n must be a positive number’);
5 end
s = 1; % Initialize the sum
for ii = 2 : n
s = s + ii^2;
end
10 end
Function-file: sum_squares.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 37 / 49
84. for loops — Example
Finite summations are the mathematical equivalent of for loops
3
i=1
i2
= 1 + 22
+ 32
= 14
n
3
>> sum_squares(3)
ans =
14
Command line:
function s = sum_squares(n)
% Check that the input argument is positive
if n < 0
error(’n must be a positive number’);
5 end
s = 1; % Initialize the sum
for ii = 2 : n
s = s + ii^2;
end
10 end
Function-file: sum_squares.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 37 / 49
85. for loops — Example
Finite summations are the mathematical equivalent of for loops
3
i=1
i2
= 1 + 22
+ 32
= 14
n
3
>> sum_squares(3)
ans =
14
Command line:
function s = sum_squares(n)
% Check that the input argument is positive
if n < 0
error(’n must be a positive number’);
5 end
s = 1; % Initialize the sum
for ii = 2 : n
s = s + ii^2;
end
10 end
Function-file: sum_squares.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 37 / 49
86. for loops — Example
Finite summations are the mathematical equivalent of for loops
3
i=1
i2
= 1 + 22
+ 32
= 14
n
3
1:n
1 2 3
i
1
>> sum_squares(3)
ans =
14
Command line:
function s = sum_squares(n)
% Check that the input argument is positive
if n < 0
error(’n must be a positive number’);
5 end
s = 1; % Initialize the sum
for ii = 2 : n
s = s + ii^2;
end
10 end
Function-file: sum_squares.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 37 / 49
87. for loops — Example
Finite summations are the mathematical equivalent of for loops
3
i=1
i2
= 1 + 22
+ 32
= 14
n
3
1:n
1 2 3
i
1
>> sum_squares(3)
ans =
14
Command line:
function s = sum_squares(n)
% Check that the input argument is positive
if n < 0
error(’n must be a positive number’);
5 end
s = 1; % Initialize the sum
for ii = 2 : n
s = s + ii^2;
end
10 end
Function-file: sum_squares.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 37 / 49
88. for loops — Example
Finite summations are the mathematical equivalent of for loops
3
i=1
i2
= 1 + 22
+ 32
= 14
n
3
1:n
1 2 3
i
1
>> sum_squares(3)
ans =
14
Command line:
function s = sum_squares(n)
% Check that the input argument is positive
if n < 0
error(’n must be a positive number’);
5 end
s = 1; % Initialize the sum
for ii = 2 : n
s = s + ii^2;
end
10 end
Function-file: sum_squares.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 37 / 49
89. for loops — Example
Finite summations are the mathematical equivalent of for loops
3
i=1
i2
= 1 + 22
+ 32
= 14
n
3
1:n
1 2 3
i
2
>> sum_squares(3)
ans =
14
Command line:
function s = sum_squares(n)
% Check that the input argument is positive
if n < 0
error(’n must be a positive number’);
5 end
s = 1; % Initialize the sum
for ii = 2 : n
s = s + ii^2;
end
10 end
Function-file: sum_squares.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 37 / 49
90. for loops — Example
Finite summations are the mathematical equivalent of for loops
3
i=1
i2
= 1 + 22
+ 32
= 14
n
3
1:n
1 2 3
i
2
>> sum_squares(3)
ans =
14
Command line:
function s = sum_squares(n)
% Check that the input argument is positive
if n < 0
error(’n must be a positive number’);
5 end
s = 1; % Initialize the sum
for ii = 2 : n
s = s + ii^2;
end
10 end
Function-file: sum_squares.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 37 / 49
91. for loops — Example
Finite summations are the mathematical equivalent of for loops
3
i=1
i2
= 1 + 22
+ 32
= 14
n
3
1:n
1 2 3
i
2
>> sum_squares(3)
ans =
14
Command line:
function s = sum_squares(n)
% Check that the input argument is positive
if n < 0
error(’n must be a positive number’);
5 end
s = 1; % Initialize the sum
for ii = 2 : n
s = s + ii^2;
end
10 end
Function-file: sum_squares.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 37 / 49
92. for loops — Example
Finite summations are the mathematical equivalent of for loops
3
i=1
i2
= 1 + 22
+ 32
= 14
n
3
1:n
1 2 3
i
3
>> sum_squares(3)
ans =
14
Command line:
function s = sum_squares(n)
% Check that the input argument is positive
if n < 0
error(’n must be a positive number’);
5 end
s = 1; % Initialize the sum
for ii = 2 : n
s = s + ii^2;
end
10 end
Function-file: sum_squares.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 37 / 49
93. for loops — Example
Finite summations are the mathematical equivalent of for loops
3
i=1
i2
= 1 + 22
+ 32
= 14
n
3
1:n
1 2 3
i
3
>> sum_squares(3)
ans =
14
Command line:
function s = sum_squares(n)
% Check that the input argument is positive
if n < 0
error(’n must be a positive number’);
5 end
s = 1; % Initialize the sum
for ii = 2 : n
s = s + ii^2;
end
10 end
Function-file: sum_squares.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 37 / 49
94. for loops — Example
Finite summations are the mathematical equivalent of for loops
3
i=1
i2
= 1 + 22
+ 32
= 14
n
3
1:n
1 2 3
i
3
>> sum_squares(3)
ans =
14
Command line:
function s = sum_squares(n)
% Check that the input argument is positive
if n < 0
error(’n must be a positive number’);
5 end
s = 1; % Initialize the sum
for ii = 2 : n
s = s + ii^2;
end
10 end
Function-file: sum_squares.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 37 / 49
95. for loops — Example
Finite summations are the mathematical equivalent of for loops
3
i=1
i2
= 1 + 22
+ 32
= 14
n
3
1:n
1 2 3
i
3
>> sum_squares(3)
ans =
14
Command line:
function s = sum_squares(n)
% Check that the input argument is positive
if n < 0
error(’n must be a positive number’);
5 end
s = 1; % Initialize the sum
for ii = 2 : n
s = s + ii^2;
end
10 end
Function-file: sum_squares.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 37 / 49
96. for loops — Example
Finite summations are the mathematical equivalent of for loops
3
i=1
i2
= 1 + 22
+ 32
= 14
n
3
1:n
1 2 3
i
3
>> sum_squares(3)
ans =
14
Command line:
function s = sum_squares(n)
% Check that the input argument is positive
if n < 0
error(’n must be a positive number’);
5 end
s = 1; % Initialize the sum
for ii = 2 : n
s = s + ii^2;
end
10 end
Function-file: sum_squares.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 37 / 49
97. for loops
Example—Nested loops
Loops can be placed within other loops
ii
jj
M
>> nested_for
ans =
1 2
2 4
Command line:
function M = nested_for
M = zeros(2);
for ii = 1:2
for jj = 1:2
5 M(ii,jj) = ii * jj
end
end
end
Function-file: nested_for.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 38 / 49
98. for loops
Example—Nested loops
Loops can be placed within other loops
ii
jj
0 0
0 0
M
>> nested_for
ans =
1 2
2 4
Command line:
function M = nested_for
M = zeros(2);
for ii = 1:2
for jj = 1:2
5 M(ii,jj) = ii * jj
end
end
end
Function-file: nested_for.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 38 / 49
99. for loops
Example—Nested loops
Loops can be placed within other loops
1
ii
jj
0 0
0 0
M
>> nested_for
ans =
1 2
2 4
Command line:
function M = nested_for
M = zeros(2);
for ii = 1:2
for jj = 1:2
5 M(ii,jj) = ii * jj
end
end
end
Function-file: nested_for.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 38 / 49
100. for loops
Example—Nested loops
Loops can be placed within other loops
1
ii
1
jj
0 0
0 0
M
>> nested_for
ans =
1 2
2 4
Command line:
function M = nested_for
M = zeros(2);
for ii = 1:2
for jj = 1:2
5 M(ii,jj) = ii * jj
end
end
end
Function-file: nested_for.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 38 / 49
101. for loops
Example—Nested loops
Loops can be placed within other loops
1
ii
1
jj
0 0
0 0
M
>> nested_for
ans =
1 2
2 4
Command line:
function M = nested_for
M = zeros(2);
for ii = 1:2
for jj = 1:2
5 M(ii,jj) = ii * jj
end
end
end
Function-file: nested_for.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 38 / 49
102. for loops
Example—Nested loops
Loops can be placed within other loops
1
ii
1
jj
1 0
0 0
M
>> nested_for
ans =
1 2
2 4
Command line:
function M = nested_for
M = zeros(2);
for ii = 1:2
for jj = 1:2
5 M(ii,jj) = ii * jj
end
end
end
Function-file: nested_for.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 38 / 49
103. for loops
Example—Nested loops
Loops can be placed within other loops
1
ii
1
jj
1 0
0 0
M
>> nested_for
ans =
1 2
2 4
Command line:
function M = nested_for
M = zeros(2);
for ii = 1:2
for jj = 1:2
5 M(ii,jj) = ii * jj
end
end
end
Function-file: nested_for.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 38 / 49
104. for loops
Example—Nested loops
Loops can be placed within other loops
1
ii
2
jj
1 0
0 0
M
>> nested_for
ans =
1 2
2 4
Command line:
function M = nested_for
M = zeros(2);
for ii = 1:2
for jj = 1:2
5 M(ii,jj) = ii * jj
end
end
end
Function-file: nested_for.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 38 / 49
105. for loops
Example—Nested loops
Loops can be placed within other loops
1
ii
2
jj
1 0
0 0
M
>> nested_for
ans =
1 2
2 4
Command line:
function M = nested_for
M = zeros(2);
for ii = 1:2
for jj = 1:2
5 M(ii,jj) = ii * jj
end
end
end
Function-file: nested_for.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 38 / 49
106. for loops
Example—Nested loops
Loops can be placed within other loops
1
ii
2
jj
1 2
0 0
M
>> nested_for
ans =
1 2
2 4
Command line:
function M = nested_for
M = zeros(2);
for ii = 1:2
for jj = 1:2
5 M(ii,jj) = ii * jj
end
end
end
Function-file: nested_for.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 38 / 49
107. for loops
Example—Nested loops
Loops can be placed within other loops
1
ii
2
jj
1 2
0 0
M
>> nested_for
ans =
1 2
2 4
Command line:
function M = nested_for
M = zeros(2);
for ii = 1:2
for jj = 1:2
5 M(ii,jj) = ii * jj
end
end
end
Function-file: nested_for.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 38 / 49
108. for loops
Example—Nested loops
Loops can be placed within other loops
2
ii
2
jj
1 2
0 0
M
>> nested_for
ans =
1 2
2 4
Command line:
function M = nested_for
M = zeros(2);
for ii = 1:2
for jj = 1:2
5 M(ii,jj) = ii * jj
end
end
end
Function-file: nested_for.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 38 / 49
109. for loops
Example—Nested loops
Loops can be placed within other loops
2
ii
2
jj
1 2
0 0
M
>> nested_for
ans =
1 2
2 4
Command line:
function M = nested_for
M = zeros(2);
for ii = 1:2
for jj = 1:2
5 M(ii,jj) = ii * jj
end
end
end
Function-file: nested_for.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 38 / 49
110. for loops
Example—Nested loops
Loops can be placed within other loops
2
ii
1
jj
1 2
0 0
M
>> nested_for
ans =
1 2
2 4
Command line:
function M = nested_for
M = zeros(2);
for ii = 1:2
for jj = 1:2
5 M(ii,jj) = ii * jj
end
end
end
Function-file: nested_for.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 38 / 49
111. for loops
Example—Nested loops
Loops can be placed within other loops
2
ii
1
jj
1 2
0 0
M
>> nested_for
ans =
1 2
2 4
Command line:
function M = nested_for
M = zeros(2);
for ii = 1:2
for jj = 1:2
5 M(ii,jj) = ii * jj
end
end
end
Function-file: nested_for.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 38 / 49
112. for loops
Example—Nested loops
Loops can be placed within other loops
2
ii
1
jj
1 2
2 0
M
>> nested_for
ans =
1 2
2 4
Command line:
function M = nested_for
M = zeros(2);
for ii = 1:2
for jj = 1:2
5 M(ii,jj) = ii * jj
end
end
end
Function-file: nested_for.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 38 / 49
113. for loops
Example—Nested loops
Loops can be placed within other loops
2
ii
1
jj
1 2
2 0
M
>> nested_for
ans =
1 2
2 4
Command line:
function M = nested_for
M = zeros(2);
for ii = 1:2
for jj = 1:2
5 M(ii,jj) = ii * jj
end
end
end
Function-file: nested_for.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 38 / 49
114. for loops
Example—Nested loops
Loops can be placed within other loops
2
ii
2
jj
1 2
2 0
M
>> nested_for
ans =
1 2
2 4
Command line:
function M = nested_for
M = zeros(2);
for ii = 1:2
for jj = 1:2
5 M(ii,jj) = ii * jj
end
end
end
Function-file: nested_for.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 38 / 49
115. for loops
Example—Nested loops
Loops can be placed within other loops
2
ii
2
jj
1 2
2 0
M
>> nested_for
ans =
1 2
2 4
Command line:
function M = nested_for
M = zeros(2);
for ii = 1:2
for jj = 1:2
5 M(ii,jj) = ii * jj
end
end
end
Function-file: nested_for.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 38 / 49
116. for loops
Example—Nested loops
Loops can be placed within other loops
2
ii
2
jj
1 2
2 4
M
>> nested_for
ans =
1 2
2 4
Command line:
function M = nested_for
M = zeros(2);
for ii = 1:2
for jj = 1:2
5 M(ii,jj) = ii * jj
end
end
end
Function-file: nested_for.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 38 / 49
117. for loops
Example—Nested loops
Loops can be placed within other loops
2
ii
2
jj
1 2
2 4
M
>> nested_for
ans =
1 2
2 4
Command line:
function M = nested_for
M = zeros(2);
for ii = 1:2
for jj = 1:2
5 M(ii,jj) = ii * jj
end
end
end
Function-file: nested_for.m
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 38 / 49
118. Outline
1 Making plots
Plotting Arrays
Plotting Functions
Formatting Plots
2 The m-file
Scripts
Functions
3 Control Structures
for loops
Boolean Expressions
if…then…else…end structures
while loops
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 39 / 49
119. What is a boolean expression?
Definition
A is a mathematical expression that has only two possible outcomes
TRUE or FALSE
In Matlab
TRUE ≡ 1
FALSE ≡ 0
Boolean expressions are constructed from boolean operators.
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 40 / 49
120. Relational Operators (Comparison operators)
Definition
A relational operator returns a boolean result by comparing two numbers with
each other
Operator Description
<Numerical Expression> < <Numerical Expression> Less than
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 41 / 49
121. Relational Operators (Comparison operators)
Definition
A relational operator returns a boolean result by comparing two numbers with
each other
Operator Description
<Numerical Expression> < <Numerical Expression> Less than
<Numerical Expression> <= <Numerical Expression> Less than or equal to
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 41 / 49
122. Relational Operators (Comparison operators)
Definition
A relational operator returns a boolean result by comparing two numbers with
each other
Operator Description
<Numerical Expression> < <Numerical Expression> Less than
<Numerical Expression> <= <Numerical Expression> Less than or equal to
<Numerical Expression> > <Numerical Expression> Greater than
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 41 / 49
123. Relational Operators (Comparison operators)
Definition
A relational operator returns a boolean result by comparing two numbers with
each other
Operator Description
<Numerical Expression> < <Numerical Expression> Less than
<Numerical Expression> <= <Numerical Expression> Less than or equal to
<Numerical Expression> > <Numerical Expression> Greater than
<Numerical Expression> >= <Numerical Expression> Greater than or equal to
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 41 / 49
124. Relational Operators (Comparison operators)
Definition
A relational operator returns a boolean result by comparing two numbers with
each other
Operator Description
<Numerical Expression> < <Numerical Expression> Less than
<Numerical Expression> <= <Numerical Expression> Less than or equal to
<Numerical Expression> > <Numerical Expression> Greater than
<Numerical Expression> >= <Numerical Expression> Greater than or equal to
<Numerical Expression> == <Numerical Expression> Equal to
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 41 / 49
125. Relational Operators (Comparison operators)
Definition
A relational operator returns a boolean result by comparing two numbers with
each other
Operator Description
<Numerical Expression> < <Numerical Expression> Less than
<Numerical Expression> <= <Numerical Expression> Less than or equal to
<Numerical Expression> > <Numerical Expression> Greater than
<Numerical Expression> >= <Numerical Expression> Greater than or equal to
<Numerical Expression> == <Numerical Expression> Equal to
<Numerical Expression> ∼= <Numerical Expression> Not equal to
Both <Numerical Expression>s are evaluated before making the comparison
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 41 / 49
126. Relational Operators — Examples
Start with these variables:
>> a = .3;
>> b = .1;
>> c = .2;
Command line:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 42 / 49
127. Relational Operators — Examples
Start with these variables:
>> a = .3;
>> b = .1;
>> c = .2;
Command line:
Basic comparisons work as expected
>> c == 2 * b; % Be careful: ’==’ doesn’t always work as expected
5 ans =
1
>> a > b
ans =
1
10 >> a < b
ans =
0
>> a ~= b
ans =
15 1
Command line:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 42 / 49
128. Relational Operators — Examples
Start with these variables:
>> a = .3;
>> b = .1;
>> c = .2;
Command line:
Others do not
>> a - 3 * b == 0 % .3 - 3 * .1 equals 0 right?
ans =
0 % FALSE! Why?
Command line:
Why??
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 42 / 49
129. Relational Operators — Examples
Start with these variables:
>> a = .3;
>> b = .1;
>> c = .2;
Command line:
Others do not
>> a - 3 * b == 0 % .3 - 3 * .1 equals 0 right?
ans =
0 % FALSE! Why?
Command line:
Why??
Numerical errors mean that strict equalities between arithmetically
equivalent #s are not numerically equivalent
>> format long g % Force MATLAB to show full precision
20 >> a-3*b
ans =
-5.55111512312578e-017 % Very close to 0, but NOT EXACTLY 0
Command line:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 42 / 49
130. Relational Operators — Examples
Start with these variables:
>> a = .3;
>> b = .1;
>> c = .2;
Command line:
A better way to test for equality of 2 numerical expressions is to use
the abs function:
>> tol = 10^-15;
>> abs( (a-3*b) - 0 ) < tol
25
ans =
1 % The absolute value of (.3 - 3 * .1) is smaller than our tolerance
>> tol = 10^-17;
30 >> abs( (a-3*b) - 0 ) < tol
ans =
0
Command line:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 42 / 49
131. Logical Operators
Definition
A logical operator returns a boolean result based on the relationship between
one or two boolean expressions
Operator Name Result
∼ <Boolean Exp.> Not
∼ 0 = 1
∼ 1 = 0
<Boolean Exp.> && <Boolean Exp.> And
0 && 0 = 0 1 && 0 = 0
0 && 1 = 0 1 && 1 = 1
<Boolean Exp.> || <Boolean Exp.> Or
0 || 0 = 0 1 || 0 = 1
0 || 1 = 1 1 || 1 = 1
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 43 / 49
132. Outline
1 Making plots
Plotting Arrays
Plotting Functions
Formatting Plots
2 The m-file
Scripts
Functions
3 Control Structures
for loops
Boolean Expressions
if…then…else…end structures
while loops
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 44 / 49
133. The if statement
We often would like functions & scripts to behave differently based
on their input.
To look for and report errors
function root = square_root(number)
if(number < 0)
error(’The square root of a negative number is imaginary!’)
end
5 root = sqrt(number);
end
Function-file: Find the square root of non-negative numbers only
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 45 / 49
134. The if statement
We often would like functions & scripts to behave differently based
on their input.
To look for and report errors
function root = square_root(number)
if(number < 0)
error(’The square root of a negative number is imaginary!’)
end
5 root = sqrt(number);
end
Function-file: Find the square root of non-negative numbers only
To make a decision
function signs = signof(number)
if number < 0
signs = -1;
5 elseif number > 0
signs = 1;
else
signs = 0;
end
Function-file: Find the square root of non-negative numbers only
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 45 / 49
135. Outline
1 Making plots
Plotting Arrays
Plotting Functions
Formatting Plots
2 The m-file
Scripts
Functions
3 Control Structures
for loops
Boolean Expressions
if…then…else…end structures
while loops
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 46 / 49
136. while loops
The while loop is used to repeat a section of code while a boolean
expression evaluates to TRUE.
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 47 / 49
137. while loops
The while loop is used to repeat a section of code while a boolean
expression evaluates to TRUE.
Useful in numerical methods that need to iterate while the result still
needs improvement
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 47 / 49
138. while loops
The while loop is used to repeat a section of code while a boolean
expression evaluates to TRUE.
Useful in numerical methods that need to iterate while the result still
needs improvement
Usually a counter variable is introduced to keep track of the
number of times through the loop.
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 47 / 49
139. while Loops
A simple example
>> while_demo(3)
ans =
-0.5
Command line:
function result = while_demo(max_iter)
iter = 0;
result = 10;
5 while iter < max_iter && result > 0
result = result / 2 - 1;
iter = iter + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 48 / 49
140. while Loops
A simple example
max_iter
3 >> while_demo(3)
ans =
-0.5
Command line:
function result = while_demo(max_iter)
iter = 0;
result = 10;
5 while iter < max_iter && result > 0
result = result / 2 - 1;
iter = iter + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 48 / 49
141. while Loops
A simple example
max_iter
3
iter
0
>> while_demo(3)
ans =
-0.5
Command line:
function result = while_demo(max_iter)
iter = 0;
result = 10;
5 while iter < max_iter && result > 0
result = result / 2 - 1;
iter = iter + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 48 / 49
142. while Loops
A simple example
max_iter
3
iter
0
result
10
>> while_demo(3)
ans =
-0.5
Command line:
function result = while_demo(max_iter)
iter = 0;
result = 10;
5 while iter < max_iter && result > 0
result = result / 2 - 1;
iter = iter + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 48 / 49
143. while Loops
A simple example
max_iter
3
iter
0
result
10
>> while_demo(3)
ans =
-0.5
Command line:
function result = while_demo(max_iter)
iter = 0;
result = 10;
5 while iter < max_iter && result > 0
result = result / 2 - 1;
iter = iter + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 48 / 49
144. while Loops
A simple example
max_iter
3
iter
0
result
10
>> while_demo(3)
ans =
-0.5
Command line:
function result = while_demo(max_iter)
iter = 0;
result = 10;
5 while iter < max_iter && result > 0
result = result / 2 - 1;
iter = iter + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 48 / 49
145. while Loops
A simple example
max_iter
3
iter
0
result
4
>> while_demo(3)
ans =
-0.5
Command line:
function result = while_demo(max_iter)
iter = 0;
result = 10;
5 while iter < max_iter && result > 0
result = result / 2 - 1;
iter = iter + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 48 / 49
146. while Loops
A simple example
max_iter
3
iter
0
result
4
>> while_demo(3)
ans =
-0.5
Command line:
function result = while_demo(max_iter)
iter = 0;
result = 10;
5 while iter < max_iter && result > 0
result = result / 2 - 1;
iter = iter + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 48 / 49
147. while Loops
A simple example
max_iter
3
iter
1
result
4
>> while_demo(3)
ans =
-0.5
Command line:
function result = while_demo(max_iter)
iter = 0;
result = 10;
5 while iter < max_iter && result > 0
result = result / 2 - 1;
iter = iter + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 48 / 49
148. while Loops
A simple example
max_iter
3
iter
1
result
4
>> while_demo(3)
ans =
-0.5
Command line:
function result = while_demo(max_iter)
iter = 0;
result = 10;
5 while iter < max_iter && result > 0
result = result / 2 - 1;
iter = iter + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 48 / 49
149. while Loops
A simple example
max_iter
3
iter
1
result
4
>> while_demo(3)
ans =
-0.5
Command line:
function result = while_demo(max_iter)
iter = 0;
result = 10;
5 while iter < max_iter && result > 0
result = result / 2 - 1;
iter = iter + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 48 / 49
150. while Loops
A simple example
max_iter
3
iter
1
result
1
>> while_demo(3)
ans =
-0.5
Command line:
function result = while_demo(max_iter)
iter = 0;
result = 10;
5 while iter < max_iter && result > 0
result = result / 2 - 1;
iter = iter + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 48 / 49
151. while Loops
A simple example
max_iter
3
iter
1
result
1
>> while_demo(3)
ans =
-0.5
Command line:
function result = while_demo(max_iter)
iter = 0;
result = 10;
5 while iter < max_iter && result > 0
result = result / 2 - 1;
iter = iter + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 48 / 49
152. while Loops
A simple example
max_iter
3
iter
2
result
1
>> while_demo(3)
ans =
-0.5
Command line:
function result = while_demo(max_iter)
iter = 0;
result = 10;
5 while iter < max_iter && result > 0
result = result / 2 - 1;
iter = iter + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 48 / 49
153. while Loops
A simple example
max_iter
3
iter
2
result
1
>> while_demo(3)
ans =
-0.5
Command line:
function result = while_demo(max_iter)
iter = 0;
result = 10;
5 while iter < max_iter && result > 0
result = result / 2 - 1;
iter = iter + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 48 / 49
154. while Loops
A simple example
max_iter
3
iter
2
result
1
>> while_demo(3)
ans =
-0.5
Command line:
function result = while_demo(max_iter)
iter = 0;
result = 10;
5 while iter < max_iter && result > 0
result = result / 2 - 1;
iter = iter + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 48 / 49
155. while Loops
A simple example
max_iter
3
iter
2
result
-0.5
>> while_demo(3)
ans =
-0.5
Command line:
function result = while_demo(max_iter)
iter = 0;
result = 10;
5 while iter < max_iter && result > 0
result = result / 2 - 1;
iter = iter + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 48 / 49
156. while Loops
A simple example
max_iter
3
iter
2
result
-0.5
>> while_demo(3)
ans =
-0.5
Command line:
function result = while_demo(max_iter)
iter = 0;
result = 10;
5 while iter < max_iter && result > 0
result = result / 2 - 1;
iter = iter + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 48 / 49
157. while Loops
A simple example
max_iter
3
iter
3
result
-0.5
>> while_demo(3)
ans =
-0.5
Command line:
function result = while_demo(max_iter)
iter = 0;
result = 10;
5 while iter < max_iter && result > 0
result = result / 2 - 1;
iter = iter + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 48 / 49
158. while Loops
A simple example
max_iter
3
iter
3
result
-0.5
>> while_demo(3)
ans =
-0.5
Command line:
function result = while_demo(max_iter)
iter = 0;
result = 10;
5 while iter < max_iter && result > 0
result = result / 2 - 1;
iter = iter + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 48 / 49
159. while Loops
A simple example
max_iter
3
iter
3
result
-0.5
>> while_demo(3)
ans =
-0.5
Command line:
function result = while_demo(max_iter)
iter = 0;
result = 10;
5 while iter < max_iter && result > 0
result = result / 2 - 1;
iter = iter + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 48 / 49
160. while Loops
Another Example—estimating π
π can be evaluated numerically using the Gregory-Leibniz series:
π = 4
∞
i=0
(−1)i 1
2i + 1 >> [ pi_est n_iter ] = pi_estimate(12,.1)
pi_est =
3.2837
n_iter =
5 7
Command line:
estimate ii err
0.1
tol
lastestimate
12
maxiter
function [ estimate ii ]= pi_estimate(maxiter, tol)
% Initialization section
estimate = 0;
ii = 0;
5 err = tol + 1;
% Loop section
while err > tol && ii < maxiter
lastestimate = estimate;
estimate = estimate + 4*(-1)^ii / (2*ii + 1);
10 err = abs((estimate-lastestimate)/estimate);
ii = ii + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 49 / 49
161. while Loops
Another Example—estimating π
π can be evaluated numerically using the Gregory-Leibniz series:
π = 4
∞
i=0
(−1)i 1
2i + 1 >> [ pi_est n_iter ] = pi_estimate(12,.1)
pi_est =
3.2837
n_iter =
5 7
Command line:
0
estimate
ii err
0.1
tol
lastestimate
12
maxiter
function [ estimate ii ]= pi_estimate(maxiter, tol)
% Initialization section
estimate = 0;
ii = 0;
5 err = tol + 1;
% Loop section
while err > tol && ii < maxiter
lastestimate = estimate;
estimate = estimate + 4*(-1)^ii / (2*ii + 1);
10 err = abs((estimate-lastestimate)/estimate);
ii = ii + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 49 / 49
162. while Loops
Another Example—estimating π
π can be evaluated numerically using the Gregory-Leibniz series:
π = 4
∞
i=0
(−1)i 1
2i + 1 >> [ pi_est n_iter ] = pi_estimate(12,.1)
pi_est =
3.2837
n_iter =
5 7
Command line:
0
estimate
0
ii
err
0.1
tol
lastestimate
12
maxiter
function [ estimate ii ]= pi_estimate(maxiter, tol)
% Initialization section
estimate = 0;
ii = 0;
5 err = tol + 1;
% Loop section
while err > tol && ii < maxiter
lastestimate = estimate;
estimate = estimate + 4*(-1)^ii / (2*ii + 1);
10 err = abs((estimate-lastestimate)/estimate);
ii = ii + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 49 / 49
163. while Loops
Another Example—estimating π
π can be evaluated numerically using the Gregory-Leibniz series:
π = 4
∞
i=0
(−1)i 1
2i + 1 >> [ pi_est n_iter ] = pi_estimate(12,.1)
pi_est =
3.2837
n_iter =
5 7
Command line:
0
estimate
0
ii
1.1
err
0.1
tol
lastestimate
12
maxiter
function [ estimate ii ]= pi_estimate(maxiter, tol)
% Initialization section
estimate = 0;
ii = 0;
5 err = tol + 1;
% Loop section
while err > tol && ii < maxiter
lastestimate = estimate;
estimate = estimate + 4*(-1)^ii / (2*ii + 1);
10 err = abs((estimate-lastestimate)/estimate);
ii = ii + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 49 / 49
164. while Loops
Another Example—estimating π
π can be evaluated numerically using the Gregory-Leibniz series:
π = 4
∞
i=0
(−1)i 1
2i + 1 >> [ pi_est n_iter ] = pi_estimate(12,.1)
pi_est =
3.2837
n_iter =
5 7
Command line:
0
estimate
0
ii
1.1
err
0.1
tol
lastestimate
12
maxiter
function [ estimate ii ]= pi_estimate(maxiter, tol)
% Initialization section
estimate = 0;
ii = 0;
5 err = tol + 1;
% Loop section
while err > tol && ii < maxiter
lastestimate = estimate;
estimate = estimate + 4*(-1)^ii / (2*ii + 1);
10 err = abs((estimate-lastestimate)/estimate);
ii = ii + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 49 / 49
165. while Loops
Another Example—estimating π
π can be evaluated numerically using the Gregory-Leibniz series:
π = 4
∞
i=0
(−1)i 1
2i + 1 >> [ pi_est n_iter ] = pi_estimate(12,.1)
pi_est =
3.2837
n_iter =
5 7
Command line:
0
estimate
0
ii
1.1
err
0.1
tol
0
lastestimate
12
maxiter
function [ estimate ii ]= pi_estimate(maxiter, tol)
% Initialization section
estimate = 0;
ii = 0;
5 err = tol + 1;
% Loop section
while err > tol && ii < maxiter
lastestimate = estimate;
estimate = estimate + 4*(-1)^ii / (2*ii + 1);
10 err = abs((estimate-lastestimate)/estimate);
ii = ii + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 49 / 49
166. while Loops
Another Example—estimating π
π can be evaluated numerically using the Gregory-Leibniz series:
π = 4
∞
i=0
(−1)i 1
2i + 1 >> [ pi_est n_iter ] = pi_estimate(12,.1)
pi_est =
3.2837
n_iter =
5 7
Command line:
0
estimate
0
ii
1.1
err
0.1
tol
0
lastestimate
12
maxiter
function [ estimate ii ]= pi_estimate(maxiter, tol)
% Initialization section
estimate = 0;
ii = 0;
5 err = tol + 1;
% Loop section
while err > tol && ii < maxiter
lastestimate = estimate;
estimate = estimate + 4*(-1)^ii / (2*ii + 1);
10 err = abs((estimate-lastestimate)/estimate);
ii = ii + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 49 / 49
167. while Loops
Another Example—estimating π
π can be evaluated numerically using the Gregory-Leibniz series:
π = 4
∞
i=0
(−1)i 1
2i + 1 >> [ pi_est n_iter ] = pi_estimate(12,.1)
pi_est =
3.2837
n_iter =
5 7
Command line:
4
estimate
0
ii
1.1
err
0.1
tol
0
lastestimate
12
maxiter
function [ estimate ii ]= pi_estimate(maxiter, tol)
% Initialization section
estimate = 0;
ii = 0;
5 err = tol + 1;
% Loop section
while err > tol && ii < maxiter
lastestimate = estimate;
estimate = estimate + 4*(-1)^ii / (2*ii + 1);
10 err = abs((estimate-lastestimate)/estimate);
ii = ii + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 49 / 49
168. while Loops
Another Example—estimating π
π can be evaluated numerically using the Gregory-Leibniz series:
π = 4
∞
i=0
(−1)i 1
2i + 1 >> [ pi_est n_iter ] = pi_estimate(12,.1)
pi_est =
3.2837
n_iter =
5 7
Command line:
4
estimate
0
ii
1.1
err
0.1
tol
0
lastestimate
12
maxiter
function [ estimate ii ]= pi_estimate(maxiter, tol)
% Initialization section
estimate = 0;
ii = 0;
5 err = tol + 1;
% Loop section
while err > tol && ii < maxiter
lastestimate = estimate;
estimate = estimate + 4*(-1)^ii / (2*ii + 1);
10 err = abs((estimate-lastestimate)/estimate);
ii = ii + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 49 / 49
169. while Loops
Another Example—estimating π
π can be evaluated numerically using the Gregory-Leibniz series:
π = 4
∞
i=0
(−1)i 1
2i + 1 >> [ pi_est n_iter ] = pi_estimate(12,.1)
pi_est =
3.2837
n_iter =
5 7
Command line:
4
estimate
0
ii
1
err
0.1
tol
0
lastestimate
12
maxiter
function [ estimate ii ]= pi_estimate(maxiter, tol)
% Initialization section
estimate = 0;
ii = 0;
5 err = tol + 1;
% Loop section
while err > tol && ii < maxiter
lastestimate = estimate;
estimate = estimate + 4*(-1)^ii / (2*ii + 1);
10 err = abs((estimate-lastestimate)/estimate);
ii = ii + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 49 / 49
170. while Loops
Another Example—estimating π
π can be evaluated numerically using the Gregory-Leibniz series:
π = 4
∞
i=0
(−1)i 1
2i + 1 >> [ pi_est n_iter ] = pi_estimate(12,.1)
pi_est =
3.2837
n_iter =
5 7
Command line:
4
estimate
0
ii
1
err
0.1
tol
0
lastestimate
12
maxiter
function [ estimate ii ]= pi_estimate(maxiter, tol)
% Initialization section
estimate = 0;
ii = 0;
5 err = tol + 1;
% Loop section
while err > tol && ii < maxiter
lastestimate = estimate;
estimate = estimate + 4*(-1)^ii / (2*ii + 1);
10 err = abs((estimate-lastestimate)/estimate);
ii = ii + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 49 / 49
171. while Loops
Another Example—estimating π
π can be evaluated numerically using the Gregory-Leibniz series:
π = 4
∞
i=0
(−1)i 1
2i + 1 >> [ pi_est n_iter ] = pi_estimate(12,.1)
pi_est =
3.2837
n_iter =
5 7
Command line:
4
estimate
1
ii
1
err
0.1
tol
0
lastestimate
12
maxiter
function [ estimate ii ]= pi_estimate(maxiter, tol)
% Initialization section
estimate = 0;
ii = 0;
5 err = tol + 1;
% Loop section
while err > tol && ii < maxiter
lastestimate = estimate;
estimate = estimate + 4*(-1)^ii / (2*ii + 1);
10 err = abs((estimate-lastestimate)/estimate);
ii = ii + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 49 / 49
172. while Loops
Another Example—estimating π
π can be evaluated numerically using the Gregory-Leibniz series:
π = 4
∞
i=0
(−1)i 1
2i + 1 >> [ pi_est n_iter ] = pi_estimate(12,.1)
pi_est =
3.2837
n_iter =
5 7
Command line:
4
estimate
1
ii
1
err
0.1
tol
0
lastestimate
12
maxiter
function [ estimate ii ]= pi_estimate(maxiter, tol)
% Initialization section
estimate = 0;
ii = 0;
5 err = tol + 1;
% Loop section
while err > tol && ii < maxiter
lastestimate = estimate;
estimate = estimate + 4*(-1)^ii / (2*ii + 1);
10 err = abs((estimate-lastestimate)/estimate);
ii = ii + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 49 / 49
173. while Loops
Another Example—estimating π
π can be evaluated numerically using the Gregory-Leibniz series:
π = 4
∞
i=0
(−1)i 1
2i + 1 >> [ pi_est n_iter ] = pi_estimate(12,.1)
pi_est =
3.2837
n_iter =
5 7
Command line:
4
estimate
1
ii
1
err
0.1
tol
0
lastestimate
12
maxiter
function [ estimate ii ]= pi_estimate(maxiter, tol)
% Initialization section
estimate = 0;
ii = 0;
5 err = tol + 1;
% Loop section
while err > tol && ii < maxiter
lastestimate = estimate;
estimate = estimate + 4*(-1)^ii / (2*ii + 1);
10 err = abs((estimate-lastestimate)/estimate);
ii = ii + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 49 / 49
174. while Loops
Another Example—estimating π
π can be evaluated numerically using the Gregory-Leibniz series:
π = 4
∞
i=0
(−1)i 1
2i + 1 >> [ pi_est n_iter ] = pi_estimate(12,.1)
pi_est =
3.2837
n_iter =
5 7
Command line:
4
estimate
1
ii
1
err
0.1
tol
4
lastestimate
12
maxiter
function [ estimate ii ]= pi_estimate(maxiter, tol)
% Initialization section
estimate = 0;
ii = 0;
5 err = tol + 1;
% Loop section
while err > tol && ii < maxiter
lastestimate = estimate;
estimate = estimate + 4*(-1)^ii / (2*ii + 1);
10 err = abs((estimate-lastestimate)/estimate);
ii = ii + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 49 / 49
175. while Loops
Another Example—estimating π
π can be evaluated numerically using the Gregory-Leibniz series:
π = 4
∞
i=0
(−1)i 1
2i + 1 >> [ pi_est n_iter ] = pi_estimate(12,.1)
pi_est =
3.2837
n_iter =
5 7
Command line:
4
estimate
1
ii
1
err
0.1
tol
4
lastestimate
12
maxiter
function [ estimate ii ]= pi_estimate(maxiter, tol)
% Initialization section
estimate = 0;
ii = 0;
5 err = tol + 1;
% Loop section
while err > tol && ii < maxiter
lastestimate = estimate;
estimate = estimate + 4*(-1)^ii / (2*ii + 1);
10 err = abs((estimate-lastestimate)/estimate);
ii = ii + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 49 / 49
176. while Loops
Another Example—estimating π
π can be evaluated numerically using the Gregory-Leibniz series:
π = 4
∞
i=0
(−1)i 1
2i + 1 >> [ pi_est n_iter ] = pi_estimate(12,.1)
pi_est =
3.2837
n_iter =
5 7
Command line:
2.667
estimate
1
ii
1
err
0.1
tol
4
lastestimate
12
maxiter
function [ estimate ii ]= pi_estimate(maxiter, tol)
% Initialization section
estimate = 0;
ii = 0;
5 err = tol + 1;
% Loop section
while err > tol && ii < maxiter
lastestimate = estimate;
estimate = estimate + 4*(-1)^ii / (2*ii + 1);
10 err = abs((estimate-lastestimate)/estimate);
ii = ii + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 49 / 49
177. while Loops
Another Example—estimating π
π can be evaluated numerically using the Gregory-Leibniz series:
π = 4
∞
i=0
(−1)i 1
2i + 1 >> [ pi_est n_iter ] = pi_estimate(12,.1)
pi_est =
3.2837
n_iter =
5 7
Command line:
2.667
estimate
1
ii
1
err
0.1
tol
4
lastestimate
12
maxiter
function [ estimate ii ]= pi_estimate(maxiter, tol)
% Initialization section
estimate = 0;
ii = 0;
5 err = tol + 1;
% Loop section
while err > tol && ii < maxiter
lastestimate = estimate;
estimate = estimate + 4*(-1)^ii / (2*ii + 1);
10 err = abs((estimate-lastestimate)/estimate);
ii = ii + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 49 / 49
178. while Loops
Another Example—estimating π
π can be evaluated numerically using the Gregory-Leibniz series:
π = 4
∞
i=0
(−1)i 1
2i + 1 >> [ pi_est n_iter ] = pi_estimate(12,.1)
pi_est =
3.2837
n_iter =
5 7
Command line:
2.667
estimate
1
ii
0.5
err
0.1
tol
4
lastestimate
12
maxiter
function [ estimate ii ]= pi_estimate(maxiter, tol)
% Initialization section
estimate = 0;
ii = 0;
5 err = tol + 1;
% Loop section
while err > tol && ii < maxiter
lastestimate = estimate;
estimate = estimate + 4*(-1)^ii / (2*ii + 1);
10 err = abs((estimate-lastestimate)/estimate);
ii = ii + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 49 / 49
179. while Loops
Another Example—estimating π
π can be evaluated numerically using the Gregory-Leibniz series:
π = 4
∞
i=0
(−1)i 1
2i + 1 >> [ pi_est n_iter ] = pi_estimate(12,.1)
pi_est =
3.2837
n_iter =
5 7
Command line:
2.667
estimate
1
ii
0.5
err
0.1
tol
4
lastestimate
12
maxiter
function [ estimate ii ]= pi_estimate(maxiter, tol)
% Initialization section
estimate = 0;
ii = 0;
5 err = tol + 1;
% Loop section
while err > tol && ii < maxiter
lastestimate = estimate;
estimate = estimate + 4*(-1)^ii / (2*ii + 1);
10 err = abs((estimate-lastestimate)/estimate);
ii = ii + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 49 / 49
180. while Loops
Another Example—estimating π
π can be evaluated numerically using the Gregory-Leibniz series:
π = 4
∞
i=0
(−1)i 1
2i + 1 >> [ pi_est n_iter ] = pi_estimate(12,.1)
pi_est =
3.2837
n_iter =
5 7
Command line:
2.667
estimate
2
ii
0.5
err
0.1
tol
4
lastestimate
12
maxiter
function [ estimate ii ]= pi_estimate(maxiter, tol)
% Initialization section
estimate = 0;
ii = 0;
5 err = tol + 1;
% Loop section
while err > tol && ii < maxiter
lastestimate = estimate;
estimate = estimate + 4*(-1)^ii / (2*ii + 1);
10 err = abs((estimate-lastestimate)/estimate);
ii = ii + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 49 / 49
181. while Loops
Another Example—estimating π
π can be evaluated numerically using the Gregory-Leibniz series:
π = 4
∞
i=0
(−1)i 1
2i + 1 >> [ pi_est n_iter ] = pi_estimate(12,.1)
pi_est =
3.2837
n_iter =
5 7
Command line:
2.667
estimate
2
ii
0.5
err
0.1
tol
4
lastestimate
12
maxiter
function [ estimate ii ]= pi_estimate(maxiter, tol)
% Initialization section
estimate = 0;
ii = 0;
5 err = tol + 1;
% Loop section
while err > tol && ii < maxiter
lastestimate = estimate;
estimate = estimate + 4*(-1)^ii / (2*ii + 1);
10 err = abs((estimate-lastestimate)/estimate);
ii = ii + 1;
end
Function-file:
Keep repeating until while condition is false
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 49 / 49
182. while Loops
Another Example—estimating π
π can be evaluated numerically using the Gregory-Leibniz series:
π = 4
∞
i=0
(−1)i 1
2i + 1 >> [ pi_est n_iter ] = pi_estimate(12,.1)
pi_est =
3.2837
n_iter =
5 7
Command line:
3.2837
estimate
7
ii
0.0937
err
0.1
tol
2.976
lastestimate
12
maxiter
function [ estimate ii ]= pi_estimate(maxiter, tol)
% Initialization section
estimate = 0;
ii = 0;
5 err = tol + 1;
% Loop section
while err > tol && ii < maxiter
lastestimate = estimate;
estimate = estimate + 4*(-1)^ii / (2*ii + 1);
10 err = abs((estimate-lastestimate)/estimate);
ii = ii + 1;
end
Function-file:
Che 310 — Chapra 2–3 2 — Introduction August 24, 2017 49 / 49