Introductory Programming
Fall 2006

Homework 1

Some of these problems are adapted from Faires and Burden, Numerical Methods.

MATLAB expressions

The sequence ${F_n}$ described by $F_0 = 1$, $F_1 = 1$, and for $n \ge
0$, $F_{n+2} = F_n + F_{n+1}$, is called a Fibonacci sequence. Its terms occur naturally in many botanical species, particularly those with petals or scales arranged in the form of a logarithmic spiral (see http://maven.smith.edu/~phyllo/) .

The Fibonacci sequence can be approximated by the following expression:


\begin{displaymath}
F_n \approx \frac{1}{\sqrt{5}}
\left[
\left( \frac{1 + \sqr...
...t)^{n+1} -
\left( \frac{1 - \sqrt{5}}{2} \right)^{n+1}
\right]
\end{displaymath} (1)

  1. Use MATLAB to evaluate this expression for $n=10$ and $n=100$.

  2. Write a script called fibonacci1.m that evaluates this expression for whatever the current value of n is. You should be able to invoke your script like this:

    >> n = 10
    >> fibonacci1
            89
    

MATLAB math functions

  1. To see the list of MATLAB's elementary math functions, type help elfun. To read the documentation of abs, type help abs. Then try to evaluate the following expressions:

    abs(pi)
    abs (pi)
    abs ( pi )
    a b s ( p i )
    abs[pi]
    abs{pi}
    abs(pi
    abs()
    abs(1, 2)
    abs pi
    abs('pi')
    abs(1 + i)
    exp(i*pi)
    

  2. Use MATLAB to check if the following equations hold. You can check the equations two ways: first, compute the value of both sides and see if they are the same (use the format long command to see all the digits); alternatively, compute the difference between the right and left sides and see if the difference is zero. You should check 1 or 2 values of $x$ for each equation.

    1. $\tan x = \sin x / \cos x$

    2. $\sin^2 x + \cos^2 x = 1$

    3. $\sinh \frac{x}{2} = \pm \sqrt{ \frac{1}{2}(\cosh x - 1) }$

    4. $\log_{10} x = \frac{log_e x}{log_e 10}$

    Can you find values of $x$ for which these equations are not exactly true in floating-point arithmetic?