1、双击matlab软件图标,打开matlab软件,可以看到matlab软件的界面。
3、使用函数chol(A)对矩阵A进行Cholesky分解。
4、使用函数表达式[B,C]=lu(A)对矩阵进行LU分解,也成为高斯消去法。其中B是下三角矩阵,C是上三角矩阵。
5、使用函数magic(4)创建一个4x4的矩阵A。使用函数表达式[Q,R]=qr(A),对矩阵A进行QR分解,其中Q是正交矩阵。
matlab有多种LU分解程序下面算一种:function [L,U]=myLU(A)
%实现对矩阵A的LU分解,L为下三角矩阵
A
[n,n]=size(A)
L=zeros(n,n)
U=zeros(n,n)
for i=1:n
L(i,i)=1
end
for k=1:n
for j=k:n
U(k,j)=A(k,j)-sum(L(k,1:k-1).*U(1:k-1,j)')
end
for i=k+1:n
L(i,k)=(A(i,k)-sum(L(i,1:k-1).*U(1:k-1,k)'))/U(k,k)
end
end
用法,在控制台输入
A=[1 2 3 -4-3 -4 -12 132 10 0 -34 14 9 -13]
function X=Ni(A)%Input - A is an N x N matrix
%Output - I is an N x N inverse matrix of A
%and I(j,:)containing the solution to AX(:,j) =E(:,j).
%Initialize X, Y,the temporary storage matrix C, and the row
% permutation information matrix R
[N,N]=size(A)
B=eye(N) %B is an N x N identity matrix
X=zeros(N,N)
Y=zeros(N,N)
C=zeros(1,N)
R=1:N
%the next steps is to find the factorization(factorize for only once)
for p=1:N-1
%Find the pivot row for column p
[max1, j]=max(abs(A(p:N,p)))
%Interchange row p and j
C=A(p,:)
A(p,:)=A(j+p-1,:)
A(j+p-1,:)=C
d=R(p)
R(p)=R(j+p-1)
R(j+p-1)=d
if A(p,p)==0
'A is singular. No unique solution'
break
end
%Calculate multiplier and place in subdiagonal portion of A
for k=p+1:N
mult=A(k,p)/A(p,p)
A(k,p) = mult
A(k,p+1:N)=A(k,p+1:N)-mult*A(p,p+1:N)
end
end
for j=1:N
%when j is fixed then the method is similar to the Program 3.3
%Solve for Y(:,j)
Y(1,j) = B(R(1),j)
for k=2:N
Y(k,j)= B(R(k),j)-A(k,1:k-1)*Y(1:k-1,j)
end
%Solve for X(:,j)
X(N,j)=Y(N,j)/A(N,N)
for k=N-1:-1:1
X(k,j)=(Y(k,j)-A(k,k+1:N)*X(k+1:N,j))/A(k,k)
end
end
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)