relative error not working properly

조회 수: 3 (최근 30일)
Ifechukwu
Ifechukwu 2014년 7월 27일
댓글: Star Strider 2014년 7월 27일
I wrote this function
function [L,U,P,x]=LU_pivot(A,b)
% LU factorization with partial (row) pivoting
[n,n]=size(A);
x = zeros(n,1);
L=eye(n);
P=L;
U=A;
for k=1:n
[pivot m]=max(abs(U(k:n,k)));
m =m+k-1;
if m~=k
% interchange rows m and k in U
temp=U(k,:);
U(k,:)=U(m,:);
U(m,:)=temp;
% interchange rows m and k in P
temp=P(k,:);
P(k,:)=P(m,:);
P(m,:)=temp;
if k >= 2
temp=L(k,1:k-1);
L(k,1:k-1)=L(m,1:k-1);
L(m,1:k-1)=temp;
end
end
for j=k+1:n
L(j,k)=U(j,k)/U(k,k); %multipliers
U(j,:)=U(j,:)-L(j,k)*U(k,:); %rows
end
y=zeros(m,1); % initiation for y
y(1)=b(1)/L(1,1);
for i=2:m
%y(i)=B(i)-L(i,1)*y(1)-L(i,2)*y(2)-L(i,3)*y(3);
y(i)=-L(i,1)*y(1);
for k=2:i-1
y(i)=y(i)-L(i,k)*y(k);
end;
y(i)=(b(i)+y(i))/L(i,i);
end;
% Now we use this y to solve Ux = y
x=zeros(m,1);
x(m)=y(m)/U(m,m);
i=m-1;
q=0;
while (i~= 0)
x(i)=-U(i,m)*x(m);
q=i+1;
while (q~=m)
x(i)=x(i)-U(i,q)*x(q);
q=q+1;
end;
x(i)=(y(i)+x(i))/U(i,i);
i=i-1;
end;
end
Then I wrote this Script
n=input ('size of the system....>');
A= 10*rand(n,n);
z=10*rand(n,1);
b=zeros(n,1);
b= A*z;
[L,U,P,x]=LU_pivot(A,b);
relerr = norm(x-z)/norm(z)
But my relative error isn't working correctly please what's the problem

답변 (0개)

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by