Excercise sheet 8

Loops

1. What is the difference between the While and Do loops that were presented in the lecture notes?
2. Write the Mathematica commands to calculates the n first Fibonacci numbers, defined according to the relation
	(Subscript[x, i]) = (Subscript[x, i - 1]) + (Subscript[x, i - 2]),  
      i ≥ 2
(Subscript[x, 0]) = 0
(Subscript[x, 1]) = 1
ie, each Fibonacci number is the sum of two previous ones. Ask the number n from the user. To print out the numbers you can use the Print-command.
3. Calculate the sum (∑_(i = 0)^10) (i^2) using Do- and While loops.
4. The Hénon map.  Make a loop that iterates the famous Hénon map
	(Subscript[x, i + 1]) = 1 + (Subscript[y, i]) + a ((Subscript[x, i])^2)
(Subscript[y, i + 1]) = b (Subscript[x, i]), a = 1.4
b = 0.3.
Try if you can store the results in a list of points ((Subscript[x, i]), (Subscript[y, i])) and plot those! (You can use the Append command to add (x, y) pairs to a list)

Nest and FixedPoint

The Nest and FixedPoint commands are useful when one needs to iterate a function. Say you have a function f, and you need to apply it n times to a number (Subscript[x, 0]), that is, you want to compute
	\(TraditionalForm\`x\_n = \(\(\(f(f(⋯\ \(f(f(x\_0))\) ⋯))\)\+⏟\)\+\(n\ applications\ of\ f\) = \(\(\((f\ ◦\ f\ ◦\ ⋯\ ◦\ f)\)\+⏟\)\+n\) \((x\_0)\)\)\). 
This is called nesting and to evaluate these in Mathematica, you can use the Nest command:
	Nest[f, x0, n],
where f is your function (must be a pure function, not a Mathematica expression!), x0 the argument for the innermost f, and n is the number of iterations.

1. Implement the loops of previous excersise using the Nest command.
2. Find the square root of 10 using the Newton's method and the Nest-command. If you've an equation g(x) = 0, and a guess (Subscript[x, i]) for its solution, Newton's method gives you an improved guess (Subscript[x, i + 1]) for the solution: 
	(Subscript[x, i + 1]) = f((Subscript[x, i])) = 
          (Subscript[x, i]) - ((g((Subscript[x, i])))/(g' ((Subscript[x, i])))).
When f is applied repeatedly, you end up with better and better approximations for the solution (we're ignoring the important matter of convergence here, of course).
To find the square root of a, the function g(x) = (x^2) - a, and 
(1)	f(x) = x - (((x^2) - a)/(2  x)).
So, set a = 10, and apply the Nest command to f(x). Set the initial guess (Subscript[x, 0]) to your liking and try different values of n. Compare the results to Sqrt[10].

The command FixedPoint works the same way as Nest, but you don't give the number of iterations as an argument. Instead, Mathematica applies the function f so many times that the result does not change, i.e. it tries to find the limit
	x = (lim_(n -> ∞))(Subscript[x, n]), 
This x is called a fixed point, since it satisfies f(x) = x, hence the name of the command. Try the FixedPoint command with the function f from Eq. (1). Compare the result to (Sqrt[10]).

Rocket science

Make the rocket example two dimensional, ie. take into account that the rocket can move in two directions. With some simplifications we can take the equations of motion to be
	m (Subscript[a, x]) = (Subscript[F, thrust,  x]) + (Subscript[F, G,  x])
m (Subscript[a, y]) = (Subscript[F, thrust,  y]) + (Subscript[F, G,  y])
Assume again that the thrust (Subscript[F, thrust]) is constant. Let us take the direction of this force to be the same as the velocity of the rocket. In this case, the force vector reads
	\(TraditionalForm\`F\&_\_thrust = \(\((F\_\(thrust, \ x\), \ 
            F\_\(thrust, \ y\))\) = \(F\_thrust\)(
            v\_x\/\(\(|\)\(v\)\(|\)\), \ v\_y\/\(\(|\)\(v\)\(|\)\))\)\), 	
where
	|v| = (Sqrt[((Subscript[v, x])^2) + ((Subscript[v, y])^2)]),	({{\(v\_x = x\& . \)}, {\(v\_y = y\& . \)}}).
Earth's gravity is naturally directed towards the center of the planet, so gravity reads
	\(TraditionalForm\`F\&_\_grav = 
        m\ g\ \((R\/\(\(|\)\(r\)\( | \^2\)\))\) 
          r\&_\/\(\(|\)\(r\)\(|\)\)\),
where r = (x,  y),  
      and  | r |  = (Sqrt[(x^2) + (y^2)]). Note that we've taken the positions to be relative to the center of the mass, not to the surface of the planet.

Mandelbrot set

The Mandelbrot set is defined as the set of points c for which the sequence defined by
(1)	 (Subscript[z, 0]) = 0
(Subscript[z, i + 1]) = ((Subscript[z, i])^2) + c
does not approach infinity. Construct a function that counts the number of iterations of (1) that are required for the absolute value of z to become larger than 2. You also need to limit maximum number of iterations allowed. Then make a 3D plot of the function by taking the real and imaginary parts of c to be the x and y coordinates of the plot. The result should be a rather familiar looking object.