-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpolynomial-solver.cpp
68 lines (59 loc) · 1.57 KB
/
polynomial-solver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <cmath>
double GetFuncOutput(double input, int orderOfEqn, int* coefficients) // coeff in form an * xn an-1 * xn-1 ... a0 x0
{
double output;
for (int i = orderOfEqn; i >= 0; i--)
{
output += coefficients[i] * pow(input, i);
}
return output;
}
bool CheckChangeOfSign(orderOfEqn, int* coefficients, double[2] bounds)
{
lowerBoundOutput = GetFuncOutput(bound * -1, orderOfEqn, coefficients);
upperBoundOutput = GetFuncOutput(bound, orderOfEqn, coefficients);
if (GetFuncOutput(bound[0], orderOfEqn, coefficients) * GetFuncOutput(bound[1], orderOfEqn, coefficients) <= 0)
{
return true;
}
else
{
return false;
}
}
double[2] FindInitialBounds(int orderOfEqn, int* coefficients)
{
double bound = 0.1;
double increment = bound;
bool found = false;
//double* previousOutput
while(!found)
{
lowerBoundOutput = GetFuncOutput(bound * -1, orderOfEqn, coefficients);
upperBoundOutput = GetFuncOutput(bound, orderOfEqn, coefficients);
if (lowerBoundOutput * upperBoundOutput <= 0)
{
found = true;
}
else
{
bound += increment;
}
}
double[2] bounds = {-1 * bound, bound};
return bounds;
}
double Bisection(int orderOfEqn, int* coefficients, int dp) //dp = decimal places
{
//find initial bounds
double[2] bounds = FindInitialBounds(orderOfEqn, coefficients);
//half bounds each time
double diff;
bool found = false;
while (!found)
{
double midpoint = 0.5(bounds[0] + bounds[1]);
CheckChangeOfSign({})
}
//stop when change in result is lower than required significant figures
}