0% found this document useful (0 votes)
28 views7 pages

Pract Lab 02 Edison

The document describes three different numerical methods for solving systems of linear equations: Jacobi, Gauss-Seidel, and Cholesky decomposition. It provides the MATLAB code to implement each method and examples of their use, including applying each method to solve the same sample system of equations and reporting the output solutions and number of iterations.

Uploaded by

Francis Alberto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views7 pages

Pract Lab 02 Edison

The document describes three different numerical methods for solving systems of linear equations: Jacobi, Gauss-Seidel, and Cholesky decomposition. It provides the MATLAB code to implement each method and examples of their use, including applying each method to solve the same sample system of equations and reporting the output solutions and number of iterations.

Uploaded by

Francis Alberto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

PRACTICA CALIFICADA

PRGRAMACION MATLAB

METODO JACOBI
function [ sol, it] = jacobi22( A,b,x0,tol )
[n,n]=size(A);
for i=1:n
c(i)=b(i)/A(i,i);
for J=1:n
if i==J
m(i,J)=0;
else
m(i,J)=-A(i,J)/A(i,i)
end
end
end
e=10;it=0;
while e>=tol
x1=m*(x0)+c';
e=norm(x1-x0);
x0=x1;it=it+1;
end,
sol=x0;
it=it;
end

Resultado
A=
4
1
-1
1
>> b
b=
-2
-1
0
1
>> x0
x0 =
0
0

1 -1
4 -1
-1 5
-1 1

1
-1
1
3

0
0
>> tol
tol =
1.0000e-004
>> [ sol, it] = jacobi22( A,b,x0,tol )
m=
0 -0.2500
m=
0 -0.2500

0.2500

0 -0.2500

0.2500 -0.2500

0 -0.2500
-0.2500
0

0.2500 -0.2500
0
0

m=

m=

m=
0 -0.2500
-0.2500
0

0.2500 -0.2500
0.2500
0

m=
0 -0.2500
-0.2500
0

0.2500 -0.2500
0.2500 0.2500

m=
0 -0.2500
-0.2500
0
0.2000
0

0.2500 -0.2500
0.2500 0.2500
0
0

m=
0 -0.2500
-0.2500
0

0.2500 -0.2500
0.2500 0.2500

0.2000

0.2000

m=
0 -0.2500 0.2500 -0.2500
-0.2500
0 0.2500 0.2500
0.2000 0.2000
0 -0.2000
m=
0 -0.2500 0.2500 -0.2500
-0.2500
0 0.2500 0.2500
0.2000 0.2000
0 -0.2000
-0.3333
0
0
0
m=
0 -0.2500 0.2500 -0.2500
-0.2500
0 0.2500 0.2500
0.2000 0.2000
0 -0.2000
-0.3333 0.3333
0
0
m=
0 -0.2500 0.2500 -0.2500
-0.2500
0 0.2500 0.2500
0.2000 0.2000
0 -0.2000
-0.3333 0.3333 -0.3333
0
sol =
-0.7534
0.0411
-0.2808
0.6917
it =
18
METODO GAUSS SEIDEL
function [ sol, it] = gaussseidel ( A,b,x0,tol )
[n,n]=size (A);
D= diag(diag(A));
U= triu(A)-D;
L= A - triu(A);
e=10;it=0;
while e>=tol

x1=((D+L)^-1)*U*x0 + ((D+L)^-1)*b
e=norm(x1-x0);
x0=x1;it=it+1;
end,
sol=x0;
it=it;
end

Resultado
A=
4
1
-1
1

1 -1
4 -1
-1 5
-1 1

1
-1
1
3

>> b
b=
-2
-1
0
1
>> x0
x0 =
0
0
0
0
>> tol
tol =
1.0000e-004
>> [ sol, it] = gaussseidel ( A,b,x0,tol )
x1 =
-0.5000
-0.1250
-0.1250
0.5000
x1 =
-0.3750
-0.2500
-0.0250

0.3833
x1 =
-0.4604
-0.2245
-0.0603
0.4321
x1 =
-0.4330
-0.2347
-0.0471
0.4152
x1 =
-0.4431
-0.2312
-0.0518
0.4212
x1 =
-0.4395
-0.2325
-0.0502
0.4191
x1 =
-0.4408
-0.2320
-0.0508
0.4198
x1 =
-0.4404
-0.2322
-0.0505
0.4196
x1 =
-0.4405
-0.2321

-0.0506
0.4197
x1 =
-0.4405
-0.2321
-0.0506
0.4196
sol =
-0.4405
-0.2321
-0.0506
0.4196
it =
10
METODO CHOLESKY
function [L,LT]=choles(A)
n=length(A);
for i=1:n-1
A(i+1:n,i)=A(i+1:n,i)/A(i,i);
A(i+1:n,i+1:n)= A(i+1:n,i+1:n)-A(i+1:n,i)*A(i,i+1:n);
A(i,i:n)= A(i,i:n)/A(i,i)^0.5;
end
A(n,n)=A(n,n)^0.5;
LT=triu(A);L=LT';

Resultado

A =
4
1
-1
1

1
4
-1
-1

-1
-1
5
1

1
-1
1
3

>> [L,LT]=choles(A)
L =
2.0000
0.5000
-0.5000
0.5000

0
1.9365
-0.3873
-0.6455

0
0
2.1448
0.4663

0
0
0
1.4546

LT =
2.0000
0
0
0

0.5000
1.9365
0
0

-0.5000
-0.3873
2.1448
0

0.5000
-0.6455
0.4663
1.4546

You might also like