gram01.mw

Gram-Schmidt Examples.

Ingnore this stuff:

 > restart; with(LinearAlgebra):

 > ip := proc(u, v) DotProduct(u,v, conjugate=false): end;

 > nrm := proc(v) sqrt(simplify(ip(v,v))); end;

 > nrm2 := proc(v) simplify(ip(v,v)); end;

The inner product will be denote by "ip(u,v)", where u and v are vectors.  For example:

 > X := Vector(4, symbol=x);  Y := Vector(4, symbol=y);

 > ip(X,Y);

We'll use "nrm2(v)" for the norm squared of v.

 > nrm2(X);

and "nrm(v)" will be the norm of v:

 > nrm(X);

Example: Gram-Schmidit in 4 dimensional space.

Problem: apply the gram-schmidt process to the following 4 vectors.

 > v1 := <1, 1, 0, 2>; v2 := <1, 0, 1, -1>; v3:=<1, 0, 0, 1>; v4 := <1, 1, 2, 1>;

Check that these vectors form a basis:

 > M := ;

 > Determinant(M);

Begin the Gram-Schmidt Process:

 > u1 := v1/nrm(v1);

 > nrm(v1);

 > u2p := v2 - ip(v2, u1)*u1;

 > nrm(u2p);

 > u2 := u2p/nrm(u2p);

 > u3p := v3 - ip(v3, u1)*u1 - ip(v3, u2)*u2;

 > nrm(u3p);

 > u3 := u3p/nrm(u3p);

 > u4p := v4 - ip(v4, u1)*u1 -ip(v4, u2)*u2 -ip(v4,u3)*u3;

 > nrm(u4p);

 > u4 := u4p/nrm(u4p);

Check that these are really orthogonal to each other:

 > ip(u1, u2);

 > ip(u1, u3);

 > ip(u1, u4);

 > ip(u2, u3);

 > ip(u2, u4);

 > ip(u3, u4);

 >

 >

Modified Gram-Schmidt on the same vectors:

 >

 > u1p := v1;

 > u2p := v2 - ip(v2, u1p)*u1p/nrm2(u1p);

 > u3p := v3 - ip(v3,u1p)*u1p/nrm2(u1p) -ip(v3, u2p)*u2p/nrm2(u2p);

 > u4p := v4 - ip(v4, u1p)*u1p/nrm2(u1p)-ip(v4,u2p)*u2p/nrm2(u2p) -ip(v4, u3p)*u3p/nrm2(u3p);

 > u1 := u1p/nrm(u1p);

 > u2 := u2p/nrm(u2p);

 > u3 := u3p/nrm(u3p);

 > u4 := u4p/nrm(u4p);

 >

Consider the following vector:

 > w := <1, 2, 4, 1>;

 >

Calculate the projection of w onto the span of u1 and u2:

 > p := ip(w,  u1)*u1 + ip(w, u2)*u2;

Calculate the matrix of the projection onto the span of u1 and u2:

 > U := ;

 > Pr := U.Transpose(U);

 >

Check that this gives the same result for the projection of w:

 > Pr.w;

Example in 3 dimensions

 > v1 := <1, 1, 0>; v2 := < 0, 1, 1>; v3 := <1, -1, 1>;

 > M := ;

 > Determinant(M);

 > u1 := v1/nrm(v1);

 > u2p := v2 - ip(v2, u1)*u1;

 > u2 := u2p/nrm(u2p);

 > u3p := v3 - ip(v3, u1)*u1 - ip(v3, u2)*u2;

 > u3 := u3p/nrm(u3p);

 > u1, u2, u3;

 > ip(u1, u2);

 > ip(u1, u3);

 > ip(u2, u3);

 >

 >

 >

Example in the space C[0,1]

 >

We use the inner product:

 > ip := proc(f,g) int(f*g, x=0..1); end;

 > ip(x,x);

Consider the subspaces spanned by the following polynomials.  Use Gram-Schmidt to find an ON basis of this subspace.

 > v1 := 1; v2 := x; v3 := x^2; v4 := x^3;

 >

 >

 > u1 := v1/nrm(v1);

 > u2p := v2 - ip(v2,u1)*u1;

 > u2 := u2p/nrm(u2p);

 > u3p := v3 - ip(v3, u1)*u1 -ip(v3, u2)*u2;

 > u3 := u3p/nrm(u3p);

 > u4p := v4 - ip(v4,u1)*u1 - ip(v4, u2)*u2 -ip(v4,u3)*u3;

 > u4 := u4p/nrm(u4p);

 >

 >

Same problem, modified Gram-Schmidt.

 >

 > u1p := v1;

 > u2p := v2 - ip(v2, u1p)*u1p/nrm2(u1p);

 > u3p := v3 - ip(v3, u1p)*u1p/nrm2(u1p) - ip(v3, u2p)*u2p/nrm2(u2p);

 > u4p := v4 - ip(v4, u1p)*u1p/nrm2(u1p) - ip(v4, u2p)*u2p/nrm2(u2p) - ip(v4, u3p)*u3p/nrm2(u3p);

 > u1 := u1p/nrm(u1p);

 > u2 := u2p/nrm(u2p);

 > u3 := u3p/nrm(u3p);

 > u4 := u4p/nrm(u4p);

Plot these othogonal polynomials to see what they look like:

 > plot([u1, u2, u3, u4], x=0..1);

Check that u3 and u4 are othogonal:

 > Int(u3*u4,x=0..1);

 > value(%);

Check that u4 is a unit vector:

 > plot(u4^2, x=0..1);

 > Int(u4^2, x=0..1);

 > value(%);

 >