We can use the built-in function lu()
in MATLAB to obtain the LU decomposition of a matrix. Here's how we can obtain the solution of the system of linear equation and its LU decomposition:
main.m250 chars14 lines
The output should be:
main.m253 chars15 lines
Therefore, the solution to the system of linear equation is x=[1,1,-2]
, and the matrices L
and U
are:
main.m126 chars10 lines
Note: LU decomposition may fail if a pivot element is zero or nearly zero. In such cases, we should use a pivoting strategy to avoid this.
gistlibby LogSnag