Gram Schmidt Orthonormalisation

时间:2015-07-12 22:34:28

标签: matlab

我正在为Gram Schmidt Orthogonalization编写以下代码。它说调用函数时出错。错误是什么以及如何纠正错误?

A =[1,1,1,1;-1,4,4,-1;4,-2,2,0];
A =A';
B=myGramschmidt(A);

function [B] = myGramschmidt(A)
x1=A(:,1);
x2=A(:,2);

x3=A(:,3);
v1=x1;
c = dot(v1);
v2 = x2-((dot(x2,v1)/c)* v1);
d = dot(v2);
v3 = x3-((dot(x3,v1)/c)* v1)-((dot(x3,v2)/d)* v2);
C=[v1,v2,v3];
V1=normc(v1);
V2=normc(v2);
V3=normc(v3);
B=[V1,V2,V3];
end

1 个答案:

答案 0 :(得分:0)

使用Wikipedia Gram-Schmidt页面,但Luis Mendo对您收到错误的原因是正确的。

function [B] = myGramschmidt(A)
    B = A;

    for k = 1:size(A, 1)
        for j = 1:k-1
            B(k, :) = B(k, :) - proj(B(j, :), A(k, :));
        end
    end
end

function p = proj(u, v)
% https://en.wikipedia.org/wiki/Gram%E2%80%93Schmidt_process#The_Gram.E2.80.93Schmidt_process
    p = dot(v, u) / dot(u, u) * u;
end