前言
MATLAB是一个功能强大的软件,我们可以利用MATLAB进行绘图、算法验证、仿真实验等等。在学习MATLAB的过程中,繁多的命令与代码往往容易使我们脑容量过载
本系列将总结一些常见的MATLAB编程小技巧
可能有些地方会有些错误或者是不太完善的,还请大家在评论区直接指出❤️❤️❤️
系列文章
【MATLAB100个实用小技巧】——图形应用(1-10)
【MATLAB100个实用小技巧】——图形应用(11-20)
【MATLAB100个实用小技巧】——图形应用(21-32)
【MATLAB100个实用小技巧】——界面设计(33-43)
【MATLAB100个实用小技巧】——界面设计(44-55)
【MATLAB100个实用小技巧】——界面设计(56-66)
【MATLAB100个实用小技巧】——图形处理(67-75)
【MATLAB100个实用小技巧】——图形处理(76-84)
【MATLAB100个实用小技巧】——数值分析(85-100)
代码
X=input('请输入横坐标向量X:\nX='); %输入的数据为一维数组,例如:[1,3,4,5](下同);
Y=input('请输入纵坐标向量Y:\nY=');
m = length(X);
L = ones(m,m);
for k = 1 : m
V = 1;
for i = 1 : m
if k ~= i
V = conv(V,poly(X(i))) / (X(k) - X(i));
end
end
l(k, :) = poly2sym(V);
end
fprintf('基函数为:\n');
for k=1:m
fprintf('q%d(x)=%s\n',k,l(k));
end
L = Y * l;
fprintf('拉格朗日多项式为:\nP(x)=%s\n',L);
重点 spline
代码
x = [0 1 2.5 3.6 5 7 8.1 10];
y = sin(x);
xx = 0:.25:10;
yy = spline(x,y,xx);
plot(x,y,'o',xx,yy)
效果
参考博客
代码
function [A,y]= newtonzi(X,Y,x)
% Newton插值函数
% X为已知数据点的x坐标
% Y为已知数据点的y坐标
% x为插值点的x坐标
% 函数返回A差商表
% y为各插值点函数值
n=length(X); m=length(x);
for t=1:m
z=x(t); A=zeros(n,n);A(:,1)=Y';
s=0.0; y=0.0; c1=1.0;
for j=2:n
for i=j:n
A(i,j)=(A(i,j-1)- A(i-1,j-1))/(X(i)-X(i-j+1));
end
end
C=A(n,n);
for k=1:n
p=1.0;
for j=1:k-1
p=p*(z-X(j));
end
s=s+A(k,k)*p;
end
ss(t)=s;
end
y=ss;
A=[X',A];
end
参考博客
代码
%插值算法 (常用)
%Hermite(埃尔米特)插值法
a=0;
a=input('请输入数据矩阵的行数:');
b=0;
b=input('请输入数据矩阵的列数:');
%初始化目标矩阵
c=zeros(a,b);
c=input('请依次输入数据矩阵:');
disp('数据矩阵:');
disp(c);
%确定插值区间
d=0;
d=input('请输入插值区间:');
%进行插值
e(1,:)=d;
[n,m]=size(c);
for i=2:n
e(i,:)=pchip(c(1,:),c(i,:),d);
end
%目标矩阵
disp('Hermite插值后的矩阵:');
disp(e);
效果
参考博客
代码
h0=figure('toolbar','none',...
'position',[200 150 450 250]);
h1=axes('parent',h0,...
'position',[0.05 0.15 0.65 0.6],...
'visible','off');
I=imread('abmatrix.bmp','bmp');
image(I)
axis off
huidiao=[...
'a=[1 0 3 0;0 2 1 2;3 1 15 0;0 2 0 4;];,',...
'b=[1 6 5 8]'';,',...
'r=[a,b];,',...
'n=4;,',...
'tic,',...
'x=ch(a,b,n);,',...
'time1=toc;,',...
'T=num2str(time1);,',...
'set(e1,''string'',[T,''秒'']);,',...
'msgbox([''X=['',num2str(x(1)),num2str(x(2)),num2str(x(3)),num2str(x(4)),'']''],''方程组的解'');'];
t1=uicontrol('parent',h0,...
'units','points',...
'tag','t1',...
'style','text',...
'string','方程组如下:',...
'fontsize',15,...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[20 150 100 20]);
e1=uicontrol('parent',h0,...
'units','points',...
'tag','e1',...
'style','edit',...
'horizontalalignment','right',...
'backgroundcolor',[1 1 1],...
'position',[290 100 30 20]);
t2=uicontrol('parent',h0,...
'units','points',...
'tag','t2',...
'style','text',...
'string','计算时间:',...
'fontsize',15,...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[250 125 80 20]);
b1=uicontrol('parent',h0,...
'units','points',...
'tag','b1',...
'style','pushbutton',...
'string','平方根法',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[250 60 60 20],...
'callback',huidiao);
b2=uicontrol('parent',h0,...
'units','points',...
'tag','b2',...
'string','关闭',...
'style','pushbutton',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[250 30 60 20],...
'callback','close');
效果
代码
h0=figure('toolbar','none',...
'position',[200 150 450 250],...
'name','实例 91');
h1=axes('parent',h0,...
'position',[0.05 0.15 0.65 0.6],...
'visible','off');
I=imread('abmatrix.bmp','bmp');
image(I)
axis off
huidiao=[...
'a=[1 2 4 1 7;2 3 0 1 8;4 1 7 6 1;1 1 0 2 1;1 3 0 1 1;];,',...
'b=[15 14 19 5 6]'';,',...
'r=[a,b];,',...
'n=5;,',...
'tic,',...
'x=gauss(r,n);,',...
'time1=toc;,',...
'T=num2str(time1);,',...
'set(e1,''string'',[T,''秒'']);,',...
'msgbox([''X=['',num2str(x(1)),num2str(x(2)),num2str(x(3)),num2str(x(4)),num2str(x(5)),'']''],''方程组的解'');']; t1=uicontrol('parent',h0,...
'units','points',...
'tag','t1',...
'style','text',...
'string','方程组如下:',...
'fontsize',15,...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[20 150 100 20]);
e1=uicontrol('parent',h0,...
'units','points',...
'tag','e1',...
'style','edit',...
'horizontalalignment','right',...
'backgroundcolor',[1 1 1],...
'position',[290 100 30 20]);
t2=uicontrol('parent',h0,...
'units','points',...
'tag','t2',...
'style','text',...
'string','计算时间:',...
'fontsize',15,...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[250 125 80 20]);
b1=uicontrol('parent',h0,...
'units','points',...
'tag','b1',...
'style','pushbutton',...
'string','GS 消去法',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[250 60 60 20],...
'callback',huidiao);
b2=uicontrol('parent',h0,...
'units','points',...
'tag','b2',...
'string','关闭',...
'style','pushbutton',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[250 30 60 20],...
'callback','close');
效果
代码
h0=figure('toolbar','none',...
'position',[200 150 450 250],...
'name','实例 92');
h1=axes('parent',h0,... 'position',[0.05 0.15 0.65 0.6],...
'visible','off'); I=imread('abmatrix.bmp','bmp'); image(I)
axis off huidiao=[...
'a=[1 2 4 1 7;2 3 0 1 8;4 1 7 6 1;1 1 0 2 1;1 3 0 1 1;];,',...
'b=[15 14 19 5 6]'';,',... 'n=5;,',...
'tic,',... 'x=dirang(a,b,n);,',... 'time1=toc;,',...
'T=num2str(time1);,',... 'set(e1,''string'',[T,''秒'']);,',...
'msgbox([''X =
['',num2str(x(1)),num2str(x(2)),num2str(x(3)),num2str(x(4)),num2str(x(5)),'']''],''方程组的解'');']; t1=uicontrol('parent',h0,...
'units','points',...
'tag','t1',...
'style','text',...
'string','方程组如下:',...
'fontsize',15,... 'backgroundcolor',[0.75 0.75 0.75],...
'position',[20 150 100 20]); e1=uicontrol('parent',h0,...
'units','points',...
'tag','e1',...
'style','edit',... 'horizontalalignment','right',... 'backgroundcolor',[1 1 1],...
'position',[270 100 50 20]); t2=uicontrol('parent',h0,...
'units','points',...
'tag','t2',...
'style','text',...
'string','计算时间:',...
'fontsize',15,... 'backgroundcolor',[0.75 0.75 0.75],...
'position',[250 125 80 20]); b1=uicontrol('parent',h0,...
'units','points',...
'tag','b1',...
'style','pushbutton',...
'string','三角分解法',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[250 60 60 20],... 'callback',huidiao);
b2=uicontrol('parent',h0,... 'units','points',...
'tag','b2',...
'string','关闭',...
'style','pushbutton',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[250 30 60 20],...
'callback','close');
效果
h0=figure('toolbar','none',...
'position',[200 150 450 250],...
'name','实例 93');
h1=axes('parent',h0,... 'position',[0.05 0.15 0.65 0.6],...
'visible','off'); I=imread('abmatrix.bmp','bmp'); image(I)
axis off huidiao=[...
'a=[1 0 3 0;0 2 1 2;3 1 15 0;0 2 0 4;];,',...
'b=[1 6 5 8]'';,',... 'n=4;,',...
'u=zeros(n,1);,',...
'tic,',... '[x,k]=jac(a,b,n,u);,',... 'time1=toc;,',...
'T=num2str(time1);,',... 'set(e1,''string'',[T,''秒'']);,',...
'set(e2,''string'',num2str(k));,',...
'msgbox([''X = ['',num2str(x(1)),'' '',num2str(x(2)),''
'',num2str(x(3)),'','',num2str(x(4)),'']''],''方程组的解'');']; t1=uicontrol('parent',h0,...
'units','points',...
'tag','t1',...
'style','text',...
'string','方程组如下:',...
'fontsize',15,... 'backgroundcolor',[0.75 0.75 0.75],...
'position',[20 150 100 20]); e1=uicontrol('parent',h0,...
'units','points',...
'tag','e1',...
'style','edit',... 'horizontalalignment','right',... 'backgroundcolor',[1 1 1],...
'position',[295 130 35 20]); t2=uicontrol('parent',h0,...
'units','points',...
'tag','t2',...
'style','text',...
'string','计算时间:',...
'fontsize',10,... 'backgroundcolor',[0.75 0.75 0.75],...
'position',[240 130 50 20]); e2=uicontrol('parent',h0,...
'units','points',...
'tag','e2',...
'style','edit',... 'horizontalalignment','right',... 'backgroundcolor',[1 1 1],...
'position',[295 100 35 20]); t2=uicontrol('parent',h0,...
'units','points',...
'tag','t2',...
'style','text',...
'string','迭代步数:',...
'fontsize',10,...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[240 100 50 20]); b1=uicontrol('parent',h0,...
'units','points',...
'tag','b1',...
'style','pushbutton',...
'string','Jacobi 迭代法',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[250 60 60 20],... 'callback',huidiao);
b2=uicontrol('parent',h0,... 'units','points',...
'tag','b2',...
'string','关闭',...
'style','pushbutton',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[250 30 60 20],...
'callback','close');
h0=figure('toolbar','none',...
'position',[200 150 450 250],...
'name','实例 94');
h1=axes('parent',h0,... 'position',[0.05 0.15 0.65 0.6],...
'visible','off'); I=imread('abmatrix.bmp','bmp'); image(I)
axis off huidiao=[...
'a=[1 0 3 0;0 2 1 2;3 1 15 0;0 2 0 4;];,',...
'b=[1 6 5 8]'';,',... 'n=4;,',...
'u=zeros(n,1);,',... 'tic,',... '[x,k]=gs(a,b,n,u);,',... 'time1=toc;,',...
'T=num2str(time1);,',... 'set(e1,''string'',[T,''秒'']);,',...
'set(e2,''string'',num2str(k));,',...
'msgbox([''X = ['',num2str(x(1)),'' '',num2str(x(2)),''
'',num2str(x(3)),'','',num2str(x(4)),'']''],''方程组的解'');']; t1=uicontrol('parent',h0,...
'units','points',...
'tag','t1',...
'style','text',...
'string','方程组如下:',...
'fontsize',15,... 'backgroundcolor',[0.75 0.75 0.75],...
'position',[20 150 100 20]); e1=uicontrol('parent',h0,...
'units','points',...
'tag','e1',...
'style','edit',... 'horizontalalignment','right',... 'backgroundcolor',[1 1 1],...
'position',[295 130 35 20]); t2=uicontrol('parent',h0,...
'units','points',...
'tag','t2',...
'style','text',...
'string','计算时间:',...
'fontsize',10,... 'backgroundcolor',[0.75 0.75 0.75],...
'position',[240 130 50 20]); e2=uicontrol('parent',h0,...
'units','points',...
'tag','e2',...
'style','edit',... 'horizontalalignment','right',... 'backgroundcolor',[1 1 1],...
'position',[295 100 35 20]); t2=uicontrol('parent',h0,...
'units','points',...
'tag','t2',...
'style','text',...
'string','迭代步数:',...
'fontsize',10,... 'backgroundcolor',[0.75 0.75 0.75],...
'position',[240 100 50 20]); b1=uicontrol('parent',h0,...
'units','points',...
'tag','b1',...
'style','pushbutton',...
'string','GS 迭代法',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[250 60 60 20],... 'callback',huidiao);
b2=uicontrol('parent',h0,... 'units','points',...
'tag','b2',...
'string','关闭',...
'style','pushbutton',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[250 30 60 20],...
'callback','close');
h0=figure('toolbar','none',...
'position',[200 150 450 250],...
'name','实例 95');
h1=axes('parent',h0,... 'position',[0.05 0.15 0.65 0.6],...
'visible','off'); I=imread('abmatrix.bmp','bmp'); image(I)
axis off huidiao=[...
'a=[1 0 3 0;0 2 1 2;3 1 15 0;0 2 0 4;];,',...
'b=[1 6 5 8]'';,',... 'n=4;,',...
'u=zeros(n,1);,',... 'tic,',... '[x,k]=sor(a,b,n,u);,',... 'time1=toc;,',...
'T=num2str(time1);,',... 'set(e1,''string'',[T,''秒'']);,',...
'set(e2,''string'',num2str(k));,',...
'msgbox([''X = ['',num2str(x(1)),'' '',num2str(x(2)),''
'',num2str(x(3)),'','',num2str(x(4)),'']''],''方程组的解'');']; t1=uicontrol('parent',h0,...
'units','points',...
'tag','t1',...
'style','text',...
'string','方程组如下:',...
'fontsize',15,... 'backgroundcolor',[0.75 0.75 0.75],...
'position',[20 150 100 20]); e1=uicontrol('parent',h0,...
'units','points',...
'tag','e1',...
'style','edit',...
'horizontalalignment','right',... 'backgroundcolor',[1 1 1],...
'position',[295 130 35 20]); t2=uicontrol('parent',h0,...
'units','points',...
'tag','t2',...
'style','text',...
'string','计算时间:',...
'fontsize',10,... 'backgroundcolor',[0.75 0.75 0.75],...
'position',[240 130 50 20]); e2=uicontrol('parent',h0,...
'units','points',...
'tag','e2',...
'style','edit',... 'horizontalalignment','right',... 'backgroundcolor',[1 1 1],...
'position',[295 100 35 20]); t2=uicontrol('parent',h0,...
'units','points',...
'tag','t2',...
'style','text',...
'string','迭代步数:',...
'fontsize',10,... 'backgroundcolor',[0.75 0.75 0.75],...
'position',[240 100 50 20]); b1=uicontrol('parent',h0,...
'units','points',...
'tag','b1',...
'style','pushbutton',...
'string','SOR 迭代法',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[250 60 60 20],... 'callback',huidiao);
b2=uicontrol('parent',h0,... 'units','points',...
'tag','b2',...
'string','关闭',...
'style','pushbutton',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[250 30 60 20],...
'callback','close');
h0=figure('toolbar','none',...
'position',[200 150 450 250],...
'name','实例 96');
h1=axes('parent',h0,... 'position',[0.05 0.15 0.65 0.6],...
'visible','off'); I=imread('abmatrix.bmp','bmp'); image(I)
axis off huidiao=[...
'a=[1 0 3 0;0 2 1 2;3 1 15 0;0 2 0 4;];,',...
'b=[1 6 5 8]'';,',... 'n=4;,',...
'u=zeros(n,1);,',... 'tic,',... '[x,k]=cg(a,b,n,u);,',... 'time1=toc;,',...
'T=num2str(time1);,',... 'set(e1,''string'',[T,''秒'']);,',...
'set(e2,''string'',num2str(k));,',...
'msgbox([''X = ['',num2str(x(1)),'' '',num2str(x(2)),'' '',num2str(x(3)),'','', num2str(x(4)),'']''],''方程组的解'');'];
t1=uicontrol('parent',h0,... 'units','points',...
'tag','t1',...
'style','text',...
'string','方程组如下:',...
'fontsize',15,... 'backgroundcolor',[0.75 0.75 0.75],...
'position',[20 150 100 20]); e1=uicontrol('parent',h0,...
'units','points',...
'tag','e1',...
'style','edit',... 'horizontalalignment','right',... 'backgroundcolor',[1 1 1],...
'position',[295 130 35 20]); t2=uicontrol('parent',h0,...
'units','points',...
'tag','t2',...
'style','text',...
'string','计算时间:',...
'fontsize',10,... 'backgroundcolor',[0.75 0.75 0.75],...
'position',[240 130 50 20]); e2=uicontrol('parent',h0,...
'units','points',...
'tag','e2',...
'style','edit',... 'horizontalalignment','right',... 'backgroundcolor',[1 1 1],...
'position',[295 100 35 20]); t2=uicontrol('parent',h0,...
'units','points',...
'tag','t2',...
'style','text',...
'string','迭代步数:',...
'fontsize',10,... 'backgroundcolor',[0.75 0.75 0.75],...
'position',[240 100 50 20]); b1=uicontrol('parent',h0,...
'units','points',...
'tag','b1',...
'style','pushbutton',...
'string','最速下降法',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[250 60 60 20],... 'callback',huidiao);
b2=uicontrol('parent',h0,... 'units','points',...
'tag','b2',...
'string','关闭',...
'style','pushbutton',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[250 30 60 20],...
'callback','close');
h0=figure('toolbar','none',...
'position',[200 150 450 250],...
'name','实例 97');
h1=axes('parent',h0,... 'position',[0.05 0.15 0.65 0.6],...
'visible','off'); I=imread('abmatrix.bmp','bmp'); image(I)
axis off huidiao=[...
'a=[1 0 3 0;0 2 1 2;3 1 15 0;0 2 0 4;];,',...
'b=[1 6 5 8]'';,',... 'n=4;,',...
'u=zeros(n,1);,',... 'tic,',... '[x,k]=getd(a,b,n,u);,',... 'time1=toc;,',...
'T=num2str(time1);,',... 'set(e1,''string'',[T,''秒'']);,',...
'set(e2,''string'',num2str(k));,',...
'msgbox([''X = ['',num2str(x(1)),'' '',num2str(x(2)),''
'',num2str(x(3)),'','',num2str(x(4)),'']''],''方程组的解'');']; t1=uicontrol('parent',h0,...
'units','points',...
'tag','t1',...
'style','text',...
'string','方程组如下:',...
'fontsize',15,... 'backgroundcolor',[0.75 0.75 0.75],...
'position',[20 150 100 20]); e1=uicontrol('parent',h0,...
'units','points',...
'tag','e1',...
'style','edit',... 'horizontalalignment','right',... 'backgroundcolor',[1 1 1],...
'position',[295 130 35 20]); t2=uicontrol('parent',h0,...
'units','points',...
'tag','t2',...
'style','text',...
'string','计算时间:',...
'fontsize',10,... 'backgroundcolor',[0.75 0.75 0.75],...
'position',[240 130 50 20]); e2=uicontrol('parent',h0,...
'units','points',...
'tag','e2',...
'style','edit',... 'horizontalalignment','right',... 'backgroundcolor',[1 1 1],...
'position',[295 100 35 20]);
t2=uicontrol('parent',h0,... 'units','points',...
'tag','t2',...
'style','text',...
'string','迭代步数:',...
'fontsize',10,... 'backgroundcolor',[0.75 0.75 0.75],...
'position',[240 100 50 20]); b1=uicontrol('parent',h0,...
'units','points',...
'tag','b1',...
'style','pushbutton',...
'string','共轭梯度法',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[250 60 60 20],... 'callback',huidiao);
b2=uicontrol('parent',h0,... 'units','points',...
'tag','b2',...
'string','关闭',...
'style','pushbutton',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[250 30 60 20],...
'callback','close');
h0=figure('toolbar','none',...
'position',[200 150 450 250],...
'name','实例 98');
h1=axes('parent',h0,... 'position',[0.05 0.15 0.65 0.6],...
'visible','off'); I=imread('fabmatrix.bmp','bmp'); image(I)
axis off huidiao=[...
'n=3;,',...
'u=zeros(n,1);,',... 'tic,',... '[x,k]=nnewton(u,n);,',... 'time1=toc;,',...
'T=num2str(time1);,',... 'set(e1,''string'',[T,''秒'']);,',...
'set(e2,''string'',num2str(k));,',...
'');'];
'msgbox([''X=['',num2str(x(1)),'' '',num2str(x(2)),'' '',num2str(x(3)),'']''],'' 方程组的解
t1=uicontrol('parent',h0,... 'units','points',...
'tag','t1',...
'style','text',...
'string','非线性方程组如下:',...
'fontsize',15,... 'backgroundcolor',[0.75 0.75 0.75],...
'position',[20 150 150 20]); e1=uicontrol('parent',h0,...
'units','points',...
'tag','e1',...
'style','edit',... 'horizontalalignment','right',... 'backgroundcolor',[1 1 1],...
'position',[295 130 35 20]); t2=uicontrol('parent',h0,...
'units','points',...
'tag','t2',...
'style','text',...
'string','计算时间:',...
'fontsize',10,... 'backgroundcolor',[0.75 0.75 0.75],...
'position',[240 130 50 20]); e2=uicontrol('parent',h0,...
'units','points',...
'tag','e2',...
'style','edit',... 'horizontalalignment','right',... 'backgroundcolor',[1 1 1],...
'position',[295 100 35 20]); t2=uicontrol('parent',h0,...
'units','points',...
'tag','t2',...
'style','text',...
'string','迭代步数:',...
'fontsize',10,... 'backgroundcolor',[0.75 0.75 0.75],...
'position',[240 100 50 20]); b1=uicontrol('parent',h0,...
'units','points',...
'tag','b1',...
'style','pushbutton',...
'string','Newton 迭代法',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[250 60 60 20],... 'callback',huidiao);
b2=uicontrol('parent',h0,... 'units','points',...
'tag','b2',...
'string','关闭',...
'style','pushbutton',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[250 30 60 20],...
'callback','close');
h0=figure('toolbar','none',...
'position',[200 150 450 250],...
'name','实例 99');
h1=axes('parent',h0,... 'position',[0.05 0.15 0.65 0.6],...
'visible','off'); I=imread('fabmatrix.bmp','bmp'); image(I)
axis off huidiao=[...
'n=3;,',...
'u=zeros(n,1);,',... 'tic,',... '[x,k]=broyden(u,n);,',... 'time1=toc;,',...
'T=num2str(time1);,',... 'set(e1,''string'',[T,''秒'']);,',...
'set(e2,''string'',num2str(k));,',...
'msgbox([''X=['',num2str(x(1)),'' '',num2str(x(2)),'' '',num2str(x(3)),'']''],'' 方程组的解
'');'];
t1=uicontrol('parent',h0,... 'units','points',...
'tag','t1',...
'style','text',...
'string','非线性方程组如下:',...
'fontsize',15,... 'backgroundcolor',[0.75 0.75 0.75],...
'position',[20 150 150 20]); e1=uicontrol('parent',h0,...
'units','points',...
'tag','e1',...
'style','edit',... 'horizontalalignment','right',... 'backgroundcolor',[1 1 1],...
'position',[295 130 35 20]); t2=uicontrol('parent',h0,...
'units','points',...
'tag','t2',...
'style','text',...
'string','计算时间:',...
'fontsize',10,... 'backgroundcolor',[0.75 0.75 0.75],...
'position',[240 130 50 20]); e2=uicontrol('parent',h0,...
'units','points',...
'tag','e2',...
'style','edit',... 'horizontalalignment','right',... 'backgroundcolor',[1 1 1],...
'position',[295 100 35 20]); t2=uicontrol('parent',h0,...
'units','points',...
'tag','t2',...
'style','text',...
'string','迭代步数:',...
'fontsize',10,... 'backgroundcolor',[0.75 0.75 0.75],...
'position',[240 100 50 20]); b1=uicontrol('parent',h0,...
'units','points',...
'tag','b1',...
'style','pushbutton',...
'string','Broyden 迭代法',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[250 60 60 20],... 'callback',huidiao);
b2=uicontrol('parent',h0,... 'units','points',...
'tag','b2',...
'string','关闭',...
'style','pushbutton',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[250 30 60 20],...
'callback','close');
h0=figure('toolbar','none',...
'position',[200 150 450 250],...
'name','实例 100');
h1=axes('parent',h0,... 'position',[0.05 0.15 0.65 0.6],...
'visible','off'); I=imread('fabmatrix.bmp','bmp'); image(I)
axis off huidiao=[...
'n=3;,',...
'u=zeros(n,1);,',... 'tic,',... '[x,k]=fbroyden(u,n);,',... 'time1=toc;,',...
'T=num2str(time1);,',... 'set(e1,''string'',[T,''秒'']);,',...
'set(e2,''string'',num2str(k));,',...
'msgbox([''X=['',num2str(x(1)),'' '',num2str(x(2)),'' '',num2str(x(3)),'']''],'' 方程组的解
'');'];
t1=uicontrol('parent',h0,... 'units','points',...
'tag','t1',...
'style','text',...
'string','非线性方程组如下:',...
'fontsize',15,... 'backgroundcolor',[0.75 0.75 0.75],...
'position',[20 150 150 20]); e1=uicontrol('parent',h0,...
'units','points',...
'tag','e1',...
'style','edit',... 'horizontalalignment','right',... 'backgroundcolor',[1 1 1],...
'position',[295 130 35 20]); t2=uicontrol('parent',h0,...
'units','points',...
'tag','t2',...
'style','text',...
'string','计算时间:',...
'fontsize',10,... 'backgroundcolor',[0.75 0.75 0.75],...
'position',[240 130 50 20]); e2=uicontrol('parent',h0,...
'units','points',...
'tag','e2',...
'style','edit',... 'horizontalalignment','right',... 'backgroundcolor',[1 1 1],...
'position',[295 100 35 20]); t2=uicontrol('parent',h0,...
'units','points',...
'tag','t2',...
'style','text',...
'string','迭代步数:',...
'fontsize',10,... 'backgroundcolor',[0.75 0.75 0.75],...
'position',[240 100 50 20]); b1=uicontrol('parent',h0,...
'units','points',...
'tag','b1',...
'style','pushbutton',...
'string','逆 Broyden 迭代法',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[250 60 70 20],... 'callback',huidiao);
b2=uicontrol('parent',h0,... 'units','points',...
'tag','b2',...
'string','关闭',...
'style','pushbutton',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[250 30 70 20],...
'callback','close');
XOR
function [x,st]=sor(a,b,n,x1) D=zeros(n,n);
L=zeros(n,n);
U=zeros(n,n); for i=1:n
for j=1:n
if j==i
D(i,j)=a(i,j);
end if j<i
end
end
end if j>i
end
L(i,j)=-a(i,j);
U(i,j)=-a(i,j);
opt=oumiga(a);
Bsor=inv(D-opt*L)*[(1-opt)*D+opt*U]; fsor=opt*inv(D-opt*L)*b;
k=1;
x2=Bsor*x1+fsor; e=x2-x1;
while norm(e,2)>1e-6 k=k+1;
x1=x2; x2=Bsor*x1+fsor; e=x2-x1;
end x=x2; st=k;