Approximation by orthogonal projection 

We are going to  approximate the function v = sin(x) on the interval [0 , Pi/2]  

using the orthogonal set P0(z), P1(z), P2(z), P3(z),  where  z = 4x/Pi - 1  

(in general on the interval [a,b] use the Pn( (2x-a-b)/(b-a) )  

> e1 := 1; 1; e2 := `+`(`/`(`*`(4, `*`(x)), `*`(Pi)), `-`(1)); 1; e3 := `+`(`*`(`/`(3, 2), `*`(`^`(`+`(`/`(`*`(4, `*`(x)), `*`(Pi)), `-`(1)), 2))), `-`(`/`(1, 2))); 1; e4 := `+`(`*`(`/`(5, 2), `*`(`^`(`...
 

 

 

 

1
`+`(`/`(`*`(4, `*`(x)), `*`(Pi)), `-`(1))
`+`(`*`(`/`(3, 2), `*`(`^`(`+`(`/`(`*`(4, `*`(x)), `*`(Pi)), `-`(1)), 2))), `-`(`/`(1, 2)))
`+`(`*`(`/`(5, 2), `*`(`^`(`+`(`/`(`*`(4, `*`(x)), `*`(Pi)), `-`(1)), 3))), `-`(`/`(`*`(6, `*`(x)), `*`(Pi))), `/`(3, 2)) (1)
 

Check these are orthogonal if you like.  

> v := sin(x)
 

sin(x) (2)
 

Compute the Fourier coefficients 

> alpha1 := `/`(`*`(int(`*`(v, `*`(e1)), x = 0 .. `+`(`*`(`/`(1, 2), `*`(Pi))))), `*`(int(`*`(e1, e1), x = 0 .. `+`(`*`(`/`(1, 2), `*`(Pi))))))
 

`+`(`/`(`*`(2), `*`(Pi))) (3)
 

> alpha2 := `/`(`*`(int(`*`(v, `*`(e2)), x = 0 .. `+`(`*`(`/`(1, 2), `*`(Pi))))), `*`(int(`*`(e2, e2), x = 0 .. `+`(`*`(`/`(1, 2), `*`(Pi))))))
 

`+`(`-`(`/`(`*`(6, `*`(`+`(Pi, `-`(4)))), `*`(`^`(Pi, 2))))) (4)
 

> alpha3 := `/`(`*`(int(`*`(v, `*`(e3)), x = 0 .. `+`(`*`(`/`(1, 2), `*`(Pi))))), `*`(int(`*`(e3, e3), x = 0 .. `+`(`*`(`/`(1, 2), `*`(Pi))))))
 

`+`(`/`(`*`(10, `*`(`+`(`-`(48), `*`(`^`(Pi, 2)), `*`(12, `*`(Pi))))), `*`(`^`(Pi, 3)))) (5)
 

> alpha4 := `/`(`*`(int(`*`(v, `*`(e4)), x = 0 .. `+`(`*`(`/`(1, 2), `*`(Pi))))), `*`(int(`*`(e4, e4), x = 0 .. `+`(`*`(`/`(1, 2), `*`(Pi))))))
 

`+`(`-`(`/`(`*`(14, `*`(`+`(`-`(`*`(240, `*`(Pi))), `*`(`^`(Pi, 3)), 960, `-`(`*`(24, `*`(`^`(Pi, 2))))))), `*`(`^`(Pi, 4))))) (6)
 

Approximation using 3 basis functions:  

> vp := `+`(`*`(alpha1, `*`(e1)), `*`(alpha2, `*`(e2)), `*`(alpha3, `*`(e3)))
 

`+`(`/`(`*`(2), `*`(Pi)), `-`(`/`(`*`(6, `*`(`+`(Pi, `-`(4)), `*`(`+`(`/`(`*`(4, `*`(x)), `*`(Pi)), `-`(1))))), `*`(`^`(Pi, 2)))), `/`(`*`(10, `*`(`+`(`-`(48), `*`(`^`(Pi, 2)), `*`(12, `*`(Pi))), `*`(... (7)
 

> evalf(expand(vp))
 

`+`(`-`(0.2432494e-1), `*`(1.19574506, `*`(x)), `-`(`*`(.338239999, `*`(`^`(x, 2))))) (8)
 

> plot([sin(x), vp], x = 0 .. `+`(`*`(`/`(1, 2), `*`(Pi))))
 

Plot_2d
 

> sqrt(evalf(int(`*`(`^`(`+`(v, `-`(vp)), 2)), x = 0 .. `+`(`*`(`/`(1, 2), `*`(Pi))))))
 

0.1050504122e-1 (9)
 

Approximation using 4 basis functions  

> vp := `+`(`*`(alpha1, `*`(e1)), `*`(alpha2, `*`(e2)), `*`(alpha3, `*`(e3)), `*`(alpha4, `*`(e4)))
 

`+`(`/`(`*`(2), `*`(Pi)), `-`(`/`(`*`(6, `*`(`+`(Pi, `-`(4)), `*`(`+`(`/`(`*`(4, `*`(x)), `*`(Pi)), `-`(1))))), `*`(`^`(Pi, 2)))), `/`(`*`(10, `*`(`+`(`-`(48), `*`(`^`(Pi, 2)), `*`(12, `*`(Pi))), `*`(...
`+`(`/`(`*`(2), `*`(Pi)), `-`(`/`(`*`(6, `*`(`+`(Pi, `-`(4)), `*`(`+`(`/`(`*`(4, `*`(x)), `*`(Pi)), `-`(1))))), `*`(`^`(Pi, 2)))), `/`(`*`(10, `*`(`+`(`-`(48), `*`(`^`(Pi, 2)), `*`(12, `*`(Pi))), `*`(...
(10)
 

> evalf(expand(vp))
 

`+`(`-`(0.22585e-2), `*`(1.0271697, `*`(x)), `-`(`*`(0.6994431e-1, `*`(`^`(x, 2)))), `-`(`*`(.11386894, `*`(`^`(x, 3))))) (11)
 

> plot([sin(x), vp], x = 0 .. `+`(`*`(`/`(1, 2), `*`(Pi))))
 

Plot_2d
 

> sqrt(evalf(int(`*`(`^`(`+`(v, `-`(vp)), 2)), x = 0 .. `+`(`*`(`/`(1, 2), `*`(Pi))))))
 

0.9553322608e-3 (12)
 

>