Lecture 2: Programming Basics in MATLAB: September 7
Lecture 2: Programming Basics in MATLAB: September 7
Lecture 2: Programming Basics in MATLAB: September 7
September 7
Announcements
Announcements
HW 1 Due next Tuesday in class:
Schedule:
lectures on 9/9 and 9/14 (on 9/14 Simon will replace me).
no lectures on 9/16, 9/21.
back to the usual schedule on 9/23.
Office Hours:
Tomorrow as usual 787 Evans.
On 9/15 and 9/22, Simon will receive you in 1046 Evans.
Useful tips
f (xn )
xn+1 = xn − .
f 0 (xn )
√
we will use that to find 5, i.e. the zero of f (x) = x2 − 5,
therefore, our procedure is
x2n − 5 xn 5
xn+1 = xn − = + .
2xn 2 2xn
In Matlab code
A script
Let’s open the editor window.
Input
Let’s modify the the first line to function x4=NewtFn2(x0)
and delete x0=2 line.
A good practice is to write end in the last line (more on that
coming)% Save the work, obviously.
To invoke now we write Y=NewtFn2(2).
Now we have a function that accepts the initial guess as an
input.
Functions
for
We obviously repeat the same operation. Let’s use the four
loop in order not to type everything again.
Here is the syntax to use ’For’ loop.
for i=1:4
Comands...
end
for starts a new block in our program, which has to be
terminated with end. Lot’s of times we have to nest these
blocks, which is perfectly reasonable. In fact we already
nested the for block in the main block of the function.
Newton with ’for’ loop
Debugging
Bugs are programming mistakes in the code. Debugging refers
to correcting programming mistakes.
If you wish to debug a small program, sometimes the easiest
way is just output all the variables.
Using debugger
You can use the buttons in the Editor window.
You also have the following commands:
dbstep to execute one more command in the code.
dbcont to continue to the next break point.
dbquit to stop the execution and quit debuggin.
More flow control
Flaw in fnNewt
There is a flaw in ’fnNewt3.m’.
We can correct it using the ’if’ statement.
First we need to understand how Matlab works with logical
statements and statements.
Boolean expressions
We form boolean expressions from comparison operators
<, >, ==,˜=, <=, >=. There is also a plethora of
is-functions, like isreal.
Such expression will return 0 for if false and 1 if true.
We use the logical operators &&, || and ˜ to form more
complicated expressions.
% Don’t forget to have the parenthesis right.
if, else, elseif
if statement
if condition
expression
end
Executes statement if condition is true.
if..else statement
if condition
expression1
else
expression2
end
Executes statement1 if condition is true, executes statement2 if
condition is false.
fnNewt5.m
function x=fnNewt5(x0)
if x0==0
fprintf(’Error, input not allowed’)
xnew=nan;
else
xold=x0;
for i=1:4
xnew=xold/2+5/(2*xold);
xold=xnew;
end
end
x=xnew;
end
’while’ loop
while syntax
while condition
expression
end
will repeat expression while the condition holds.
’while’ loop
while syntax
while condition
expression
end
will repeat expression while the condition holds.
Exercise 1
Rewrite your Newton function to accept as an input the number of
iterations it should perform. What would be a good thing to check
and how would you check it?
Exercise 2
Rewrite your Newton function to run until x2n − 5 ≤ 10−10 .
Additionaly, try to output the number of iterations it took.
Exercise 4
Pn 2.
Design a program that computes k=1 1/k
Exercise 5
The Fibonacci sequence Fn = Fn−1 + Fn−2 for n ≥ 2, where
F0 = 0, F1 = 1, has the explicit formula
√ !n √ !n !
1 1+ 5 1− 5
Fn = √ − .
5 2 2
Exercise 6
Below is supposed to be a routine to compute the nth term of the
FIbonacci sequence. Use the debugger to find what is wrong with
this program.
————————————————————
function Fn=Fibonnaci(n) Fnminus2=0;
Fnminus1=1;
if (n == 0)||(n == 1)
Fn=n;
else
for i=2:n
Fnminus2=Fnminus1;
Fn=Fnminus1+Fnminus2;
Fnminus1=Fn;
end
end