Lecture 5.pdf

3
9/7/2015 Lecture 5 http://ocw.mit.edu/ans7870/resources/farjoun/Lecture5.html 1/3 Lecture 5 Warm up exercise Write a for loop that calculates the first 20 Fibonacci numbers. Place result in a vector Fibo, so that Fibo(n) is the nth number. Recall that the sequence satisfies: F(1)=1 F(2)=1 for n>2 F(n)=F(n1)+F(n2) while Statements Sometime, you need to iterate many times but don't really want to create a vector, or you don't know in advance how many time you need to iterate...for this you can use a while statement. The syntax for a while statement is while expression a bunch of statements end At the beginning of each iteration, Matlab evaluates the expression at the top of the loop. if it is true, Matlab executes the statements to the end. If it is false, Matlab jumps to the end and continues after it. So, for example: >> x=10; >> while x>0 x=x1 end >> will print out all the numbers from 9 to 0 (including both) Take note of two things: Matlab does not check the expression all the time. Only before starting a new iteration If the expression at the top is false when the first iteration should start, it will not. Exercises

Transcript of Lecture 5.pdf

Page 1: Lecture 5.pdf

9/7/2015 Lecture 5

http://ocw.mit.edu/ans7870/resources/farjoun/Lecture5.html 1/3

Lecture 5Warm up exercise

Write a for loop that calculates the first 20 Fibonacci numbers. Place result in a vectorFibo, so that Fibo(n) is the nth number. Recall that the sequence satisfies:

F(1)=1F(2)=1for n>2F(n)=F(n­1)+F(n­2)

while Statements

Sometime, you need to iterate many times but don't really want to create a vector, or you don'tknow in advance how many time you need to iterate...for this you can use a while statement.

The syntax for a while statement is

while expression a bunch of statementsend

At the beginning of each iteration, Matlab evaluates the expression at the top of the loop. if itis true, Matlab executes the statements to the end. If it is false, Matlab jumps to the end and continues after it.

So, for example: >> x=10;>> while x>0x=x­1end>> will print out all the numbers from 9 to 0 (including both) Take note of two things:

Matlab does not check the expression all the time. Only before starting a new iterationIf the expression at the top is false when the first iteration should start, it will not.

Exercises

Page 2: Lecture 5.pdf

9/7/2015 Lecture 5

http://ocw.mit.edu/ans7870/resources/farjoun/Lecture5.html 2/3

Find the value of the 100th Fibonacci number, using a while loop, and no big vectors.Write your prime checking program using a while loop instead of a for loop.Write a while loop that modifies n. if n is even it changes n to n/2, otherwise, itchanges it to 3n+1. if n is 1 the loop stops. Try this out for several different startingvalues. Count how many steps you need to get to 1.

Defining your own function

While Matlab has a vast collection of functions, sometimes you want to add some of yourown. A function should be thought of as a black box, with a slot in the top (for the input) and adrawer at the bottom for the output. When you give the function input ("call the function"), itcalculates the output and puts it in the drawer ("returns it"). Once the function is written, youdo not need to care about how it works. Functions must be defined in their own file, and the name of the file must match the name ofthe function. So, for example, to define a function called example_function_1, create a filenamed example_function_1.m. That file must start with the keyword function, so function y = example_function_1(x) is the first line of the file. let's dissect this first line:

function As I said, the first word must be "function".

y = This declares the local name of a variable whose value willbe returned when the execution of the function is over.

example_function_1 The name of the function. This must match the name of thefile.

(x) The local name of the variable that will contain the inputvalue.

So the rest of the file could be y=2*x;end admittedly, a very boring function: It takes the input value (locally called x), doubles it, andassigns that result to y. Since y is declared to be the return value and the function has ended, the return value is simply twice the input. we call the function by giving it a value: >> example_function_1(17) or >> example_function_1(rand)

Page 3: Lecture 5.pdf

9/7/2015 Lecture 5

http://ocw.mit.edu/ans7870/resources/farjoun/Lecture5.html 3/3

The return value can then be assigned to another variable: >> z= example_function_1(18);>> z

Variable Scope

An important difference between a function and a script (the files that don't have "function" astheir first word are called scripts) is that of "variable scope".When executing a script from the command­line, the scripts has access to all the variables thatare defined on the command line. For instance, if you have a script called script1.m whosecontents are

x+1y=y/2

and you run this script from the commandline like so:

>> x=2>> y = 6>> script1>> y

You will see that the script knows that x=2 and that y=6. Not only this, note that y is changedin the scripts, the new value of y, 3 is also the value of y when check on the commandline. This seems natural, perhaps, but this is not how functions behave. A function with thesame content function1.m

function function1x+1y=y/2

will fail to run, since within the function there are no such variables x and y. The variables ofthe function, are different from the variables outside the function, even if they have the same name.

change your prime checking program so that the input is the number, and the output is 1if the number is prime and 0 if not.change your n­changing program, so that the input is the initial value of n and the outputis the number of steps it took to reach 1.use your primechecking function in another program (can be a script) that finds (a) the1000th prime number and (b) the first prime number greater than 1000000.use your n­changing function in another program (can be a script) that finds the numberof iterations needed for the first 100 numbers, and plots then nicely.