-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
40 lines (33 loc) · 1.45 KB
/
Copy pathexample.cpp
File metadata and controls
40 lines (33 loc) · 1.45 KB
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
#include <iostream>
#include "zeroin.hpp" // your header with Func, Poly, zero_in
int main()
{
// --- Test 1: Poly root finding ---
// p(x) = x^2 - 2, root at sqrt(2) ~ 1.41421356
Poly<double> p({-2.0, 0.0, 1.0});
double r1 = zero_in<Func<double>, double>(p, 1.0, 2.0);
std::cout << "sqrt(2) ~ " << r1 << "\n";
// --- Test 2: Trig function ---
// sin(x) = 0 near x = pi ~ 3.14159
auto f = Sin<double>();
double r2 = zero_in<Func<double>, double>(f, 3.0, 3.5);
std::cout << "pi ~ " << r2 << "\n";
// --- Test 3: Composed expression ---
// sin(x) + cos(x) = 0 near x = 3pi/4 ~ 2.356
auto g = Sin<double>() + Cos<double>();
double r3 = zero_in<Func<double>, double>(g, 2.0, 3.0);
std::cout << "3pi/4 ~ " << r3 << "\n";
// --- Test 4: Deflation ---
// p(x) = (x-1)(x-2)(x-3) = x^3 - 6x^2 + 11x - 6
Poly<double> cubic({-6.0, 11.0, -6.0, 1.0});
double rem = 0.0;
double root1 = zero_in<Func<double>, double>(cubic, 0.5, 1.5);
Poly<double> deflated = cubic.deflate(root1, &rem);
std::cout << "root1 ~ " << root1 << " remainder: " << rem << "\n";
double root2 = zero_in<Func<double>, double>(deflated, 1.5, 2.5);
Poly<double> deflated2 = deflated.deflate(root2, &rem);
std::cout << "root2 ~ " << root2 << " remainder: " << rem << "\n";
double root3 = zero_in<Func<double>, double>(deflated2, 2.5, 3.5);
std::cout << "root3 ~ " << root3 << "\n";
return 0;
}