(c) Copyright 1994,1995,1996 by Evans M. Harrell, II. All rights reserved
This is an executed Mathematica notebook, which can be read with MathReader. If you are reading it with Mathematica, you may find it instructive to modify the inputs and see what results.
In[2]:=
RMSDist[f_,g_,a_,b_] \
:= Sqrt[Integrate[(f - g)^2, \
{x,a,b}]]
The RMS norm of a single function f could be obtained with the command
RMSDist[f,0,a,b].
In[3]:=
RMSDist[1, Cos[Pi x/2], -1,1]
Out[3]=
-(8 - 3 Pi) -8 + 3 Pi Sqrt[----------- + ---------] 2 Pi 2 Pi
In[4]:=
N[%]
Out[4]=
0.67344
What is the constant which is most like the cosine? 1/2? 1/4? Let's first check some values?
In[5]:=
{N[RMSDist[1/2,Cos[Pi x/2], -1,1]],
N[RMSDist[1/4,Cos[Pi x/2], -1,1]],
N[RMSDist[-1,Cos[Pi x/2], -1,1]]}
Out[5]=
{0.476194, 0.698842, 2.3551}
In[6]:=
Plot[{Cos[Pi x/2], 1,1/2,-1}, {x, -1,1}]
Out[7]=
-Graphics-
The closest constant to Cos[Pi x/2] would be, geometrically speaking, the projection of the sine onto the direction of the constant functions, or, as we have defined it in the notes,
In[8]:=
Projf[f_,g_,a_,b_] \
:= g[x] Integrate[f[t] g[t] , {t,a,b}] \
/ Integrate[g[t]^2, {t,a,b}]
In[9]:=
f[x_] := Cos[Pi x/2]
One[x_] := 1
Projf[f,One,-1,1]
Out[9]=
2 -- Pi
In[10]:=
N[RMSDist[2/Pi,Cos[Pi x/2], -1,1]]
Out[10]=
0.435236
In[11]:=
Plot[{2/Pi,Cos[Pi x/2]}, {x,-1,1}]
Out[12]=
-Graphics-
We ought to do a better job of approximating Cos[Pi x/2] with a polynomial of 3rd or 4th degree. You may initially think of the Taylor series at x = 0, and wish regard Sin[x] as close to 0 + x + 0 - x^3/6. Or you might try the Taylor series at x= Pi, which is built into Mathematica in the Series command.
In[13]:=
Series[Cos[Pi x/2], {x, 0, 3}]
Out[13]=
2 2 Pi x 4 1 - ------ + O[x] 8
In[14]:=
Plot[{1-(Pi x)^2 /8, Cos[Pi x/2]}, {x,-1,1}]
Out[15]=
-Graphics-
In[16]:=
N[RMSDist[1-(Pi x)^2 /8, Cos[Pi x/2], -1,1]]
Out[16]=
0.111836
Perhaps it is surprising to learn that there are other polynomial expressions which do a much better job of approximating a function such as the sine than does the Taylor series about any point. They are called the Legendre polynomials
In[17]:=
P0[x_] := LegendreP[0,x]
P1[x_] := LegendreP[1,x]
P2[x_] := LegendreP[2,x]
P3[x_] := LegendreP[3,x]
In[18]:=
Projf[f, P0, -1,1]
Out[18]=
2 -- Pi
In[19]:=
Projf[f, P2, -1,1]
Out[19]=
2 2 24 - 2 Pi -24 + 2 Pi 2 5 (-(----------) + -----