-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.cpp
33 lines (28 loc) · 1.03 KB
/
example.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
#include "../apsfind.h"
#include "elliptic_integral.h"
#include <cmath>
#include <cstdio>
// adapted from https://www.boost.org/doc/libs/1_77_0/libs/math/doc/html/math_toolkit/root_finding_examples/elliptic_eg.html
// elliptic_integral code from https://people.sc.fsu.edu/~jburkardt/c_src/elliptic_integral/elliptic_integral.html
struct RootFunctor
{
RootFunctor(double arcLength, double knownRadius): arc{arcLength}, rad{knownRadius} {}
double arc, rad;
double operator()(double unknownRadius)
{
double a = fmax(unknownRadius, rad);
double b = fmin(unknownRadius, rad);
double k = sqrt(1 - (b * b) / (a * a));
return 4 * a * elliptic_ek(k) - arc;
}
};
double ellipticRoot(double arcLength, double knownRadius)
{
double guess = sqrt(arcLength * arcLength / 16 - knownRadius * knownRadius);
RootFunctor functor{arcLength, knownRadius};
return apsfind(functor, guess - arcLength / 10, guess + arcLength / 10, nullptr);
}
int main()
{
printf("%10.6f\n", ellipticRoot(300, 28));
}