diff options
| author | mzuenni <michi.zuendorf@gmail.com> | 2022-06-27 17:19:28 +0200 |
|---|---|---|
| committer | mzuenni <michi.zuendorf@gmail.com> | 2022-06-27 17:19:28 +0200 |
| commit | 5ab8a5088b729a9953b8dff1b2a985dc8fb2098b (patch) | |
| tree | ed40d6936c0e9eee40ba62751cbf99ecddbaddc2 /math/goldenSectionSearch.cpp | |
| parent | adabbad9c51cf7cd3874bfde8eac1fbcf84fec10 (diff) | |
updated tcr
Diffstat (limited to 'math/goldenSectionSearch.cpp')
| -rw-r--r-- | math/goldenSectionSearch.cpp | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/math/goldenSectionSearch.cpp b/math/goldenSectionSearch.cpp new file mode 100644 index 0000000..2be8c2d --- /dev/null +++ b/math/goldenSectionSearch.cpp @@ -0,0 +1,14 @@ +ld gss(ld l, ld r, function<ld(ld)> f) { + ld r = (sqrtl(5.0l)-1)/2; + ld x1 = b - r*(b-a), x2 = a + r*(b-a); + ld f1 = f(x1), f2 = f(x2); + for (int i = 0; i < 200; i++) { + if (f1 < f2) { //change to > to find maximum + b = x2; x2 = x1; f2 = f1; + x1 = b - r*(b-a); f1 = f(x1); + } else { + a = x1; x1 = x2; f1 = f2; + x2 = a + r*(b-a); f2 = f(x2); + }} + return a; +} |
