Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I know the Gram-Schmidt orthogonalization generates an orthonormal basis from an arbitrary basis. I need help with:

Write a program that inputs a list $\{b_1,\dotsc,b_n\}$ of linearly independent vectors and an inner proudct and outputs a list of orthonormal vectors $\{e_1,\dotsc,e_n\}$ with the same span.

            innerproduct1[f_,g_]:=Sum[(f/.t->i)(g/.t->i), {i,1,4}]

            gramschmidt2[innerproduct1_,list2_]:=
              Module[{k,u,v,e},
               k=Length[list2];
               Do[Subscript[v,i]=list2[[i]],{i,1,k}];
               Do[Subscript[u,i]=
                 Subscript[v,i]-
                  Sum[(innerproduct1[Subscript[v,i],Subscript[u,j]]/
                      innerproduct1[Subscript[u,j], Subscript[u,j]]*Subscript[u,j],  {j,1 ,i-1}];
              Subscript[e,i]=
               Subscript[u,i]/Sqrt[innerproduct1[Subscript[u,i].Subscript[u,i]]],{i, 1,k}];
            Table[Subscript[e,i],{i,1,k}]
            ]

Sorry if the program is wrong. I might be going ahead of what I read which I probably am.

I just learned somethings about the Gram-Schmidt process from reading an analysis text a couple days ago. I saw this question online and thought I would try to solve it with what I read. I came to some frustration and really wanted to know how to write a Mathematica program for this.

share|improve this question
    
What is the issue with the program you wrote? –  Daniel Lichtblau Nov 4 '13 at 20:14
    
I just fixed my program. I think this is what the question in bold is asking. Can someone please tell me if this is correct? Is there another way to do this? –  9599 Nov 4 '13 at 20:54
    
Did you check the Mathematica document about 'Orthogonalize' command. There you can specify method as Gram-Schmidt. If you check the "Applications" part you can also find a related example for linearly independent vectors, the result is a set orthonormal with the given inner product. –  s.s.o Nov 4 '13 at 21:02
    
It looks almost correct though I won't say I tested in any great detail. One issue I can see is use of a dot in the last call made to innerproduct1. I suspect a comma was wanted there. –  Daniel Lichtblau Nov 4 '13 at 21:15
    
@DanielLichtblau Is it ok if you can show me your written program. I tried this for a couple days now and would like to compare this to someone who knows this. –  9599 Nov 4 '13 at 22:26
add comment

1 Answer

up vote 0 down vote accepted

There is a built-in command for orthogonalizing vectors by generating a Gram-Schmidt basis (as suggested by s.s.o in a comment). Say you have some linearly independent vectors (generated here using RandomReal). For example, this generates three 10-D vectors at random.

z = RandomReal[{-1, 1}, {3, 10}]

orthoZ = Orthogonalize[z]

gives the three vectors in orthonormal form, as you can check:

orthoZ.Transpose[orthoZ] // Chop
{{1., 0, 0}, {0, 1., 0}, {0, 0, 1.}}

You can also specify your own inner product using the optional second argument of Orthogonalize. For example:

Orthogonalize[{1, x, x^2, x^3}, Integrate[Times[##], {x, -1, 1}] &]

uses $<x,y>=\int x(t) y(t) dt$ as the inner product and orthogonalizes the first few polynomials.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.