Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
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

namespace  numerical_methods
 for assert
 
namespace  false_position
 Functions for [False Position] (https://en.wikipedia.org/wiki/Regula_falsi) method.
 

Functions

static float numerical_methods::false_position::eq (float x)
 This function gives the value of f(x) for given x.
 
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).
 
void numerical_methods::false_position::printRoot (float root, const int16_t &count)
 This function prints roots of the equation.
 
int main ()
 Main function.
 

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
xvalue for which we have to find value of f(x).
Returns
value of f(x) for given x.
44 {
45 return (x * x - x); // original equation
46}
Here is the call graph for this function:

◆ main()

int main ( void )

Main function.

Returns
0 on exit
102 {
103 float a = 0, b = 0, i = 0, root = 0;
104 int16_t count = 0;
105 float range =
106 100000; // Range in which we have to find the root. (-range,range)
107 float gap = 0.5; // interval gap. lesser the gap more the accuracy
109 i = ((-1) * range + gap);
110 // while loop for selecting proper interval in provided range and with
111 // provided interval gap.
112 while (i <= range) {
114 if (b == 0) {
115 count++;
117 }
118 if (a * b < 0) {
120 a, b);
121 count++;
123 }
124 a = b;
125 i += gap;
126 }
127 return 0;
128}
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
static float eq(float x)
This function gives the value of f(x) for given x.
Definition false_position.cpp:44
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
rootwhich we have to print.
countwhich is count of the root in an interval [-range,range].
84 {
85 if (count == 1) {
86 std::cout << "Your 1st root is : " << root << std::endl;
87 } else if (count == 2) {
88 std::cout << "Your 2nd root is : " << root << std::endl;
89 } else if (count == 3) {
90 std::cout << "Your 3rd root is : " << root << std::endl;
91 } else {
92 std::cout << "Your " << count << "th root is : " << root << std::endl;
93 }
94}
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,x2values for an interval in which root is present.
y1,y2values of function at x1, x2 espectively.
Returns
root of the equation in the given interval.
55 {
56 float diff = x1 - x2;
57 if (diff < 0) {
58 diff = (-1) * diff;
59 }
60 if (diff < 0.00001) {
61 if (y1 < 0) {
62 y1 = -y1;
63 }
64 if (y2 < 0) {
65 y2 = -y2;
66 }
67 if (y1 < y2) {
68 return x1;
69 } else {
70 return x2;
71 }
72 }
73 float x3 = 0, y3 = 0;
74 x3 = x1 - (x1 - x2) * (y1) / (y1 - y2);
75 y3 = eq(x3);
76 return regula_falsi(x2, x3, y2, y3);
77}
static double eq(double i)
Definition bisection_method.cpp:26
Here is the call graph for this function: