]> gitweb.michael.orlitzky.com - sage.d.git/commitdiff
Add an example of a least squares problem.
authorMichael Orlitzky <michael@orlitzky.com>
Sun, 18 Nov 2012 22:53:21 +0000 (17:53 -0500)
committerMichael Orlitzky <michael@orlitzky.com>
Sun, 18 Nov 2012 22:53:21 +0000 (17:53 -0500)
mjo/orthogonal_polynomials.py

index 0da947cd6f0bf21705f501ec4523868135ff658f..e8e9210f46437755dc99333ce3159f9b7abd5a71 100644 (file)
@@ -63,6 +63,26 @@ def legendre_p(n, x, a = -1, b = 1):
         sage: legendre_P(3, GF(11)(5))
         8
 
+    Solve a simple least squares problem over `[-\pi, \pi]`::
+
+        sage: a = -pi
+        sage: b = pi
+        sage: def inner_product(v1, v2):
+        ...       return integrate(v1*v2, x, a, b)
+        ...
+        sage: def norm(v):
+        ...       return sqrt(inner_product(v,v))
+        ...
+        sage: def project(basis, v):
+        ...       return sum([ inner_product(v, b)*b/norm(b)**2
+        ...                    for b in basis])
+        ...
+        sage: f = sin(x)
+        sage: legendre_basis = [ legendre_p(k, x, a, b) for k in range(0,4) ]
+        sage: proj = project(legendre_basis, f)
+        sage: proj.simplify_trig()
+        5/2*(7*(pi^2 - 15)*x^3 - 3*(pi^4 - 21*pi^2)*x)/pi^6
+
     TESTS:
 
     We should agree with Maxima for all `n`::