mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-04 02:59:43 +08:00
rename Computer Oriented Statistical Methods -> computer_oriented_statistical_methods (#656)
This commit is contained in:
53
computer_oriented_statistical_methods/Bisection_method.CPP
Normal file
53
computer_oriented_statistical_methods/Bisection_method.CPP
Normal file
@@ -0,0 +1,53 @@
|
||||
#include <iostream.h>
|
||||
#include <conio.h>
|
||||
#include <math.h>
|
||||
|
||||
float eq(float i)
|
||||
{
|
||||
return (pow(i, 3) - (4 * i) - 9); // original equation
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
float a, b, x, z;
|
||||
clrscr();
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
z = eq(i);
|
||||
if (z >= 0)
|
||||
{
|
||||
b = i;
|
||||
a = --i;
|
||||
goto START;
|
||||
}
|
||||
}
|
||||
|
||||
START:
|
||||
|
||||
cout << "\nFirst initial: " << a;
|
||||
cout << "\nSecond initial: " << b;
|
||||
for (i = 0; i < 100; i++)
|
||||
{
|
||||
x = (a + b) / 2;
|
||||
z = eq(x);
|
||||
cout << "\n\nz: " << z << "\t[" << a << " , " << b << " | Bisect: " << x << "]";
|
||||
|
||||
if (z < 0)
|
||||
{
|
||||
a = x;
|
||||
}
|
||||
else
|
||||
{
|
||||
b = x;
|
||||
}
|
||||
|
||||
if (z > 0 && z < 0.0009) // stoping criteria
|
||||
{
|
||||
goto END;
|
||||
}
|
||||
}
|
||||
|
||||
END:
|
||||
cout << "\n\nRoot: " << x;
|
||||
getch();
|
||||
}
|
||||
Reference in New Issue
Block a user