Gram Schmidt Process Algorithm

So I have an assignment to write algorithm code for Gram Schmidt process...I have done it in Matlab ,but when I run the code with input argument rand(some number), it gives me a result with variable ans... and some numbers in matrix... I want to create an output variable which won't be ans ,and can anyone see the code to tell me if it's okay? Thanks in advance,I would appreciate ur help..
% function Q = gramschmidt( A )
% FUNCTION computes orthonormal basis from independent vectors in A.
%
% Q = gramschmidt( A );
%
% INPUT :
% A - a matrix with *INDEPENDENT* vectors in columns
% (e.g. A = rand(3) will produce one)
%
% OUTPUT :
% Q - a matrix with orthonomal basis of A
%
%
% The vectors in A are independent BUT NOT YET orthonormal. Check A'*A. % If it is orthonormal, you should get strictly an identity matrix.
% number of vectors n = size( A, 2 );
% initialize output Q = zeros( n );
% turn every independent vector into a basis vector % (1) jth basis vector will be perpendicular to 1..j-1 previous found basis % (2) will be of length 1 (norm will be equal to 1) for j = 1 : n
% pick the jth independent vector
u = A( :, j );
% special case for j = 1: we will not run the following for loop. Will
% just normalize it and put as the first found basis. There are no
% previous basis to make orthogonal to.
% remove from raw "u" all components spanned on 1..j-1 bases, their
% contributions will be removed
% ==> this effectively makes jth independent vector orthogonal to all
% previous bases found in the previous steps.
% ==> enforcing orthogonality principle here. Not orthonormality yet.
for i = 1 : j - 1
u = u - proj( Q(:,i), A(:,j) );
end
% normalize it to length of 1 and store it
Q(:,j) = u ./ norm( u );
end
end
% projects a vector "a" on a direction "e" function p = proj( e, a )
% project "a" onto "e": (e' * a) / (e' * e) is the length (!) of "a" on "e" % multiplication by "e" is necessary to output the resulting vector which is % (colinear with "e") p = (e' * a) / (e' * e) .* e;
end
end

Answers (2)

Sebastián Soto
Sebastián Soto on 9 Nov 2022
for i = 1 : j - 1
u = u - proj( Q(:,i), A(:,j) );
end
% normalize it to length of 1 and store it
Q(:,j) = u ./ norm( u );
Yashwanth
Yashwanth 12 minutes ago
%ECE24063
t1 = 0:0.01:1;
t2 = 1:0.01:2;
t3 = 2:0.01:3;
t = [t1,t2,t3];
s1 = ones(1,length(t1));
s2 = ones(1,length(t2));
s3 = zeros(1,length(t3));
S_1 = [s1 s2 s3];
s4 = ones(1,length(t1));
s5 = -1*ones(1,length(t2));
s6 = zeros(1,length(t3));
S_2 = [s4 s5 s6];
s7 = ones(1,length(t1));
s8 = ones(1,length(t2));
s9 = ones(1,length(t3));
S_3 = [s7 s8 s9];
figure;
subplot(3,1,1)
plot(t,S_1)
title("S1 signal")
xlabel("Time")
ylabel("Amplitude")
subplot(3,1,2)
plot(t,S_2)
title("S2 signal")
xlabel("Time")
ylabel("Amplitude")
subplot(3,1,3)
plot(t,S_3)
title("S3 signal")
xlabel("Time")
ylabel("Amplitude")
e1 = sum(S_1.*S_1)*0.01;
ps1 = S_1/(sqrt(e1));
c21 = sum(S_2.*ps1)*0.01;
d2 = S_2 - (c21*ps1);
e2 = sum(d2.*d2)*0.01;
ps2 = d2/(sqrt(e2));
c31 = sum(S_3.*ps1)*0.01;
c32 = sum(S_3.*ps2)*0.01;
d3 = S_3 - ((c31*ps1)+(c32*ps2));
e3 = sum(d3.*d3)*0.01;
ps3 = d3/(sqrt(e3));
figure;
subplot(3,1,1)
plot(t,ps1)
title("1st basis function")
xlabel("Time")
ylabel("Amplitude")
subplot(3,1,2)
plot(t,ps2)
title("2nd basis function")
xlabel("Time")
ylabel("Amplitude")
subplot(3,1,3)
plot(t,ps3)
title("3rd basis function")
xlabel("Time")
ylabel("Amplitude")
c11 = sum(S_1.*ps1)*0.01;
c12 = sum(S_1.*ps2)*0.01;
c13 = sum(S_1.*ps3)*0.01;
S1 = [c11 c12 c13];
display(S1)
c22 = sum(S_2.*ps2)*0.01;
c23 = sum(S_2.*ps3)*0.01;
S2 = [c21 c22 c23];
display(S2)
c33 = sum(S_3.*ps3)*0.01;
S3 = [c31 c32 c33];
display(S3)

Categories

Find more on Linear Algebra in Help Center and File Exchange

Asked:

on 5 Oct 2015

Answered:

about 7 hours ago

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!