Lab 5: Visualizing Newton's Method and Fractals
You won't be writing much code for this lab, but hopefully you'll derive from it a deeper understanding of how, exactly, the "universe" programs we've been creating relate to earlier programs and functions (and in particular recursion).
For starters, you should review your solutions for inlab 2 and the related lab to recall how programs for computing square roots using Newton's method and for drawing fractals worked. Go ahead. I'll wait.
The key to both Newton's method and fractal painting is that we continually apply a function (newton
or fractal
) over and over again to its own result. For instance, the result of applying newton
to an initial guess produces an improved guess, that can be used as input to newton
, which computes another improved guess, that can be used as input to newton
, which ... and you get the idea.
Our original programs were able to compute "answers" --- i.e., a guess that was "good enough" or a fractal some number of levels deep --- but we couldn't see what happened at each step all too easily (unless you used DrRacket's stepper). In this lab, we'll write universe programs that allow us to manually step through each re-application of newton
and fractal
.
Exercise 1: Visualizing Square Root Approximation
For this exercise you are to write a program that contains a global variable named *n*
which is bound to a number for which Newton's method will be used to compute a square root.
You will also write a function named "start
" that will take as input an initial guess, and will call big-bang
with that guess as the initial world state, and also provide two handlers:
- a
to-draw
handler, which will draw the current world state (which is the current guess) - an
on-key
handler, which will advance the world state by computing the next guess --- note that the actual key being pressed is unimportant.
I will demo this program at the start of lecture (and thereafter on demand).
Hint: to make it easier to display the numbers -- and more efficient to compute them -- you should convert all computed guesses to inexact numbers before returning them. The built-in Racket function "exact->inexact
" will do the conversion.
When done, try different values of *n*
and initial guesses ... does it make a big difference on how quickly the result converges?
Exercise 2: Visualizing Cubed Root Approximation
Repeat the previous exercise, but this time for visualizing cube roots. If you're smart about this, you should be able to re-use the same program, but change just the on-key
handler you give to big-bang
.
Can you see the difference in how quickly Newton's method converges on a cube-root result?
Exercise 3: Visualizing Fractals
For this exercise you will write a program, similar to the above two, that allows us to step through the creation of a fractal image --- the same type of fractal you created for inlab 2. This time, you will have a global variable named *seed-image*
that will be the image from which the fractal is constructed. Each time a key is pressed, the world state (which will be an image) will be updated so as to reflect the next "level" fractal.
Again, you will write a start
function that calls big-bang
using the to-draw
and on-key
clauses. This time, however, we will always call start
using *seed*
as the initial input.
I will demo this program at the start of lecture (and thereafter on demand).