Thursday, 28 May 2015

Approximate Solution of equations by iteration: Wolfram Language Implementation of the Newton's Method




Dedicated to the memory of a Great Mathematician, a beautiful mind, John Forbes Nash.

Newton's Method


The newton method also known as the Newton-Raphson method is another important iterative method for solving equations f(x), here f is assumed to have a continuous derivative f'.

The derivation of this simple method (with great speed) can be looked up from textbooks as we are only concerned with the implementation of the method using the Wolfram Language.

newton-graph

Algorithm


  1. Inputs: f,x0,ε,N.
  2. For n=1,2N-1, compute xn+1=xn-f(xn)f'(xn)
  3. Stopping Criteria: If |f(xn+1)|>ε, stop computation and Print xn+1.
  4. Print xn+1.

Wolfram Language Implementation

Code 1 (newton)
newton code

This implementation defines a function "newton[fun, {var_Symbol, init_?NumericQ}, n_Integer]", where fun is the function f(x) we want to find its approximate solution, var is the variable in the function f(x), init is the approximation x0 and n the number of iterations we want, the program terminates after n iteration(s), then outputs the approximate solution xn+1.
Code 2 (newton1)
newton1-code

This second implementation, slightly different, defines a function "newton1[fun, {var_Symbol, init_?NumericQ}, ε_0.0001]" with ε, the degree of accuracy with default value 0.0001. The program terminates at any point where the absolute value of |f(xn+1)|>ε and returns xn+1.

If f(x)=x3+x-1, we can find the approximate solution of f(x) using an initial approximation x0=1.

Using the first implementation, we have

newton-result

And for the second implementation,

newton1-result

Having any difficulty understanding the implementations? You can definitely drop a comment at the comment section. I will be glad to be at your service.

Wolfram Cloud has won me over, i want everyone to have a feel of what these implementation does. Despite not having Mathematica installed on your PC, you can test the implementation. This time, I'm going to embed the cloud deployment here, you don't have to leave this place. Its very easy. Follow me.


Wolfram Cloud Depoyment


Below is the embedded cloud deployment, very easy to use. You only have to enter the appropriate values in the text boxes and click the Submit button, anything else is automatic.



Still need a link to the cloud deployment? See it here. Will be back with Secant. Drop a comment.

References

Jain, M., Iyengar, S., & Jain, R. (2010). Numerical Method for Scientific and Engineeing Computiation (5th ed.). New Delhi: New Age International.
Kreyszig, E. (2010). Advanced Engineering Mathematics (9th ed.). USA: John Wiley and Sons Inc. Wellin, P. (2013). Programming with Mathematica (1st ed.). New York: Cambridge University Press.
Weisstein, Eric W. "Newton's Method." From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/NewtonsMethod.html

No comments:

Post a Comment