Many basic equations in engineering and scientific calculations are based on the law of conservation, such as the conservation of mass. Mathematically, a balance equation like [A]{x}={b} can be established. Where {x} denotes the value of each component at equilibrium, and they denote the state or response of the system; The right-end vector {b} consists of constants independent of the state of the system and is usually expressed as external excitation. The matrix A is expressed as a coefficient matrix consisting of the parameters of the interaction or coupling relationship between the parts of the system. In engineering it means [interaction][response]=[excitation].
For a single equation, some of the root seeking methods described earlier can be used to solve it. However, in fact, there are some relationships that are coupled with each other, such as Kirchhoff's law of complex circuits. These relationships need to be represented as a system of linear algebraic equations. Here are some methods of solving linear algebraic equations using MATLAB, with emphasis on the Gauss elimination method.
Catalog
1. Negative Sum and Left Division
4. Solution of tridiagonal equations
1. Negative Sum and Left Division
For systems of linear algebraic equations such as A*x=b, MATLAB provides two direct solutions: inversion and left division.
-
Reverse
Matrix inversion function of MATLAB is inv(A)
x=inv(A)*b
-
be demoted
x=A\b
2. Kleim's Law
Students who have studied linear algebra should not be unfamiliar with the Kramer's Law. It will not be repeated here, but just about the implementation in MATLAB:
Problem: Solving
Code implementation:
A=[0.3 0.52 1;0.5 1 1.9;0.1 0.3 0.5]; b=[-0.01 0.67 -0.44]'; D=det(A); A(:,1)=b; x(1)=det(A)/D; A=[0.3 0.52 1;0.5 1 1.9;0.1 0.3 0.5]; A(:,2)=b; x(2)=det(A)/D; A=[0.3 0.52 1;0.5 1 1.9;0.1 0.3 0.5]; A(:,3)=b; x(3)=det(A)/D; x
Problem solving:
>> Clem x = -14.9000 -29.5000 19.8000
3. Gauss Elimination
For small equations, it is still convenient to use the Kramer's rule, but engineering is often confronted with a huge system of equations, at which point it is not enough to use the Kramer's rule alone.
Although Gauss elimination is one of the oldest methods for solving simultaneous equations, it is still one of the most important methods in practical applications today.
Principle:
General equations (A*x=b):
Step: Eliminate Yuan Forward --> Backward Regeneration
Forward elimination: The first phase is to reduce the A matrix to an upper triangular matrix. This is done by multiplying both left and right of the first equation by
Then subtract it from the second equation and you get rid of it.
Similarly, by doing this repeatedly, the original equation system can be transformed into a new Upper Trigonometric Equation System.
Backward generation: The second stage is to solve x using the Upper Trigonometric System of Equations.
Solve firstIt is then brought into the second reciprocal equation and solved
And so on.
This successfully solves x.
However, I don't know if you didn't find it, if you encountered it when performing forward extinction=0, or
Very small, so there is a problem encountered - "divisor cannot be 0". This requires that the largest number be selected as the "principal element" and the principal element as the new "divisor" each time it is executed.
The former Gauss method is called simple Gauss elimination, and the latter is the selected principal element Gauss elimination.
Code implementation:
function x = GaussPivot(A,b) %%Establishment of implementation code for Gauss elimination %Solve equation A*x=b %Steps: %(1)Forward elimination; %(2)Backward generation. %%Input: %A=coefficient matrix %b=Right vector %%Output: %x=Solution vectors of equations [m,n]=size(A); if m~=n,error("Coefficient matrix must be square");end nb=n+1; Aug=[A b]; %Forward elimination for k=1:n-1 %Select Primary Element(max Functions can return maximum and maximum indices) [big,i] = max(abs(Aug(k:n,k))); ipr = i+k-1; if ipr~=k Aug([k,ipr],:) = Aug([ipr,k],:);%Exchange: Use a number with a large absolute value as the primary element end for i=k+1:n factor = Aug(i,k)/Aug(k,k); Aug(i,k:nb) = Aug(i,k:nb)-factor*Aug(k,k:nb); end end %Backward Hypothesis x = zeros(n,1);%Create Solution Vector x(n)=Aug(n,nb)/Aug(n,n);%Find the last value first for i=n-1:-1:1 x(i)=(Aug(i,nb)-Aug(i,i+1:n)*x(i+1:n))/Aug(i,i); end end
Problem solving:
Question:
%%Gauss Elimination A=[0.3 0.52 1;0.5 1 1.9;0.1 0.3 0.5]; b=[-0.01 0.67 -0.44]'; x = GaussPivot(A,b)
Result:
>> GaussPivot_test x = -14.9000 -29.5000 19.8000
4. Solution of tridiagonal equations
The following groups of equations are often encountered in engineering:
Solving steps are still forward elimination--> backward generation, not only because of the sparsity of A, but also because of the time-consuming use of Gaussian elimination, which is specially designed in the book.
Code implementation:
function x = Tridiag(e,f,g,r) %Solving tridiagonal equations % Because the coefficient matrix of the tridiagonal equation is sparse, the amount of computation is equal to n Proportional rather than Gaussian elimination n^3 %%Input: %e =sub diagonals %f = Principal Diagonal %g =up diagonal %r = Right vector %%Output: %x=Solution vectors of equations n=length(f); for k=2:n factor=e(k)/f(k-1); f(k)=f(k)-factor*g(k-1); r(k)=r(k)-factor*r(k-1); end x(n)=r(n)/f(n); for k= n-1:-1:1 x(k)=(r(k)-g(k)*x(k+1))/f(k); end
Problem solving:
Solve the above problem
e=[0,-1,-1,-1]; f=[2.04,2.04,2.04,2.04]; g=[-1,-1,-1,0]; r=[40.8,0.8,0.8,200.8]; x = Tridiag(e,f,g,r)
>> Tridiag_test x = 65.9698 93.7785 124.5382 159.4795
Statement: This article is from the notes of Steven C. CHapra, author of "Engineering in MATLAB Implementation of Scientific Numeric Methods" (4th Edition). If you have fallacies or want to have a better understanding, please read the original book.