You are on page 1of 3

clear

clc
format long

for i=4:15
n=i; %Initialize the dimension of the system
H=hilb(n); %Create the Hibert matrix
xex=ones(n,1); %Initialize the exact solution of x
b=Hxex; %initialize the vector b
L,U,P,=lu(H); %Calculate the LU factorization of H
%From the permutation matrix, it's posible to see that the rows exchanged
%were: 2 to 3; 3 to 2; 4 to 5; and 5 to 4.
b2=Pb;
y=L\b2;
x=U\y; %Calculate the solution of the system.
er(i)=norm(xex-x); %Calculate of the normalized residual.
er1(i)=norm((xex-x)/(xex)); %Calculate the euclidian norm of the relative
error
end

semilogy(er,'--r') %Plot the relative error in semilog. scale
hold on
semilogy(er1,'g') %Plot the normal residual in semilog. scale

%Both plots have the same value, both curves are overlapped, because en the
relative error, each value is divided by 1.

%In the calculation of the error using as one initial parameter matrix
%Hilbert it is posilbe to see that for high values of n, the calculation of
%the error lack accuracy, it could occur because the calculation of the
%error array with the error calculation of the vector x.

clear
clc
a=1; %Initialize the value of the base of the triangle
for i=1:7
b=10^(i-1); %Calculate the value of the height of the rectangle
triangle
c=(a^2+b^2)^.5; %Calculate the value of the hypotenuse
P=(a+b+c)/2; %Calculate the perimeter
A(i)=ab/2; %Calculate the area value by the conventional
calculation
T(i)=(P(P-a)(P-b)(P-c))^.5; %Calculate the area value by the
%semiperimeter calculation
er(i)=abs(T(i)-A(i))/A(i); %Calculate the diference between the
%Area values.
end

plot(er)

You might also like