Tuesday, January 26, 2010

Newton's method on your TI

Say you want to solve for the root of some equation that has no analytic solution, and you have your TI-8x handy. Should you take the 'shotgun' approach? No! You can easily implement Newton's method, making use of the fact that you have an 'ans' function to use the previous result as part of the next calculation.

Let's say we want to solve for the roots of f(x) = x^2 - 2. We know the roots are the plus and minus square roots of 2, but we need to start with an easy example. Newton's method, in short, asks you to guess a solution to f(x)=0, and calculate an improved guess using f(x) and its first derivative f'(x). The algorithm:
x_{n+1} = x_{n} - \frac{f(x_{n})}{f^{\prime}(x_{n+1})}

For our function, this means
x_{n+1} = x_n - \frac{x_n^2-2}{2x_n}

So, you first guess at the root, and that is your starting x. The next guess for x is calculated from the formula above. Let's say we guess 1 for the root. It is a bad guess, but it doesn't matter too much.

x_0=1 \qquad \Longrightarrow \qquad x_1 = 1 - \frac{1^2-2}{2\cdot 1}=1.5

Our next-best guess is then 1.5. We plug 1.5 into the equation above, and the next-next-best guess is 1.417. Not bad! After 3-4 iterations, you'll see the numbers start to converge.

How to do this on the TI? First, type your guess and hit [enter]. That makes the current value of "ans" equal to the number you just typed. Now type this as your formula:
(ans) - ((ans)-2)/(2*(ans))
Don't forget the parentheses. Hit [enter], and you've found your second guess, which is now the new value of "ans." Keep hitting [enter] until the result starts to converge, and that's that! You've implemented an iterative root-finding algorithm.

Doing this for a more complicated function, like the one on the homework, is not much harder. You just need to know the function and its derivative, otherwise you follow the same procedure.

No comments:

Post a Comment