Friday, 24 April 2015

Approximate Solution of equations by iteration: Wolfram Language Implementation of the Bisection Method




I am going to implement four iteration methods for finding the approximate solutions of equations of the form f(x)=0 using the Wolfram Language, and also deploy these implementations to the cloud for the sake of those who do not have Wolfram Mathematica locally installed in their computers. The cloud deployment allows the user to test these implementations with just their web browsers.
This is the first series of this root finding article, it is based on the bisection method, while the other series will be based on the Newton method, Secant method, and the method of false position (Regula – Falsi)

Bisection Method

The bisection method is based on the repeated application of the intermediate value theorem


If the zero of a function f(x)=0 lies in an interval I=(a,b), we bisect I at the point m=a+b2. If f(a)f(m)<0 replace the interval I with (a,m),that is I=(a,m) else f(m)f(b)<0, then I=(m,b).Thus, the new interval I also contains the zero of f(x). Continuing this bisection process until |f(m)|<ε, where ε is the degree of accuracy, we take the last midpoint as the desired zero (approximate solution) of the function.

Algorithm


  1. Inputs: Initial interval I=(a,b), ε.
  2. If f(a)f(b)>0, "Print Approximate solution may not exist in that interval". Goto 7. Else.
  3. Compute m=a+b2 and f(m)=0
  4. While |f(m)|<ε, If f(a)f(m)<0, b=m, i.e I=(a,m), else a=m, i.e I=(m,b).
  5. Goto 3. (* The loop continues until |f(m)|<ε *).
  6. Print m
  7. Stop

Wolfram Language Implementation



The code above is a procedural implementation of our bisection algorithm. In the code, we defined a function "bisect[fun,{var_Symbol,a_?NumericQ,b_?NumericQ},ε_]", where fun is the equation f(x) we want to find its approximate root, var is the variable in the function f(x), a and b are the initial values of the interval where the zero of f(x) lies and ε is the degree of accuracy that we want, with default value 0.0001.

Given that f(x)=ex+x4+x-2, we can say that the zero of f(x) lies the interval (0,1) since f(0)f(1)<0.

local1


Thus, we proceed to finding the approximate solution to f(x) using our implementation.


local2

Wolfram Cloud Deployment


Clicking this link, you will be presented with a web form as pictured


For expr , enter the function f(x) you want to find its approximate root, var is the variable in the function f(x), a and b where the initial values of the interval where the zero of f(x) lies and ε is the degree of accuracy that you want, with default value 0.0001. Then Submit.


After the form submission, comes the result


I really hope you find this interesting and important. Up next is the famous Newton method.



References

Jain, M., Iyengar, S., & Jain, R. (2010). Numerical Method for Scientific and Engineering Computation (5th ed.). New Delhi: New Age International.
Kreyszig, E. (2010). Advanced Engineering Mathematics (9th ed.). USA: John Wiley and Sons Inc.
Welin, P. (2013). Programming with Mathematica (1st ed.). New York: Cambridge University Press.

No comments:

Post a Comment