GEOL 5470
Homework/Labwork Humphrey Intro to iteration
A fundament problem in numerical methods is finding the solution to: f(x)=0 , which means finding the x that makes the function f() equal to zero. If f is simple, this is a simple algebra problem, but if f is even slightly involved, or if x is multidimensional, then finding x can range from being difficult to impossible. We are going to start thinking about this sort of problem by introducing a solution technique to this fundamental problem: iteration. The basic idea is that we guess a solution x. Put the guess into the equation and see how far from zero we are. Try another guess, if that improves the ‘fit’, then try another guess in the same ‘direction’. Hopefully we will converge on a guess that makes f(x) almost zero. The skill of course is in choosing the next ‘guesses’, and there are many sophisticated techniques. However the bottomline is that all the techniques iterate towards a solution.
1. So to illustrate we are going to find the ‘roots’ of the following transcendental equation: cos(x) – ln(x)/2 = 0, we need to find the x that makes this zero. There are actually 3 roots, which adds difficulty. The method we will use is very simple: make an initial guess for x, and then take a small ‘step’ in x to the right. If the small step reduces f(x) then take another small step. Stop when the sign of the error changes or when the error starts to get bigger. Call this x the solution. This method has a lot of problems, the 2 biggest are: 1) it will get trapped in local minima or maxima , 2) it can’t find multiple roots. Also the error in the answer is about the size of the ‘steps’. However, it will work on this simple function. Your job is to find the 3 roots of the equation.
2. As a modification, try to write a more sophisticated code that lets the step size get smaller, once you are near the root, so that you can choose the size of the error.
Note this problem is a good example of the value of plotting and visualization. Plotting the curves and the solutions will allow you check your own work. And the fact that you can end up with the ‘wrong’ root or answer, is a trivial example of my adage that ‘you should never model unless you already know the answer’. This labwork also introduces the last of the common ‘loop’ commands in python, the ‘while’ loop. The ‘while’ loop has a familiar syntax: while ‘some logical true or false question’: followed by indented code.