In (ℝ^3)

Pick three vectors:

u1={1,0,2};
u2={2,3,1};
u3={0,1,1};

A dot product shows these are not orthogonal:

u1.u2

Do Gram-Schmidt to get an orthogonal set.  First step is easy:

v1=u1

For second step, take second vector and subtract a piece in direction of first vector:

v2=u2-(u2.v1)/(v1.v1) v1

Redefine new second vector to get nice integer entries:

v2=(5/3)v2

Similar steps to get third vector in our orthogonal basis:

v3=u3-u3.v1/v1.v1 v1-u3.v2/v2.v2 v2

v3=3v3

Check orthogonality:

v1.v2

v1.v3

v2.v3

Now pick some other vector:

w={4,3,7}

Want to write w as a linear combination of v1, v2, v3.  Can compute expansion coefficients:

w.v1/v1.v1

w.v2/v2.v2

w.v3/v3.v3

Check that these coefficents work:

18/5 v1+8/15v2+1/3v3

Build a plot of the four vectors w, v1, v2, v3:

Graphics3D[{Thick,Black,Line[{{0,0,0},w}],Red,Line[{{0,0,0},v1}],
Green,Line[{{0,0,0},v2}],Blue,Line[{{0,0,0},v3}]},PlotRange->{{-10,10},{-10,10},{-10,10}}]

Create a manipulation showing the four vectors w, v1, v2, v3 along with a linear combination a1*v1+a2*v2+a3*v3.  Play with choices of a1, a2, a3 to get a linear combination (orange vector below) to match w (black vector below):

Manipulate[Graphics3D[{Thick,Black,Line[{{0,0,0},w}],Red,Line[{{0,0,0},v1}],Green,Line[{{0,0,0},v2}],Blue,Line[{{0,0,0},v3}],Orange,Line[{{0,0,0},a1*v1+a2*v2+a3*v3}]},
PlotRange->{{-10,10},{-10,10},{-10,10}}],
{{a1,0},-5,5},
{{a2,0},-5,5},
{{a3,0},-5,5}]

Here's another way to visualize this.  Plot the components of w:

ListPlot[w,
PlotStyle->PointSize[0.02],
Filling->Axis,
FillingStyle->Automatic,
PlotRange->{-10,10},
Ticks->{Range[0,4],Range[-10,10,2]}]

Create a manipulation showing the components of w and the components of the linear combination a1*v1+a2*v2+a3*v3.  Play with choices of a1, a2, a3 to get a linear combination that matches w:

Manipulate[
ListPlot[{w,a1*v1+a2*v2+a3*v3},
	PlotStyle->PointSize[0.02],
	Filling->Axis,
	FillingStyle->Automatic,
	PlotRange->{-10,10},
	Ticks->{Range[0,4],Range[-10,10,2]},
	ImageSize->Large],
{{a1,0},-4,4},
{{a2,0},-4,4},
{{a3,0},-4,4}]

In (L^2)[-π,π]

Define the function we looked at in our class example:

f[x_]=Piecewise[{{Pi^2/8(-2-2/Pi x),-Pi<x<-Pi/2},{Pi^2/8(2/Pi x),-Pi/2<x<Pi/2},{Pi^2/8(2-2/Pi x), Pi/2<x<Pi}}]

Plot to check we've got it right.  Note the Ticks option to control placement of tick marks.

Plot[f[x],{x,-Pi,Pi},
Ticks->{Range[-Pi,Pi,Pi/2],Range[-Pi^2/8,Pi^2/8,Pi^2/8]}]

Make a manipulation to play with matching a linear combination of sine functions to the given function:

Manipulate[Plot[{f[x],a1*Sin[ x]+a2*Sin[2x]+a3*Sin[3x],a2*Sin[2x],a3*Sin[3x]},{x,-Pi,Pi},
PlotRange->{-1.5,1.5},
ImageSize->Large],
{{a1,0},-2,2},
{{a2,0},-2,2},
{{a3,0},-2,2}]

Here's another version that shows the square of the difference between f(x) and the linear combination (multiplied by 10 so you can see things on the same scale).  We'll see that choosing the correct coefficients amounts to minimizing the total squared error (that is, to minimizing the total area under (f(x)-linear comb)^2).

Manipulate[Plot[{f[x],a1*Sin[ x]+a2*Sin[2x]+a3*Sin[3x],10*(f[x]-(a1*Sin[ x]+a2*Sin[2x]+a3*Sin[3x]))^2},{x,-Pi,Pi},
PlotRange->{-1.5,1.5},
ImageSize->Large],
{{a1,0},-2,2},
{{a2,0},-2,2},
{{a3,0},-2,2}]

We can compute the correct cofficients.   Note the use of the Assumptions option in the next two Integrate inputs:

fdotvk=Integrate[f[x] Sin[k x],{x,-Pi,Pi},Assumptions->k∈Integers]

vkdotvk=Integrate[ Sin[k x]^2,{x,-Pi,Pi},Assumptions->k∈Integers]

a[k_]=fdotv/vdotv

a[1]

a[2]

a[3]

Table[a[k],{k,1,9}]

Table[Sum[a[k]Sin[k x],{k,1,n}],{n,1,9}]

Manipulate[
Plot[{f[x],Sum[a[m]Sin[m x],{m,1,n}],10*(f[x]-Sum[a[m]Sin[m x],{m,1,n}])^2},{x,-Pi,Pi}, ImageSize->Large],{n,1,101,2}]