Download presentation
Presentation is loading. Please wait.
1
MATLAB EXAMPLES Matrix Solution Methods
58:110 Computer-Aided Engineering Spring 2005 MATLAB EXAMPLES Matrix Solution Methods Department of Mechanical and industrial engineering January 2005
2
Some useful functions det (A) Determinant lu(A) LU decomposition
cond(A) Matrix condition number inv(A) Matrix inverse rank(A) Matrix rank diag, trace Vector and sum of the diagonal elements tril,triu Lower and upper triangular part of matrix cgs, pcg (Preconditioned) conjugate gradients iterative linear equation solver
3
LU decomposition Here is an example to use LU decomposition for 4X4 matrix A: >> A=[ ; ; ; ] A = >> [l u p]=lu(A) l = u = p = Note: p is a permutation matrix corresponding to the pivoting strategy used. If the matrix is not diagonally dominant, p will not be an identity matrix.
4
Condition number In MATLAB, Condition number is defined by:
To calculate condition number of matrix A by MATLAB built-in function: >> cond(A) ans = Or by the definition, the product of 2-norm of A and inverse A: >> norm(A,2)*norm(inv(A),2)
5
Iterative methods – Jocobi method
The following M-file shows how to use Jocobi method in MATLAB: (jocobi_example.m) if j==i continue else s=s+A(i,j)*x0(j); end x1(i)=(b(i)-s)/A(i,i); if norm(x1-x0)<erp break x0=x1; % show the final solution x=x1 % show the total iteration number n_iteration=k % Iterative Solutions of linear euations:(1) Jocobi Method % Linear system: A x = b % Coefficient matrix A, right-hand side vector b A=[ ; ; ; ]; b= [-1;0;-3;1]; % Set initial value of x to zero column vector x0=zeros(1,4); % Set Maximum iteration number k_max k_max=1000; % Set the convergence control parameter erp erp=0.0001; % Show the q matrix q=diag(diag(A)) % loop for iterations for k=1:k_max for i=1:4 s=0.0; for j=1:4
6
Iterative methods- Jocobi method
Running M-file in command window: >> jocobi_r_ex q = x = n_iteration = 60
7
Iterative methods - Gauss-Seidel method
In the M-file for Gauss-seidel method, the only difference is the q matrix, which replaced by: q=tril(A), the codes in the loop changed as the following: for i=1:4 s1=0.0; s2=0.0; if (i==1) continue else for j=1:i-1 s1=s1+A(i,j)*x1(j); end for j=i+1:4 s2=s2+A(i,j)*x0(j); x1(i)=(b(i)-s1-s2)/A(i,i); Running M-file in command window >> gauss_example q = x = n_iteration = 10
8
Iterative methods - SOR method
Again, In the M-file for SOR method, the only difference is the q matrix, which replaced by: q1=tril(A)-diag(diag(A)); q2=diag(diag(A))/1.4; q=q1+q2; Note: the relaxation factor set to 1.4 for this case. for i=1:4 s1=0.0; s2=0.0; if (i==1) continue else for j=1:i-1 s1=s1+A(i,j)*x1(j); end for j=i+1:4 s2=s2+A(i,j)*x0(j); x1(i)=r*(b(i)-s1-s2)/A(i,i)+(1.0-r)*x0(i); Run this M-file: >> SOR_example q = r = 1.4000 x = n_iteration = 12
9
Iterative methods – Conjugate gradient method
Use cgs, pcg to solve above linear equations, the tolerance is 1.0E-6 (default) >> x=cgs(A,b, ) cgs converged at iteration 4 to a solution with relative residual 1.2e-015 x = 1.0000 >> x=pcg(A,b, ) pcg converged at iteration 4 to a solution with relative residual 3.1e-016
10
Iterative methods - Summary
Solution Total Iteration number Jocobi method ( , , , ) 60 Gauss –Seidel method ( , , , ) 10 SOR method ( , , , ) 12 Conjugate Gradient method ( , , , ) 4 Note: the exact solution is (-1,1,-1,1)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.