Finding root by False position method in Mathematica

False position method is a method of finding root. For numerical analysis, here we should take two such points near root as the root stays between them. Here we will find a root of an equation by mathematica application.


Like bisection method the false position method starts with two points a and b such that f(a) and f(b) are opposite signs.


May be you will understand from the following photo.
From wikipedia



Now we write the code for mathematica.


Clear["`*"];
f[x_] := x^3 + 3x + 1;
Plot[f[x],{x,-5,5}];
a = Input["Enter a"];
b = Input["Enter b"];
tol = Input["Enter tolerance"];
n = Input["Enter total iteration"];
x0 = a;
x1 = b;
If[f[x0]*f[x1]>0,
{
Print["There is no root in this interval"], Exit[]}];
Print["     n         x0           x1            x2            f(x2)"];
Do[
x2 = x0 - (f[x0]/(f[x1]-f[x0]))*(x1-x0) //N;
Print[PaddedForm[i,5],
PaddedForm[x0,{12,6}],
PaddedForm[x1,{12,6}],
PaddedForm[x2,{12,6}],
PaddedForm[f[x2],{12,6}]];
If[Abs[x2-x1]<tol  ||  Abs[x2-x0]<tol,
{
Print["The root is: ", x2], Exit[]}];
If[f[x0]*f[x2]>0, x0=x2, x1=x2],
{i,1,n}];
Print["Maximum iteration failed"];


Now hit shift + enter.

This graph will be shown;

Easily we see that, here is a root between -2 and 0. Hence we will input
a = -2
b = 0
tol = .001
n  = 25


The solve has been done.

Related Posts
Previous
« Prev Post