Sunteți pe pagina 1din 4

Facultad de Matemáticas

Álgebra Lineal Numérica

Laboratorio 3

Catedratico:
Dr. Freddy Vides

Alumno:
Wilfredo Ebanks

Cuenta:
20142002814

Julio 2020
Practica

Ejercicio 2: Escribir un programa Octave que implemente el método de Richardson. Para


ejecutar los programas se hara uso de una matriz de nxn siendo n= 5000. Para esto, se creo
una función que genere una matriz, que cumpla ser positiva dominante:

Script
function [A] = matrizgenerada (n)
A = diag (2:2:2* n) + diag (1/2:1/2:(1/2) *(n -2) ,2) + diag (1/4:1/4:(1/4) *(n -4) ,4)
A = A+ diag (3/2:1/2:(1/2) *n , -2) + diag (5/4:1/4:(1/4) *n , -4) ;
endfunction

Ejercicio 4: Escribir un programa Octave que implemente el método de Jacobi.

Script
function [x k r t] = Jacobi (A,b,x,iter,tol)
tic () ;
n = length (b) ;
y = zeros (n ,1) ;
for i =1: iter
for j =1: n
y(j) = (b(j) - (A(j ,1:j -1) *x (1:j -1) ) - (A(j,j +1: n) *x (1+ j:n) ) ) /A(j,j);
end
er = x -y;
%r = max (abs (er) ) / max (abs (x) ) ; Criterios para detener las iteraciones
r = norm (er ,inf) / norm (x, inf ) ;
x = y;
if r <= tol
k = i;
break ;
end
end
t = toc () ;
endfunction
>> A = diag (2:2:2* 5000) + diag (1/2:1/2:(1/2) *(5000 -2) ,2) + diag (1/4:1/4:(1/4) *(5
>> A = A+ diag (3/2:1/2:(1/2) *5000 , -2) + diag (5/4:1/4:(1/4) *5000 , -4) ;
>> b = randn(5000,1);
>> x = randn(5000,1);
>> [x k r t] =Jacobi(A,b,x,2,0.1)
Ejercicio 6: Escribir un programa Octave que implemente el método de Gauss-Seidel.

Script
function [x k r t] = gauss_seidel (A,b,x,iter , tol )
tic () ;
n= length (b) ;

1
y = x;
for i = 1: iter
for j =1: n
y(j) = (b(j) -(A(j ,1:j -1) *y (1:j -1) ) -(A(j,j +1: n) *x (1+ j:n) ) ) /A(j,j) ;
endfor
er = x-y;
%r = max (abs(er) ) / max (abs (x) ) ; Criterios para detener las iteraciones
r = norm (er ,inf) / norm (x ,2) ;
x=y;
if r <= tol
k=i;
break ;
endif
endfor
t = toc () ;
endfunction
>> A = diag (2:2:2* 5000) + diag (1/2:1/2:(1/2) *(5000 -2) ,2) + diag (1/4:1/4:(1/4) *(5
>> A = A+ diag (3/2:1/2:(1/2) *5000 , -2) + diag (5/4:1/4:(1/4) *5000 , -4) ;
>> b = randn(5000,1);
>> x = randn(5000,1);
>> [x k r t] =gauss_seidel(A,b,x,2,0.1)
Ejercicio 8: Escribir un programa Octave que implemente el método de SOR.

Script
function [x k r t] = sor (A,b,x,iter ,tol ,w)
tic () ;
n= length (b) ;
y = x;
for i = 1: iter
for j =1: n
y(j) = w*(b(j) -(A(j ,1:j -1) *y (1:j -1) ) -(A(j,j +1: n) *x (1+ j:n) ) ) /A(j,j) +(1 -
w) *y(j) ;
endfor
er = x-y;
%r = max (abs(er) ) / max (abs (x) ) ; Criterios para detener las iteraciones
r = norm (er ,inf) / norm (x ,2) ;
x=y;
if r <= tol
k=i;
break ;
endif
endfor
t = toc () ;
endfunction
>> A = diag (2:2:2* 5000) + diag (1/2:1/2:(1/2) *(5000 -2) ,2) + diag (1/4:1/4:(1/4) *(5
>> A = A+ diag (3/2:1/2:(1/2) *5000 , -2) + diag (5/4:1/4:(1/4) *5000 , -4) ;
>> b = randn(5000,1);
>> x = randn(5000,1);

2
>> [x k r t] =sor(A,b,x,2,0.1,0.9)
Ejercicio: Escribir un programa Octave que implemente tanto el algoritmo de gradiente
conjugado resultante de su investigación.

Script
function [x k r t] =gc(A,b,x,iter , tol )
tic () ;
r = b-A*x;
v = r;
y= x;
for i =1: iter
paso = (r’*r)/(v’*A*v) ; %Calculando el paso
y = x+ paso *v;
r0 = r;
r = r- paso *(A*v) ;
s =(r’*r)/( r0 ’*r0) ;
v = r+s*v; %busqueda
error = x-y;
%r = max (abs( error ) ) / max ( abs (x) ) ; %Criterios para detener las
iteraciones
r = norm (error ,inf ) / norm (x ,2) ;
x=y;
if r <= tol
k=i;
break ;
endif
end
t = toc () ;
end
>> A = diag (2:2:2* 5000) + diag (1/2:1/2:(1/2) *(5000 -2) ,2) + diag (1/4:1/4:(1/4) *(5
>> A = A+ diag (3/2:1/2:(1/2) *5000 , -2) + diag (5/4:1/4:(1/4) *5000 , -4) ;
>> b = randn(5000,1);
>> x = randn(5000,1);
>> [x k r t] =gc(A,b,x,2,0.1)

S-ar putea să vă placă și