QRGram<-function(A){
n=length(A[1,])
m=length(A[,1])
V=A
Q=0*A
R=matrix(0,n,n)
for (i in 1:n){
R[i,i]=sqrt(V[,i]%*%V[,i])
Q[,i]=V[,i]/R[i,i]
for (j in (i+1):n){
if (j!=(n+1)){
R[i,j]=Q[,i]%*%V[,j]
V[,j]=V[,j]-R[i,j]*Q[,i]
}
}}
return(list(Q=Q,R=R))
}
#-------Teste
c=c(3,2,1,1,2,3,2,1,1,1,3,1)
b=c(0,1,-2,1)
A=matrix(c,4,3)
A
B=QRGram(A)
Q=B$Q;Q
R=B$R; R
v=t(Q)%*%b; v
# Resolução de sistema triangular superior
TSup<-function(R,v){ # Retorna u tal que Ru=v
n=length(v);u=0*v
u[n]=v[n]/R[n,n]
for ( i in (n-1):1){
p=v[i]
for (j in (i+1):n){
p=p-u[j]*R[i,j]
}
u[i]=p/R[i,i]
}
u
}
u=TSup(R,v); u
print("Teste de erro"); A%*%u-b
To embed this project on your website, copy the following code and paste it into your website's HTML: