summaryrefslogtreecommitdiff
path: root/fits.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'fits.cpp')
-rw-r--r--fits.cpp21
1 files changed, 18 insertions, 3 deletions
diff --git a/fits.cpp b/fits.cpp
index f2f3a1d..d86af73 100644
--- a/fits.cpp
+++ b/fits.cpp
@@ -13,7 +13,7 @@ using Real = double;
using Vector = Eigen::Matrix<Real, Eigen::Dynamic, 1>;
/*
-inline Real basisFunctions(unsigned i, Real x) const {
+inline Real basisFunctions(unsigned N, unsigned i, Real x) {
if (i == 0) {
return 1;
} else {
@@ -22,9 +22,21 @@ inline Real basisFunctions(unsigned i, Real x) const {
}
*/
+Real legendreP(unsigned n, Real x) {
+ if (n == 0) return 1;
+ else if (n == 1) return x;
+ else return ((2 * (n - 1) + 1) * x * legendreP(n - 1, x) - (n - 1) * legendreP(n - 2, x)) / n;
+}
+
+inline Real basisFunctions(unsigned N, unsigned i, Real x) {
+ return legendreP(i, x);
+}
+
+/*
inline Real basisFunctions(unsigned N, unsigned i, Real x) {
return abs(x - i / (N - 1.0));
}
+*/
Real value(const Vector& coeffs, Real x) {
Real v = 0;
@@ -167,7 +179,7 @@ int main(int argc, char* argv[]) {
Rng r;
- std::list<std::tuple<Real, Real>> data = generateData([](Real x) {return x;}, M, σ, r);
+ std::list<std::tuple<Real, Real>> data = generateData([](Real x) {return std::cos(2 * M_PI * x);}, M, σ, r);
std::cout << std::setprecision(15);
@@ -177,7 +189,10 @@ int main(int argc, char* argv[]) {
std::cout << std::endl;
Vector a₀ = Vector::Zero(N);
- Vector a = stochasticGradientDescent(data, a₀, r, maxSteps);
+ for (Real& aa : a₀) {
+ aa = r.variate<Real, std::normal_distribution>(0, 0);
+ }
+ Vector a = gradientDescent(data, a₀, maxSteps);
for (unsigned i = 0; i < N; i++) {
std::cout << a[i] << " ";