You are on page 1of 7

Tejas Bhavsar(20FTPHDE38)

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=

3.0000 -0.1000 -0.2000


0.1000 7.0000 -0.3000
0.3000 -0.2000 10.0000

A=

3.0000 -0.1000 -0.2000


0.1000 7.0000 -0.3000
0.3000 -0.2000 10.0000

n=

i=

2 3

A=
3.0000 -0.1000 -0.2000
0.0333 7.0000 -0.3000
0.1000 -0.2000 10.0000

A=

3.0000 -0.1000 -0.2000


0.0333 7.0033 -0.2933
0.1000 -0.1900 10.0200

i=

A=

3.0000 -0.1000 -0.2000


0.0333 7.0033 -0.2933
0.1000 -0.0271 10.0200

A=

3.0000 -0.1000 -0.2000


0.0333 7.0033 -0.2933
0.1000 -0.0271 10.0120

i=

1×0 empty double row vector


A=

3.0000 -0.1000 -0.2000


0.0333 7.0033 -0.2933
0.1000 -0.0271 10.0120

A=

3.0000 -0.1000 -0.2000


0.0333 7.0033 -0.2933
0.1000 -0.0271 10.0120

L=

1.0000 0 0
0.0333 1.0000 0
0.1000 -0.0271 1.0000

U=

3.0000 -0.1000 -0.2000


0 7.0033 -0.2933
0 0 10.0120
Problem:2
Polynomial Regression

 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 =

2.1000 7.7000 13.6000 27.2000 40.9000


61.1000

p1 =

11.6629 -3.7238

p2 =

1.8607 2.3593 2.4786

p3 =

0.0759 1.2913 3.3995 2.2508

y1 =

-3.7238 7.9390 19.6019 31.2648 42.9276


54.5905

y2 =

2.4786 6.6986 14.6400 26.3029 41.6871


60.7929

y3 =
2.2508 7.0175 14.8222 26.1206 41.3683
61.0206

>>

You might also like