HW2 - 3101 Matlab -- (INSERT NAME and UNI HERE)

Due 11:59pm on Tuesday, March 25th, 2008

Contents

Goals

Start using matlab as a simple scripting language

Directions

Please fill in your name at the top of this page and write your answers under their corresponding question. When you are done, publish this file to html using File > Publish To HTML (or the command publish('hw1.m', 'html') and create a new zip file that is labeled with your UNI and homework number (eg. bs2018-hw1.zip), that contains this original m file, and the html directory created from publishing the file. Email this file to cs3101@gmail.com, making sure to include your name, and uni in the subject.

Note

When I say display a variable I mean display it so the output is labeled with the variable name, don't use disp, just leave off the semicolon from the end of the command.

Help

If you get stuck, type the command doc to pull up the documentation window, from there it's easy to search for any other commands or functions you might want to use...

Problem 1 - Fibonacci numbers and the golden ratio

(http://en.wikipedia.org/wiki/Fibonacci_number) Use a for loop to compute the first 100 Fibonacci numbers, and store them in a variable called f. only display the first 10 elements, make sure not to display anything while you are looping (use a semicolon)

1.2

plot the first 20 Fibonacci numbers, and title your plot.

1.3

(http://en.wikipedia.org/wiki/Golden_ratio) The golden ratio (also known as phi) is an irrational number that can be found throughout science, art and architecture since the renaissance... we know that phi is equal to (1 + sqrt(5)) / 2, however it is also easily expressed as the ratio between two successive Fibonacci numbers (weird... i know...). use a while loop to find the first two fibonacci numbers whose ratio approximates phi with an error less then 0.001, and print out only those two successive Fibonacci numbers... note: the while loop should terminate after it has found the first set of Fibonacci numbers whose ratio yields an error less then 0.001

1.5

display the exact phi, then display your approximation for phi calculated above, and then display the absolute value of their difference

1.5

change the display format to long and then display the exact phi, the approximate phi, and their difference

1.6

change the display format back to short (the default)

Problem 2 - Exploring the julia set fractals

(http://en.wikipedia.org/wiki/Julia_set) Julia set fractals are very interesting, and beautiful; they are also not too hard to create in matlab. first we start with a grid of equally spaced complex numbers, where the real components vary from -2 to 2, and the imaginary components vary from -2i to 2i. then for each number in this grid, we recursively apply a mathematical function to it many times and see how many times before the complex number becomes bigger then some cutoff value... that count of how many iterations it takes to blow up determines the color of that one pixel of the fractal

2.1

first lets start with a matrix of all zeros, so let's set N equal to 10, and make a new matrix F which is NxN and contains all zeros

2.2

create a variable called x which contains N numbers that are equally spaced between -2 and 2, display x

2.2.1

how many elements are in x?

2.3

create a variable called z0 which is a matrix of size NxN, such that element i,j of z0 should equal x(i) + sqrt(-1)*x(j). When we are done filling z0, it should contain equally spaced complex numbers such that the real component goes from -2 to 2 horizontally, and imaginary component goes from -2i to 2i vertically. Display z0. (Extra credit, if you can make z0 using only 1 line, no loops or semicolons)

2.4

for each element i,j in F, get the initial value of z from the matrix z0 and then use a while loop and a counter variable to count how many iterations of applying the function z = z^2 + c causes the absolute value of z to become bigger then the variable blowup. After you are done, F(i,j) should equal the number of times you had to apply z = z^2 + c repeatedly, before abs(z) gets bigger then the blowup parameter. Note: make sure to put a limit on the number of times the inner while loop can iterate if abs(z) doesn't blowup.

c=0.285+0.01*sqrt(-1)
blowup = 200;
maxiter = 200;
c =

   0.2850 + 0.0100i

2.5

display F, and then plot it using imagesc. make sure the axes are set to image

2.6

Collect all the code from this problem, and put it together as 1 chunk of code below and set N to be much bigger, like between 100 and 500

2.7

Make your own fractal! you can change the value for c, or pick a different function instead of z = z^2 + c, also try picking a colormap that is different then the default one... pick a setup that you find interesting and calculate it and plot it below