Lab 9 – Iterative Estimation
Function and aesthetics are often governed by mathematics. Beautiful examples of this can be found in nature, from the growth pattern of plants to the feature placement on the human face. Read a low-tech blog post (Links to an external site.) about how Aston Martin uses the golden ratio in car design. The Golden Ratio, phi, is defined by:
Equation 1. Exact Phi
One way that phi can be approximated is by taking the limit as n goes to infinity of a term in the Fibonacci sequence divided by it’s proceeding term:
Equation 2. Estimated Phi
Be sure to read the module page on Printing to Log Files because it contains code on how to compute terms of the Fibonacci sequence. Similarly, e can be approximated by:
Equation 3. Estimated E
We will use our new input and output tools to create an iterative estimation program for both e and phi and compare our estimates to the true values.
As usual, create a script, include a comment at the top explaining the purpose of the script, your name, and the date it was created.
1. Use the menu function to ask the user if they would like to estimate e or phi. Use the input function to prompt the user to select a desired accuracy (tolerance) between .1 to .01. Include error checking to ensure the user input corresponds to your desired format. See the page on Input Error Checking for an example of how to do this. Use a conditional or switch statement to select between the two estimation functions that you will write (see below).
2. To write your function to estimate e, create a function that accepts as input the user’s desired tolerance and outputs the estimate of e and the number of iterations required to meet the desired accuracy. Your function header will look like: function [myE, numIters] = estE(tol). Use the limit equation above for large n and compare the result to MATLAB’s built-in value for e. While the absolute value of the difference between e and your estimated e is greater than the tolerance, continue looping and increasing n.
3. Use a similar method to estimate phi. Since MATLAB has no built-in constant for phi, create your own true phi using Equation 1 above.
4. Create a text file to keep track of your progress at each iteration with the fprintf command (you may have to print every 100 iterations or even 1000 iterations if it takes a long time to reach the desired accuracy). Report the iteration, the estimated value and the absolute value of the difference between the estimated and true values at each step. Here’s a sample output:
After 133 iterations, estimated e is 2.708133 and error is 0.010149.
After 134 iterations, estimated e is 2.708208 and error is 0.010074.
After 135 iterations, estimated e is 2.708282 and error is 0.010000.
You will need to submit .m files for each function you define, main.m, estE.m and estPhi.m as well as your text log file. Each .m file should include your name and function description in comments. Upload your files via Canvas. Let me know if you have any questions!