function test_solve_triangle
clc,clear
a=[ 4 5 6
0 2 3
0 0 1]
b=[ 7
1
4]
x=solvetriangle(a',b,'down')
xsystem=inv(a')*b
运行结果:
x =
1.7500
-3.8750
5.1250
xsystem =
1.7500
-3.8750
5.1250
function b=solvetriangle(a,b,matrixstyle,symbolmatrixjudge)
%求解上、下三角形矩阵的线性方程
%时间:2013/11/17 姓名:邓能财
if nargin<4, symbolmatrixjudge=false; end
if symbolmatrixjudge, a=sym(a);b=sym(b); end
[dim,dim2]=size(a);
%错误的矩阵:
assert( dim==dim2 && dim==length(b),...
'Argument input error: ',...
'Matrix dimensions must agree.')
switch lower(matrixstyle)
case 'up'
disp('上三角形矩阵的线性方程求解')
for col=dim:-1:1
b(col)=b(col)/a(col,col);
a(col,col)=1;
for row=col-1:-1:1
b(row)=-a(row,col)*b(col)+b(row);
a(row,col)=0;
end
end
b,a
case 'down'
disp('下三角形矩阵的线性方程求解')
for col=1:dim
b(col)=b(col)/a(col,col);
a(col,col)=1;
for row=col+1:dim
b(row)=-a(row,col)*b(col)+b(row);
a(row,col)=0;
end
end
b,a
otherwise
error('第三个参数只能为‘up’或‘down’')
end
end
clc,clear
%%%%%%%%%%%%%%%
%‘cousant’或‘doolittle’
a=sym([ 2 -1 4 -3 1
-1 1 2 1 3
4 2 3 3 -1
-3 1 3 2 4
1 3 1 4 4])
b=sym([ 11
14
4
16
18])
transformationstyle = 'cousant';
[l,u]=luresolution(a,transformationstyle)
运行结果:
l =
[ 2, 0, 0, 0, 0]
[ -1, 1/2, 0, 0, 0]
[ 4, 4, -37, 0, 0]
[ -3, -1/2, 13, 58/37, 0]
[ 1, 7/2, -29, -44/37, 54/29]
u =
[ 1, -1/2, 2, -3/2, 1/2]
[ 0, 1, 8, -1, 7]
[ 0, 0, 1, -13/37, 31/37]
[ 0, 0, 0, 1, -35/29]
[ 0, 0, 0, 0, 1]
function [l,u]=luresolution(a,resolutionstyle)
%时间:2013/11/17 姓名:邓能财
if nargin<2, resolutionstyle='cousant'; end
[dim,dim2]=size(a);
%错误的矩阵:
assert( dim==dim2,...
'Argument input error: ',...
'Matrix dimensions must agree.')
switch lower(resolutionstyle)
case 'cousant'
disp('cousant LU分解法(U含E)')
l=sym(zeros(dim));u=sym(eye(dim));
%1
l(:,1)=a(:,1);
u(1,2:end)=a(1,2:end)./l(1,1);
%2
for i=2:dim
for j=i:dim
l(j,i)=a(j,i) -l(j,1:i-1)*u(1:i-1,i);
end
for j=i+1:dim
u(i,j)=(a(i,j) -l(i,1:i-1)*u(1:i-1,j))/l(i,i);
end
end
% a,l_multi_u=l*u,index_not_a=find(a~=l_multi_u)
disp('cousant LU分解结束')
case 'doolittle'
disp('doolittle LU分解法(L含E)')
[l_,u_]=luresolution(a','cousant');
l=u_';
u=l_';
disp('doolittle LU分解结束')
otherwise
error('第三个参数只能为‘cousant’或‘doolittle’')
end
function test_lusolve
clc,clear
%%%%%%%%%%%%%%%
%‘cousant’或‘doolittle’
a=sym([ 2 -1 4 -3 1
-1 1 2 1 3
4 2 3 3 -1
-3 1 3 2 4
1 3 1 4 4])
b=sym([ 11
14
4
16
18])
% transformationstyle = 'cousant';
% [l,u]=luresolution(a,transformationstyle)
x=lusolve(a,b,'doolittle')
disp('系统解')
xsystem=inv(a)*b
运行结果:
doolittle LU分解法(L含E)
cousant LU分解法(U含E)
cousant LU分解结束
doolittle LU分解结束
由LU求解Ly=b
下三角形矩阵的线性方程求解
由LU求解Ux=y
上三角形矩阵的线性方程求解
x =
1/27
14/3
13/9
-62/27
79/27
系统解
xsystem =
1/27
14/3
13/9
-62/27
79/27
function x=lusolve(a,b,transformationstyle)
%时间:2013/11/17 姓名:邓能财
if nargin<3, transformationstyle='cousant'; end
[dim,dim2]=size(a);
%错误的矩阵:
assert( dim==dim2 && dim==length(b),...
'Argument input error: ',...
'Matrix dimensions must agree.')
%LU分解
[l,u]=luresolution(a,transformationstyle);
%求解两分解式
disp('由LU求解Ly=b')
y=solvetriangle(l,b,'down',true);
% y_system=inv(l)*b
disp('由LU求解Ux=y')
x=solvetriangle(u,y,'up',true);
% x_system=inv(u)*y
end
function test_gauss
clc,clear
a=[1.15 0.42 100.71
1.19 0.55 0.32
1.00 0.35 3.00]
b=[-198.70 2.29 -3.65]'
% x=gauss(a,b)
% ‘normalstyle’、‘rowmax’或‘allmax’
x=gauss(a,b,'rowmax')
xsystem=inv(a)*b
运行结果:
列主元消元法
%找到该列绝对值最大元
%若非子矩阵首行,则调换行
%化为行阶梯型
%找到该列绝对值最大元
%若非子矩阵首行,则调换行
%化为行阶梯型
%化为行最简型
x =
2.0000
1.0000
-2.0000
xsystem =
2.0000
1.0000
-2.0000
function b=gauss(a,b,transformationstyle)
%gauss消元法求解线性方程组
%时间:2013/11/17 姓名:邓能财
if nargin<3, transformationstyle='normalstyle'; end
[dim,dim2]=size(a);
%错误的矩阵:
assert( dim==dim2 && dim==length(b),...
['Argument input error: ',...
'Matrix dimensions must agree.'])
switch transformationstyle
case 'normalstyle'
disp('高斯消元法')
for col=1:dim-1
disp('%找到该列非零元')
for i=col:dim
if a(i,col)~=0, i; break; end
end
if i~=col
disp('%若非首行,则调换行')
tmp=a(col,[col:end]);
a(col,[col:end])=a(i,[col:end]);
a(i,[col:end])=tmp;
tmp=b(col);b(col)=b(i);b(i)=tmp;
end
% b,a
disp('%化为行阶梯型')
for row=col+1:dim
tmpk=-a(row,col)/a(col,col);
% tmpk=decimallimit(tmpk);
b(row)=tmpk*b(col)+b(row);
a(row,:)=tmpk*a(col,:)+a(row,:);
end
% b=decimallimit(b)
% a=decimallimit(a)
% b,a
end
disp('%化为行最简型')
for col=dim:-1:1
tmpk=1/a(col,col);
% tmpk=decimallimit(tmpk)
b(col)=tmpk*b(col);
% b=decimallimit(b)
a(col,col)=1;
for row=col-1:-1:1
tmpk=-a(row,col)/a(col,col);
% tmpk=decimallimit(tmpk);
b(row)=tmpk*b(col)+b(row);
a(row,col)=0;
end
% b=decimallimit(b)
% a=decimallimit(a)
% b,a
end
case 'rowmax'
disp('列主元消元法')
for col=1:dim-1
disp('%找到该列绝对值最大元')
[abs_max,rindex]=max(abs(a([col:end],col)));
rindex=rindex+(col-1);
if rindex~=col
disp('%若非子矩阵首行,则调换行')
tmp=a(col,[col:end]);
a(col,[col:end])=a(rindex,[col:end]);
a(rindex,[col:end])=tmp;
tmp=b(col);b(col)=b(rindex);b(rindex)=tmp;
end
% b,a
disp('%化为行阶梯型')
for row=col+1:dim
tmpk=-a(row,col)/a(col,col);
% tmpk=decimallimit(tmpk);
b(row)=tmpk*b(col)+b(row);
a(row,:)=tmpk*a(col,:)+a(row,:);
end
% b=decimallimit(b)
% a=decimallimit(a)
% b,a
end
disp('%化为行最简型')
for col=dim:-1:1
tmpk=1/a(col,col);
% tmpk=decimallimit(tmpk)
b(col)=tmpk*b(col);
% b=decimallimit(b)
a(col,col)=1;
for row=col-1:-1:1
tmpk=-a(row,col)/a(col,col);
% tmpk=decimallimit(tmpk);
b(row)=tmpk*b(col)+b(row);
a(row,col)=0;
end
% b=decimallimit(b)
% a=decimallimit(a)
% b,a
end
case 'allmax'
disp('全主元消元法')
otherwise
error('第三个参数只能为‘normalstyle’、‘rowmax’或‘allmax’')
end
end
clc,clear
ma=[1 2 0 0 5
2 3 5 0 0
0 1 2 1 0
0 0 5 3 7
11 0 0 9 1]
e=[3 5 7 9 11]';
[a,b,c]=matrix2CycleBand(ma)
% m=cycleBand2Matrix(a,b,c)
x=cycleBandMatrixSolve(a,b,c,e)
x_system=inv(ma)*e
运行结果:
ma =
1 2 0 0 5
2 3 5 0 0
0 1 2 1 0
0 0 5 3 7
11 0 0 9 1
a =
1 3 2 3 1
b =
2 5 1 7 11
c =
5 2 1 5 9
x =
-2.1822
3.3427
-0.1328
3.9228
-0.3007
x_system =
-2.1822
3.3427
-0.1328
3.9228
-0.3007
function x=cycleBandMatrixSolve(a,b,c,e)
%【周期带状矩阵】方程组求解之待定系数法
%矩阵A由中b、a、c(dim维),构成
%等式右边的向量为:e
%算法:
%1 待定系数法:x1和x2为待定系数
% k(常数,x1系数,x2系数)
%2 经过迭代后,得到关于x1、x2的二阶方程
%3 解2中方程,得x1、x2; 代入x3,x4,...,xdim中求之
%时间:2013/11/17 姓名:邓能财
dim=length(a);
assert( dim==length(b)&&dim==length(c)&&dim==length(e),...
['Argument input error: ',...
'Matrix dimensions must agree.'])
k=zeros(dim,3);
k(1,:)=[0 1 0];
k(2,:)=[0 0 1];
for i=3:dim
k(i,:)=(1/b(i-1))*(e(i-1)*[1 0 0]-c(i-1)*k(i-2,:)-a(i-1)*k(i-1,:));
end
k(1,:)=(1/b(dim))*(e(dim)*[1 0 0]-c(dim)*k(dim-1,:)-a(dim)*k(dim,:));
k(2,:)=(1/b(1))*(e(1)*[1 0 0]-c(1)*k(dim,:)-a(1)*k(1,:));
% k
x=zeros(dim,1);
little_mat=[k(1,2)-1 k(1,3)
k(2,2) k(2,3)-1];
little_b= [-k(1,1) -k(2,1)]';
x(1:2)=inv(little_mat)*little_b;
%返迭代
for i=3:dim
x(i)=k(i,:)*[1 x(1) x(2)]';
end
end
function [a,b,c]=matrix2CycleBand(m)
%普通矩阵转化为带状矩阵
%时间:2013/11/17 姓名:邓能财
[dim,dim2]=size(m);
assert( dim==dim2,...
['Argument input error: ',...
'Matrix dimensions must agree.'])
a=zeros(1,dim); b=a; c=a;
for i=1:dim-1
a(i)=m(i,i);
b(i)=m(i,i+1);
c(i+1)=m(i+1,i);
end
a(dim)=m(dim,dim);
b(dim)=m(dim,1);
c(1)=m(1,dim);
end
function m=cycleBand2Matrix(a,b,c)
%带状矩阵转化为普通矩阵
%时间:2013/11/17 姓名:邓能财
dim=length(a);
assert( dim==length(b) && dim==length(c),...
['Argument input error: ',...
'Matrix dimensions must agree.'])
m=zeros(dim);
for i=1:dim-1
m(i,i)=a(i);
m(i,i+1)=b(i);
m(i+1,i)=c(i+1);
end
m(dim,dim)=a(dim);
m(dim,1)=b(dim);
m(1,dim)=c(1);
end
clc,clear
ma=[1 2 0 0 0
5 3 5 0 0
0 11 2 1 0
0 0 5 3 7
0 0 0 9 1]
e=[3 5 7 9 11]';
% x=catchSolve(a,b)
[a,b,c]=matrix2Band(ma)
x=bandMatrixSolve(a,b,c,e)
xsystem=inv(ma)*e
运行结果:
ma =
1 2 0 0 0
5 3 5 0 0
0 11 2 1 0
0 0 5 3 7
0 0 0 9 1
a =
1 3 2 3 1
b =
2 5 1 7
c =
5 11 5 9
x =
1.5581 0.7210 -0.9907 1.0508 1.5430
xsystem =
1.5581
0.7210
-0.9907
1.0508
1.5430
function x=bandMatrixSolve(a,b,c,e)
%【程序特点】:考虑将内存和运算量同时达到极小化
%带状矩阵方程组求解
%矩阵A由中a(dim维)、b、c,构成
%等式右边的向量为:e
%算法:追赶法
%时间:2013/11/17 姓名:邓能财
dim=length(a);
assert( dim==length(b)+1 &&dim==length(c)+1 &&dim==length(e),...
['Argument input error: ',...
'Matrix dimensions must agree.'])
%%%%%%%%%%%%%%%%%%%LU分解
alfa=zeros(1,dim); beta=zeros(1,dim-1);
%初始值
alfa(1)=a(1);
beta(1)=b(1)/alfa(1);
for i=2:dim-1
alfa(i)=a(i)-c(i-1)*beta(i-1);
beta(i)=b(i)/alfa(i);
end
i=i+1;
alfa(i)=a(i)-c(i-1)*beta(i-1);
a=[]; b=[];%释放内存
%%%%%%%%%%%%%%%%%%%解Ly=e
y=zeros(1,dim);
%初始值
y(1)=e(1)/alfa(1);
for i=2:dim
y(i)=(e(i)-c(i-1)*y(i-1))/alfa(i);
end
alfa=[]; c=[];%释放内存
%%%%%%%%%%%%%%%%%%%解Ux=y
x=zeros(1,dim);
%初始值
x(dim)=y(dim);
for i=dim-1:-1:1
x(i)=y(i)-beta(i)*x(i+1);
end
beta=[]; y=[];%释放内存
end
end