Professional Documents
Culture Documents
Problem:1
LU decomposition method
Algorithm:
Step 1: Enter matrix 3 by 3 of ones.
Step 2: Enter values of from a11 to a33.
Step 3: decide value of n.
Step 4: Find value of i and find value of a11 to a33.
Step 5: Find value of L.
Step 6: Find value of U.
Step 7: END.
MATLAB code:
a=ones(3,3)
a(1,1)=3;a(1,2)=-0.1;a(1,3)=-
0.2;a(2,1)=0.1;a(2,2)=7;a(2,3)=-
0.3;a(3,1)=0.3;a(3,2)=-0.2;a(3,3)=10
A=a
n = size(A,1)
for k = 1:n
if A(k,k)==0
warning('LU factorization fails');
L = []; U = []; return;
end
i = k+1:n
A(i,k) = A(i,k)/A(k,k)
A(i,i) = A(i,i)-A(i,k)*A(k,i)
end
L = tril(A,-1)+eye(n)
U = triu(A)
Solution:
a=
1 1 1
1 1 1
1 1 1
a=
A=
n=
i=
2 3
A=
3.0000 -0.1000 -0.2000
0.0333 7.0000 -0.3000
0.1000 -0.2000 10.0000
A=
i=
A=
A=
i=
A=
L=
1.0000 0 0
0.0333 1.0000 0
0.1000 -0.0271 1.0000
U=
Algorithm:
Step 1: Enter values if x and y.
Step 2: Determine value of p1.
Step 3: Determine value of p2.
Step 4: Determine value of p3.
Step 5: Determine value of x1 and y1.
Step 6: Determine value of x2 and y2.
Step 7: Determine value of x3 and y3.
Step 8: Plot graph for (x,y).
Step 9: Describe title of graph.
Step 10: Describe label of x.
Step 11: Describe label of y.
Step 12: Plot graph for (x1,y1,x2,y2,x3,y3).
Step 13: Describe legends of graph.
Step 14: END.
MATLAB code:
x=[0,1,2,3,4,5]
y=[2.1,7.7,13.6,27.2,40.9,61.1]
p1=polyfit(x,y,1)
p2=polyfit(x,y,2)
p3=polyfit(x,y,3)
x1=x;y1=polyval(p1,x1)
x2=x;y2=polyval(p2,x2)
x3=x;y3=polyval(p3,x3)
figure
plot(x,y,'o')
title('Polynomial Fit')
xlabel('x')
ylabel('y')
hold on
plot(x1,y1,x2,y2,x3,y3)
legend({'x&y','first order fit','second order
fit','third order fit'},'Location','northwest')
Solution:
x =
0 1 2 3 4 5
y =
p1 =
11.6629 -3.7238
p2 =
p3 =
y1 =
y2 =
y3 =
2.2508 7.0175 14.8222 26.1206 41.3683
61.0206
>>