lecture 6(matlab)

18
Lecture 6: Iterative programming in MATLAB February 14

Upload: vipin-nair

Post on 21-Oct-2015

12 views

Category:

Documents


0 download

DESCRIPTION

matlab tutorial

TRANSCRIPT

Page 1: Lecture 6(matlab)

Lecture 6: Iterative programming in MATLAB

February 14

Page 2: Lecture 6(matlab)

Announcements

Announcements

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

HW 2 postponed to Feb. 25 :

Page 3: Lecture 6(matlab)

Announcements

Announcements

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

HW 2 postponed to Feb. 25 :

Page 4: Lecture 6(matlab)

Iterations, for block

Iterations,loops

We want to learn how to perform hundreds and thousands ofoperations. The way to do it is to repeat a similar command manytimes. 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.

Page 5: Lecture 6(matlab)

More on for

Caution

It’s not as simple as it seems.

In the homework, you will need to perform the sameoperation many times.

Usually, you will need to perform different operations andadjust the result between the iterations of the loop. (andbefore you start the loop).

Page 6: Lecture 6(matlab)

Example - sum of squares

Example

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

s =

n∑k=1

k2,

using the for loop.

This is just for the sake of exercise since sum( (1:n).^ 2)

achieves the same result.

Page 7: Lecture 6(matlab)

Example - sum of squares

Example

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

s =

n∑k=1

k2,

using the for loop.

This is just for the sake of exercise since sum( (1:n).^ 2)

achieves the same result.

Page 8: Lecture 6(matlab)

Designing our program

Design dilemmas

for block should be thought as implementing an inductiveprocedure (i.e. something that mathematical induction workson) , you achieve a partial result in i− 1 steps and do the i-thstep to get to the next partial result.

In this case denote si =∑i

k=1 k2 then si+1 = si + i2.

A correct way to initialize it is s0 = 0.

Page 9: Lecture 6(matlab)

Designing our program

Design dilemmas

for block should be thought as implementing an inductiveprocedure (i.e. something that mathematical induction workson) , you achieve a partial result in i− 1 steps and do the i-thstep to get to the next partial result.

In this case denote si =∑i

k=1 k2 then si+1 = si + i2.

A correct way to initialize it is s0 = 0.

Page 10: Lecture 6(matlab)

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

Page 11: Lecture 6(matlab)

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

Page 12: Lecture 6(matlab)

’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 thevariables for the next iteration.

For the while loop, make sure that the termination conditiongets updated or you won’t exit the loop.

Page 13: Lecture 6(matlab)

’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 thevariables for the next iteration.

For the while loop, make sure that the termination conditiongets updated or you won’t exit the loop.

Page 14: Lecture 6(matlab)

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

Page 15: Lecture 6(matlab)

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 theHW).

The last open (unended) block gets terminated first.

A good practice is to indent the code in the block to theright. That way, you it is easier to know in which block youare exactly.

MATLAB editor helps you do that both interactively andusing ’smart indent’ in the right-click menu.

Observe the indentation rules for the homework!

Page 16: Lecture 6(matlab)

Exercises

Exercise 1

Rewrite your Newton function to accept as inputs the initial guessand the number of iterations it should perform (and output theresult).

Exercise 2

Rewrite your Newton function to run until |x2n − 5| ≤ 10−10.Additionaly, try to output the number of iterations it took.

More on the next slide

Page 17: Lecture 6(matlab)

Exercises (continued)

Exercise 3

Design a program that computes n!

Exercise 4

Design a program that computes∑n

k=1 1/k2.

Exercise 5

The Fibonacci sequence Fn = Fn−1 + Fn−2 for n ≥ 2, whereF0 = 0, F1 = 1, has the explicit formula

Fn =1√5

((1 +√5

2

)n

(1−√5

2

)n).

What could go wrong with programming this in directly?

More on the next slide

Page 18: Lecture 6(matlab)

Exercises (continued)

Exercise 5 (cont.)

Write a function to compute Fibonacci numbers iteratively. Theinput should be n and the output Fn.

Exercise 6

Write a function to compute Fibonacci numbers using the explicitformula and compare the results for large n.