false_position.cpp File Reference

Solve the equation \(f(x)=0\) using false position method, also known as the Secant method. More...

`#include <cmath>`
`#include <iostream>`
Include dependency graph for false_position.cpp:

## Namespaces

| numerical_methods
| for io operations
| false_position
| Functions for False Position method.

## Functions

static float | numerical_methods::false_position::eq (float x)
| This function gives the value of f(x) for given x. More...
static float | numerical_methods::false_position::regula_falsi (float x1, float x2, float y1, float y2)
| This function finds root of the equation in given interval i.e. (x1,x2). More...
void | numerical_methods::false_position::printRoot (float root, const int16_t &count)
| This function prints roots of the equation. More...
int | main ()
| Main function. More...

## Detailed Description

Solve the equation \(f(x)=0\) using false position method, also known as the Secant method.

First, multiple intervals are selected with the interval gap provided. Separate recursive function called for every root. Roots are printed Separatelt.

For an interval [a,b] \(a\) and \(b\) such that \(f(a)<0\) and \(f(b)>0\), then the \((i+1)^\text{th}\) approximation is given by:

\[ x_{i+1} = \frac{a_i\cdot f(b_i) - b_i\cdot f(a_i)}{f(b_i) - f(a_i)} \]

For the next iteration, the interval is selected as: \([a,x]\) if \(x>0\) or \([x,b]\) if \(x<0\). The Process is continued till a close enough approximation is achieved.

See also
  newton_raphson_method.cpp, bisection_method.cpp

Author
  Unknown author
  Samruddha Patil

## Function Documentation

## eq()

static float numerical_methods::false_position::eq | ( | float | x | ) |

static

This function gives the value of f(x) for given x.

Parameters

  x | value for which we have to find value of f(x).

Returns
  value of f(x) for given x.

{
return (x * x - x); // original equation
}
Here is the call graph for this function:

## main()

int main | ( | void |  | ) |

Main function.

Returns
  0 on exit

{
float a = 0, b = 0, i = 0, root = 0;
int16_t count = 0;
float range =
100000; // Range in which we have to find the root. (-range,range)
float gap = 0.5; // interval gap. lesser the gap more the accuracy
a = numerical_methods::false_position::eq((-1) * range);
i = ((-1) * range + gap);
// while loop for selecting proper interval in provided range and with
// provided interval gap.
while (i <= range) {
b = numerical_methods::false_position::eq(i);
if (b == 0) {
count++;
numerical_methods::false_position::printRoot(i, count);
}
if (a * b < 0) {
root = numerical_methods::false_position::regula_falsi(i - gap, i,
a, b);
count++;
numerical_methods::false_position::printRoot(root, count);
}
a = b;
i += gap;
}
return 0;
}
numerical_methods::false_position::regula_falsi
static float regula_falsi(float x1, float x2, float y1, float y2)
This function finds root of the equation in given interval i.e. (x1,x2).
Definition: false_position.cpp:55
numerical_methods::false_position::eq
static float eq(float x)
This function gives the value of f(x) for given x.
Definition: false_position.cpp:44
numerical_methods::false_position::printRoot
void printRoot(float root, const int16_t &count)
This function prints roots of the equation.
Definition: false_position.cpp:84

## printRoot()

void numerical_methods::false_position::printRoot | ( | float | root,
|  | const int16_t & | count
| ) |  |

This function prints roots of the equation.

Parameters

  root | which we have to print.
  count | which is count of the root in an interval [-range,range].

{
if (count == 1) {
std::cout << "Your 1st root is : " << root << std::endl;
} else if (count == 2) {
std::cout << "Your 2nd root is : " << root << std::endl;
} else if (count == 3) {
std::cout << "Your 3rd root is : " << root << std::endl;
} else {
std::cout << "Your " << count << "th root is : " << root << std::endl;
}
}
std::cout
std::endl
T endl(T... args)
Here is the call graph for this function:

## regula_falsi()

static float numerical_methods::false_position::regula_falsi | ( | float | x1,
|  | float | x2,
|  | float | y1,
|  | float | y2
| ) |  |

static

This function finds root of the equation in given interval i.e. (x1,x2).

Parameters

  x1,x2 | values for an interval in which root is present.
  y1,y2 | values of function at x1, x2 espectively.

Returns
  root of the equation in the given interval.

{
float diff = x1 - x2;
if (diff < 0) {
diff = (-1) * diff;
}
if (diff < 0.00001) {
if (y1 < 0) {
y1 = -y1;
}
if (y2 < 0) {
y2 = -y2;
}
if (y1 < y2) {
return x1;
} else {
return x2;
}
}
float x3 = 0, y3 = 0;
x3 = x1 - (x1 - x2) * (y1) / (y1 - y2);
y3 = eq(x3);
return regula_falsi(x2, x3, y2, y3);
}
Here is the call graph for this function: