# Lecture 6: Iterative programming in MATLAB

February 14

Announcements

Announcements HW 1 is graded. Solution will be posted on the web site soon.

Announcements

Announcements HW 1 is graded. Solution will be posted on the web site soon. HW 2 postponed to Feb. 25 :

Iterations, for block

Iterations,loops We want to learn how to perform hundreds and thousands of operations. The way to do it is to repeat a similar command many times. Such a construction is called a ’loop’. for Here is the syntax to use for loop. for i=1:4 Comands... end Commands... will be executed for i=1,2,3,4.

More on for

Caution It’s not as simple as it seems. In the homework, you will need to perform the same operation many times. Usually, you will need to perform diﬀerent operations and adjust the result between the iterations of the loop. (and before you start the loop).

Example - sum of squares

Example Write a function function s=sumsquares(n) which will have an input of integer number n and computes
n

s=
k=1

k2 ,

using the for loop.

Example - sum of squares

Example Write a function function s=sumsquares(n) which will have an input of integer number n and computes
n

s=
k=1

k2 ,

using the for loop. This is just for the sake of exercise since sum( (1:n).^ 2) achieves the same result.

Designing our program

Design dilemmas for block should be thought as implementing an inductive procedure (i.e. something that mathematical induction works on) , you achieve a partial result in i − 1 steps and do the i-th step to get to the next partial result.

Designing our program

Design dilemmas for block should be thought as implementing an inductive procedure (i.e. something that mathematical induction works on) , you achieve a partial result in i − 1 steps and do the i-th step to get to the next partial result. In this case denote si =
i 2 k=1 k

then si+1 = si + i2 .

A correct way to initialize it is s0 = 0.

Sum squares matlab code

sumsquares.m function s=sumsquare(n) % sumsquare sums the squares of integer numbers % up to n partialsum=0; for i=1:n partialsum=partialsum+i^2; end s=partialsum; end

Newton with for loop

Newton with for loop function x=fnNewt3(x0) xold=x0; for i=1:4 xnew=xold/2+5/(2*xold); xold=xnew; end x=xnew; end

’while’ block

while syntax while condition commands end will repeat commands while the logical condition holds.

’while’ block

while syntax while condition commands end will repeat commands while the logical condition holds. while what should really happen Think how to initialize the variables before you enter the loop. Before the end of the iteration, think how to advance all the variables for the next iteration. For the while loop, make sure that the termination condition gets updated or you won’t exit the loop.

Sum squares with while

sumsquareswhile.m function s=sumsquarewhile(n) % sumsquare sums the squares of integer numbers % up to n partialsum=0; i=0; while (i<=n) partialsum=partialsum+i^2; i=i+1; end s=partialsum; end

Blocks and indentation

Blocks (of code) in Matlab if, for, while start a block of code in Matlab. Each block has to be terminated with end. You can nest blocks within a block (you will have to for the HW). The last open (unended) block gets terminated ﬁrst. A good practice is to indent the code in the block to the right. That way, you it is easier to know in which block you are exactly. MATLAB editor helps you do that both interactively and using ’smart indent’ in the right-click menu. Observe the indentation rules for the homework!

Exercises

Exercise 1 Rewrite your Newton function to accept as inputs the initial guess and the number of iterations it should perform (and output the result). Exercise 2
−10 . Rewrite your Newton function to run until |x2 n − 5| ≤ 10 Additionaly, try to output the number of iterations it took.

More on the next slide

Exercises (continued)
Exercise 3 Design a program that computes n! Exercise 4 Design a program that computes
n 2 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 1 Fn = √ 5 √ 1+ 5 2
n

√ 1− 5 2

n

.

What could go wrong with programming this in directly? More on the next slide

Exercises (continued)

Exercise 5 (cont.) Write a function to compute Fibonacci numbers iteratively. The input should be n and the output Fn . Exercise 6 Write a function to compute Fibonacci numbers using the explicit formula and compare the results for large n.