代做Math 105A Report Lab 5调试Matlab程序
- 首页 >> C/C++编程Math 105A Report Lab 5
Purpose/Objective
In this lab, we will solve two linear systems using both exact methods and iter- ative methods (Jacobi and Gauss-Seidel). We will analyze the exact solutions, eigenvalues, eigenvectors, and spectral radii for the iterative methods. The goal is to implement these methods in MATLAB and compare the results.
Introduction
The Jacobi and Gauss-Seidel methods are iterative techniques used for solving linear systems of equations. These methods are especially useful when dealing with large systems where direct methods like Gaussian elimination are compu- tationally expensive. The spectral radius of the iteration matrix is a key factor in determining the convergence of these methods.
Functions
Exact Solution Function
function x = exact solution (A, b) x = A \ b ;
end
Eigenvalues and Eigenvectors Function
function [ eigenvalues , eigenvectors ] = find eigenvalues eigenvectors (A)
[V, D] = eig (A) ;
eigenvalues = diag(D) ;
eigenvectors = V;
end
Spectral Radius Function
function [ rho jacobi , rho gauss seidel ] = spectral radius (A)
D = diag(diag(A) ) ; L = tril (A, −1);
U = triu (A, 1);
T jacobi = inv(D) * (L + U) ;
rho jacobi = max(abs( eig ( T jacobi ) ) ) ; T gauss seidel = inv(D − L) * U;
rho gauss seidel = max(abs( eig ( T gauss seidel ) ) ) ;
end
Jacobi Method Function
function x = jacobi method (A, b , x0 , tol , max iter )
n = length (b ) ; x = x0 ;
D = diag(diag(A) ) ; L = tril (A, −1);
U = triu (A, 1);
for k = 1: max iter
x new = inv(D) * (b − (L + U) * x ) ; if norm(x new − x , inf ) < tol
x = x new ; return ;
end
x = x new ;
end
disp ( ’Max – number – of – iterations – exceeded ’ ) ;
end
Gauss-Seidel Method Function
function x = gauss seidel method (A, b , x0 , tol , max iter )
n = length (b ) ; x = x0 ;
L = tril (A, −1);
U = triu (A, 1);
D = diag(diag(A) ) ;
for k = 1: max iter
for i = 1:n
x( i ) = (b( i ) − L( i , : ) * x − U( i , : ) * x0) / A( i , i ) ;
end
if norm(x − x0 , inf ) < tol
return ;
end
x0 = x ;
end
disp ( ’Max – number – of – iterations – exceeded ’ ) ;
end
Results
Problem 1
• Exact Solution:
1
2
-1
• Eigenvalues:
2 .0000 + 2 .2361i 2 .0000 - 2 .2361i 2 .0000 + 0 .0000i
• Eigenvectors: |
|
0 .1890 + 0 .4226i |
0 .1890 - 0 .4226i -0 .5774 + 0 .0000i |
0 .7559 + 0 .0000i |
0 .7559 + 0 .0000i 0 .5774 + 0 .0000i |
-0 .1890 + 0 .4226i |
-0 .1890 - 0 .4226i 0 .5774 + 0 .0000i |
• Spectral Radius for Jacobi Method: 1.1180
• Spectral Radius for Gauss-Seidel Method: 1.6514
Problem 2
• Exact Solution:
1
2
-1
• Eigenvalues:
1 .0000 + 0 .0000i 1 .0000 - 0 .0000i 1 .0000 + 0 .0000i
• Eigenvectors: |
|
-0 .5773 + 0 .0000i |
-0 .5773 - 0 .0000i 0 .5774 + 0 .0000i |
0 .5774 - 0 .0000i |
0 .5774 + 0 .0000i -0 .5774 + 0 .0000i |
0 .5774 + 0 .0000i |
0 .5774 + 0 .0000i -0 .5773 + 0 .0000i |
• Spectral Radius for Jacobi Method: 1.0810e-05
• Spectral Radius for Gauss-Seidel Method: 4.8284
Iterative Methods Results
• Problem 1 - Jacobi Method Solution:
Max number of iterations exceeded
-20.8279
2.0000
-22.8279
• Problem 1 - Gauss-Seidel Method Solution:
Max number of iterations exceeded
1.0000
2.0000
-1.0000
• Problem 2 - Jacobi Method Solution:
1
2
-1
• Problem 2 - Gauss-Seidel Method Solution:
Max number of iterations exceeded
1 .0e+09 *
1.3086
-1.3254
0.0336
Conclusion
For the first problem, neither the Jacobi nor Gauss-Seidel methods converged within 25 iterations because the spectral radius for both methods was greater than 1. It suggests that the iterative methods diverge, leading to the observed lack of convergence. For the second problem, the Jacobi method converged suc- cessfully within the 25 iterations, while the Gauss-Seidel method did not. The Jacobi method’s spectral radius, much less than 1, indicates good convergence. The Gauss-Seidel method’s spectral radius was 4.8284, much greater than 1, which explains why it did not converge and resulted in extremely large values. The results of the iterative methods show the importance of the spectral radius in determining convergence. A spectral radius less than 1 indicates potential convergence, while a spectral radius greater than 1 suggests divergence.