Gram-Schmidt Coding Assignment
Gram-Schmidt Coding Assignment
By
This operator projects the vector v orthogonally onto the line spanned by vector u. Then the 3 dimensions
projection process will work as follows:
The basic vector always be a unit vector to make this easy to be spanned by any value.
Example
Find the orthonormal basis for 3 dimensions vectors using gram-schmidt process by hand-calculation and
matlab program
V=IR3
1 6 3
v1 = 1 v2=4 v3= 6
1 5 4
6 1
15
=4 – 3
*1
5 1
1
= −1
0
u3 = v3 – Proj u1 (v3) – Proj u2 (v3)
3 1 1
18 (−3)
= 6 – 3
*1 – 2
* −1
4 1 0
−3/2
= −3/2
3
Then, make it unit vector
1 0.5774
𝑒1 1
e1 = ‖𝑒1‖ = * 1 = 0.5774
√3
1 0.5774
−1 −0.4082
𝑒2 1
e2 = ‖𝑒2‖ = * −1 = −0.4082
√6
2 0.8165
1 0.7071
𝑒3 1
e3 = ‖𝑒3‖ = * −1 = −0.7071
√2
0 0
We check the basis vector is orthonormal each other by multiplying dot product of two vector, and we get
zero for the result. Multipliplying by dot product produce the value of e1.e2 = e1.e3 = e2.e3 = 0. So the
basis is orthonormal.
>> v1=[1;1;1];
>> v2=[6;4;5];
>> v3=[3;6;4];
>> [e1,e2,e3]=gramschmidt(v1,v2,v3)
After input the initial vector, the orthonormal unit basis vector will appear on Command Window.
e1 =
0.5774
0.5774
0.5774
e2 =
0.7071
-0.7071
0
e3 =
0.4082
0.4082
-0.8165
And we can get the visual figure of initial and basis vector on one space coordinate as follow
The basis vector can be different each operation depend on which vector orthonormalized first and the
sequence of vector projection. But all the basis vector must be orthogonal each other. To make sure, we
can multiple two basis vector by dot product. If the result of multiplication is zero, so the basis vector is
orthogonal each other and the process is true.
From the result, e1.e2 = e2.e3 = e1.e3 = 0
So, the basis must be orthogonal.
APPENDIX
MATLAB SOURCE CODE
function[y1,y2,y3]=gramschmidt(a,b,c)
matrix=[a';b';c'];
r=rank(matrix);
l=length(matrix);
if r ~=l
error('coba lagi, matrix tidak linearly independent')
end
v1=a;
v2=b-(dot(a,b)/(dot(v1,v1)))*a;
v3=c-(dot(a,c)/dot(v1,v1)*a)-(dot(c,v2)/dot(v2,v2)*v2);
y1=v1/sqrt(dot(v1,v1));
y2=v2/sqrt(dot(v2,v2));
y3=v3/sqrt(dot(v3,v3));
starts=zeros(3,3);
ends=[y1';y2';y3'];
quiver3(starts(:,1),starts(:,2),starts(:,3),ends(:,1),ends(:,2),ends(:,3),'linewidth',1.5)
hold on, grid on
akhir=[a';b';c'];
quiver3(starts(:,1),starts(:,2),starts(:,3),akhir(:,1),akhir(:,2),akhir(:,3))
xlabel('sumbu-x')
ylabel('sumbu-y')
zlabel('sumbu-z')
title('Gram Schmidt Process')
legend('basis gramschmidt','vektor awal')
grid minor
START
NO
YES
END
REFERENCE