MATLAB简单程序大全
matlab经典源程序带有注释(详细经典)

2.1set 与get 函数 (1)2.2callback函数 (2)2.3元胞数组 (4)2.4结构数组 (6)2.5矩阵操作 (9)2.6字符串操作 (13)2.7判断函数使用大全 (16)2.11打开外部程序 (21)2.11程序运行时间 (22)2.14动画 (23)2.12动画 (24)2.23显示多行内容 (26)2.24 uitable 使用 (26)2.27鼠标操作 (27)2.28键盘操作 (27)2.32粘贴板 (28)2.1set 与get 函数set(edit_handle,'String','my value!'); %String为Edit控件的属性%%%2.1-1%创建figure对象hfig=figure(1);%创建坐标轴对象,指定其父对象为figure 1haxes1=axes('parent',hfig);prop.Color='b';prop.FontSize=12;set(haxes1,prop);%%%2.1-2hfig=figure(1);%查询其Units属性值get(hfig,'units')%其Units属性值为pixels(像素)% ans=% pixels%%%2.1-3%figure的Pointer属性标识了鼠标指针的形状set(gcf,'pointer');% 返回值为:[ crosshair | fullcrosshair | {arrow} | ibeam | watch | topl | topr | botl | botr | left | top | right | bottom | circle | cross | fleur | custom | hand ]%%%2.1-4%首先取得标识电脑屏幕大小的度量单位get(0,'units')% ans =% pixels%取得屏幕的尺寸get(0,'screensize')% ans =% 1 1 1280 8002.2callback函数%定义M文件的主函数名称为DefineCallback,不带输入和输出参数function DefineCallbackhFig= figure('units','normalize',...'position',[0.4 0.4 0.3 0.2]);%在窗口中创建按钮控件,并定义其Callback属性uicontrol('parent',hFig,...'style','pushbutton',...'String','Execute Callback',...'units','normalize',...'position',[0.4 0.4 0.3 0.2],...'callback',['figure;',...'x = 0:pi/20:2*pi;',...'y = sin(x);',...'plot(x,y);']);%定义M文件的主函数名称为DefineCallback,不带输入和输出参数function DefineCallback%创建界面窗口hFig= figure('units','normalize',...'position',[0.4 0.4 0.3 0.2]);%在窗口中创建按钮控件hpush=uicontrol('parent',hFig,...'style','pushbutton',...'String','Execute Callback',...'units','normalize',...'position',[0.4 0.4 0.3 0.2]);%设置按钮的Callback属性set(hpush,'callback',@mycallback);%定义回调函数为子函数function mycallback(hobj,event)figure;x = 0:pi/20:2*pi;y = sin(x);plot(x,y);2.3元胞数组a={'hello' [1 2 3;4 5 6];1 {'1''2'}}a ='hello' [2x3 double][ 1] {1x2 cell }%示例2:将元胞数组a中的元胞逐一赋值>> a{1,1}='hello';a{1,2}=[1 2 3;4 5 6];a{2,1}=1;a{2,2}={'1' '2'};>> aa ='hello' [2x3 double][ 1] {1x2 cell }%示例3:使用cell函数来创建元胞数组%生成2x3的元素为空数组的元胞数组>> a=cell(2,3)a =[] [] [][] [] []%示例4:判断数组A是否为元胞数组%定义一个元胞数组A>> A={1 2 3};%判断A是否为元胞数组,如果为元胞数组,则函数>> tf = iscell(A)tf =1%示例5:显示元胞数组C中的内容>> clear>> C={'Smith' [1 2;3 4] [12]};%直接显示元胞数组C中的内容>> celldisp(C)C{1} =SmithC{2} =1 23 4C{3} =12%显示元胞数组C中的内容,数组的名称用cellcontent代替>> celldisp(C,'cellcontent')cellcontent{1} =Smithcellcontent{2} =1 23 4cellcontent{3} =12%示例6:将字符数组转换为元胞数组>> S = ['abc '; 'defg'; 'hi m'];>> cellstr(S)ans ='abc'%原先abc后面的空格被清除'defg''hi m'%i和m之间的空格仍然保留%示例7:显示元胞数组S中的内容(包括空格和字符)>> S = {'abc ', 'defg','hi m'};>> cellplot(S)%示例8:将数字数组A按行或按列转换为元胞数组%A是4x3的数组>> A=[1 2 3;4 5 6;7 8 9;10 11 12];%把A的每一列转换为一个元胞,得到的C是1×3的元胞数组>> C=num2cell(A,1)C =[4x1 double] [4x1 double] [4x1 double] %把A的每一行转换为一个元胞,得到的C是4×1的元胞数组>> C=num2cell(A,2)C =[1x3 double][1x3 double][1x3 double][1x3 double]2.4结构数组%示例1:使用直接法来创建结构数组>> A(1).name = 'Pat';A(1).number = 176554;A(2).name = 'Tony';A(2).number = 901325;>> AA =1x2 struct array with fields:namenumber%示例2:利用struct函数来创建结构数组>> A(1)=struct('name','Pat','number',176554);A(2)=struct('name','Tony','number',901325);>> AA =1x2 struct array with fields:namenumber%示例3:使用deal函数来得到结构体中各结构域的值%定义结构数组A>> = 'Pat'; A.number = 176554;A(2).name = 'Tony';A(2).number = 901325;%得到结构数组中所有name结构域的数据>>[name1,name2] = deal(A(:).name)name1 =Patname2 =Tony%示例4:使用getfield函数来取得结构体中结构域的值%定义mystr结构数组>> mystr(1,1).name = 'alice';mystr(1,1).ID = 0;mystr(2,1).name = 'gertrude';mystr(2,1).ID = 1;%取得mystr(2,1)的结构域name的值>> f = getfield(mystr, {2,1}, 'name')f =gertrude%示例5:删除结构数组中的指定结构域%定义结构数组s>> s.field1=[1 2 3];s.field2='string';s.field3={1 2 3;4 5 6}; %删除结构域field1>> s=rmfield(s,'field1')s =field2: 'string'field3: {2x3 cell}%删除结构域'field2','field3'>> s=rmfield(s,{'field2','field3'})s =field1: [1 2 3]%示例6:%定义结构数组s>> s.field1=[1 2 3];s.field2='string';s.field3={1 2 3;4 5 6}; >> ss =field1: [1 2 3]field2: 'string'field3: {2x3 cell}%将结构数组转换为元胞数组>> c=struct2cell(s)c =[1x3 double]'string'{2x3 cell }%示例7:>>c = {'birch', 'betula', 65; 'maple', 'acer', 50}c ='birch''betula' [65]'maple''acer' [50]>>fields = {'name', 'genus', 'height'}; %fields包含struct中的结构域名>>s = cell2struct(c, fields, 2); %dim=2表示把c中的各行转换为struct数组s =2x1 struct array with fields:namegenusheight>> s(1)ans =name: 'birch'genus: 'betula'height: 65>> s(2)ans =name: 'maple'genus: 'acer'height: 50>> fields = {'field1', 'field2'};>> s = cell2struct(c, fields, 1); %dim=1表示把c中的各列转换为struct数组>> s(1)ans =field1: 'birch'field2: 'maple'>> s(2)ans =field1: 'betula'field2: 'acer'>> s(3)ans =field1:65field2:502.5矩阵操作%示例1: find函数的使用方法。
matlab程序

matlab程序(1)0122sin851z e =+ Z=(2*sin(85/180)*pi)/(1+exp(2))(2)22121(0.4552i z In x x +??=+=??-??其中x=[2,1+2i;-0.45,5]; Z=(1/2)*log(x+sqrt(1+(x*x)))(3) 已知 1234413134787,2033657327A B --==-????求下列表达式的值:A+6*B A=[12,34,-4;34,7,87;3,65,7]B=[1,3,-1;2,0,3;3,-2,7]Z=A+6*BA-B+I (其中I 为单位矩阵)U=A-B+eyeA*B D=A*BA^3 F=A^3A.^3 Z=A.^3A/B Z=A/BB\A Z=B/A[A,B] Z=[A,B][A ([1,3],:);B^2] Z=[A([1,3],:);B^2](4)设有矩阵A 和B1234530166789101769A ,111213141502341617181920970212223242541311B -==-求它们的乘积 A=[1,2,3,4,5;6,7,8,9,10;11,12,13,14,15;16,17,18,19,20;21,22,23,24,25]B=[3,0,16;17,-6,9;0,23,-4;9,7,0;4,13,11]C=A*B 将矩阵C 的右下⾓3*2⼦矩阵赋给D ;D=C([3,4,5],[2,3])(5)⽤for 循环语句实现求1~100的和 sum=0;for x=1:100 sum=sum+x;end(6) ⽤for 循环语句求10!n=10;y=1;for i=1:1:n;y=y*i;end(7) 分别⽤if 和switch 语句实现以下计算,a,b,c,x 的值从键盘输⼊:<≤<≤<≤++++=5.55.35.35.15.15.0,/,sin ,2x x x x c b In x b a c bx ax y c a=input('a=?') b=input('b=?') c=input('c=?') x=input('x=?') if(0.5<=x)&(x<1.5) y=a*x*x+b*x+celseif(1.5<=x)&(x<3.5) y=a*(sinb).^c+xelseif(3.5<=x)&(x<5.5) y=log(abs((b+c/x))) enda=input('a=?') b=input('b=?') c=input('c=?') x=input('x=?')switch xcase (0.5<=x)&(x<1.5) y=a*x*x+b*x+ccase (1.5<=x)&(x<3.5) y=a*(sinb).^c+xcase (3.5<=x)&(x<5.5) y=log(abs((b+c/x)))end(8)⽤图形表⽰连续调制波形Y=sin(t )sin(9t ) 及其包络线。
如何使用MATLAB创建一个最简单的程序

如何使用MATLAB创建一个最简单的程序使用MATLAB创建一个最简单的程序可以按照以下步骤进行:
例如,你可以使用disp函数在命令窗口中打印消息:
```
disp('Hello, World!');
```
或者,你可以使用fprintf函数在命令窗口中输出格式化的文本:```
fprintf('The sum of 2 and 3 is %d\n', 2+3);
```
这里,%d是一个格式化占位符,它将被替换为一个整数值。
4.运行程序。
在MATLAB命令窗口中,输入脚本文件名(不带.m扩展名),并按下回车键即可运行程序。
例如,如果你的脚本文件名为hello_world,你可以在命令窗口中输入:
```
hello_world
```
这将执行你所编写的MATLAB程序,并在命令窗口中显示输出结果。
以上是创建最简单的MATLAB程序的基本步骤。
此外,MATLAB还提供了一系列丰富的功能和工具箱,可以用于进行数值计算、绘图和数据分析等。
你可以在程序中使用这些功能来实现更复杂的任务。
matlab_简明实例教程

matlab_简明实例教程MATLAB是一种强大的科学计算工具,广泛应用于科学研究、数据分析和工程计算等领域。
它具有简单易用的语法和丰富的函数库,可以快速实现复杂的计算任务。
本教程将为你提供一些简单实例,帮助你入门MATLAB。
1.计算圆的面积和周长```matlabradius = input('请输入圆的半径:');area = pi * radius^2;circumference = 2 * pi * radius;disp(['圆的面积为:', num2str(area)]);disp(['圆的周长为:', num2str(circumference)]);```2.计算两个向量的点积```matlabv1 = input('请输入向量1(用逗号分隔元素):');v2 = input('请输入向量2(用逗号分隔元素):');dot_product = dot(v1, v2);disp(['两个向量的点积为:', num2str(dot_product)]);```3.绘制正弦曲线```matlabx = 0:0.1:2*pi;y = sin(x);plot(x, y);xlabel('x');ylabel('sin(x)');title('正弦曲线');```4.求解方程```matlabsyms x;eqn = x^2 - 4 == 0;sol = solve(eqn, x);disp(['方程的解为:', char(sol)]); ```5.读取和写入文件```matlabfilename = 'data.txt';data = importdata(filename);disp('文件中的数据:');disp(data);output = [1 2 3; 4 5 6; 7 8 9];dlmwrite('result.txt', output, 'delimiter', '\t', 'precision', 4);disp('结果已保存到result.txt文件中。
matlab简单编程21个题目及答案

1、设⎥⎦⎤⎢⎣⎡++=)1(sin35.0cos2xxxy,把x=0~2π间分为101点,画出以x为横坐标,y为纵坐标的曲线。
第一题的matlab源程序:①考虑cos(x)为一个整体,然后乘以中括号里面的全部x=0:2*pi/100:2*pi; %x的步长以及范围从0到2*pi y=cos(x).*(0.5+3*sin(x)./(1+x.^2)); %y的表达式plot(x,y)%画出图形图如下:②考虑对整体求解cos,先求x乘以括号中的部分x=0:2*pi/100:2*pi; %x的步长以及范围从0到2*pi y=cos(x.*(0.5+3*sin(x)./(1+x.^2))); %y的表达式plot(x,y) %画出图形图如下:2、产生8×6阶的正态分布随机数矩阵R1, 求其各列的平均值和均方差。
并求该矩阵全体数的平均值和均方差。
第二题的matlab源程序如下:R1=randn(8,6) %产生正态分布随机矩阵R1 =1.0933 -0.7697 1.5442 -0.1924 1.4193 0.21571.1093 0.3714 0.0859 0.8886 0.2916 -1.1658-0.8637 -0.2256 -1.4916 -0.7648 0.1978 -1.14800.0774 1.1174 -0.7423 -1.4023 1.5877 0.1049-1.2141 -1.0891 -1.0616 -1.4224 -0.8045 0.7223-1.1135 0.0326 2.3505 0.4882 0.6966 2.5855-0.0068 0.5525 -0.6156 -0.1774 0.8351 -0.66691.5326 1.1006 0.7481 -0.1961 -0.2437 0.1873aver=(sum(R1(1:end,1:end)))./8 %产生各行的平均值aver =0.0768 0.1363 0.1022 -0.3473 0.4975 0.1044a=std(R1(1:end,1:end)) %产生各行的均方差也就是标准差a =1.0819 0.8093 1.3456 0.8233 0.8079 1.2150aver1=(sum(R1(:)))./48 %全体数的平均值aver1 =0.0950b=std(R1(:)) %全体数的均方差即标准差b =1.01033、设x=rcost+3t,y=rsint+3,分别令r=2,3,4,画出参数t=0~10区间生成的x~y 曲线。
MATLAB操作命令大全

MATLAB操作命令大全1.基本操作:- clear: 清除工作区中的所有变量。
- clc: 清除命令窗口的内容。
- close all: 关闭所有图形窗口。
- help function-name: 显示与函数相关的帮助文档。
- who: 显示当前工作区中的所有变量。
- save file-name: 保存当前工作区中的所有变量到指定的文件。
- load file-name: 从文件中加载变量到当前工作区。
2.变量操作:-=:赋值操作符,将右边的值赋给左边的变量。
-+:加法操作符。
--:减法操作符。
-*:乘法操作符。
-/:除法操作符。
-^:幂运算操作符。
- sqrt(x): 计算 x 的平方根。
- abs(x): 计算 x 的绝对值。
- max(x): 返回 x 中的最大值。
- min(x): 返回 x 中的最小值。
- sum(x): 计算 x 中所有元素的和。
3.数组操作:- zeros(m, n): 创建一个 m 行 n 列的全零数组。
- ones(m, n): 创建一个 m 行 n 列的全一数组。
- eye(n): 创建一个 n 行 n 列的单位矩阵。
- size(x): 返回 x 的维度。
- length(x): 返回 x 的长度。
- reshape(x, m, n): 将 x 重新排列为一个 m 行 n 列矩阵。
- transpose(x): 将 x 的行和列互换。
4.控制流程:- if-else: 条件语句,根据条件执行不同的代码块。
- for loop: 循环语句,执行指定次数的代码块。
- while loop: 循环语句,根据条件反复执行代码块。
- break: 在循环中使用,用来跳出当前循环。
- continue: 在循环中使用,用来跳过当前循环的剩余部分。
5.统计分析:- mean(x): 计算 x 的平均值。
- median(x): 计算 x 的中位数。
- std(x): 计算 x 的标准差。
matlab编程实例100例(精编文档).doc

【最新整理,下载后即可编辑】1-32是:图形应用篇33-66是:界面设计篇67-84是:图形处理篇85-100是:数值分析篇实例1:三角函数曲线(1)function shili01h0=figure('toolbar','none',...'position',[198****0300],...'name','实例01');h1=axes('parent',h0,...'visible','off');x=-pi:0.05:pi;y=sin(x);plot(x,y);xlabel('自变量X');ylabel('函数值Y');title('SIN( )函数曲线');grid on实例2:三角函数曲线(2)function shili02h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例02');x=-pi:0.05:pi;y=sin(x)+cos(x);plot(x,y,'-*r','linewidth',1);grid onxlabel('自变量X');ylabel('函数值Y');title('三角函数');实例3:图形的叠加function shili03h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例03');x=-pi:0.05:pi;y1=sin(x);y2=cos(x);plot(x,y1,...'-*r',...x,y2,...'--og');grid onxlabel('自变量X');ylabel('函数值Y');title('三角函数');实例4:双y轴图形的绘制function shili04h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例04');x=0:900;a=1000;b=0.005;y1=2*x;y2=cos(b*x);[haxes,hline1,hline2]=plotyy(x,y1,x,y2,'semilogy','plot'); axes(haxes(1))ylabel('semilog plot');axes(haxes(2))ylabel('linear plot');实例5:单个轴窗口显示多个图形function shili05h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例05');t=0:pi/10:2*pi;[x,y]=meshgrid(t);subplot(2,2,1)plot(sin(t),cos(t))axis equalsubplot(2,2,2)z=sin(x)-cos(y);plot(t,z)axis([0 2*pi -2 2])subplot(2,2,3)h=sin(x)+cos(y);plot(t,h)axis([0 2*pi -2 2])subplot(2,2,4)g=(sin(x).^2)-(cos(y).^2);plot(t,g)axis([0 2*pi -1 1])实例6:图形标注function shili06h0=figure('toolbar','none',...'position',[200 150 450 400],...'name','实例06');t=0:pi/10:2*pi;h=plot(t,sin(t));xlabel('t=0到2\pi','fontsize',16);ylabel('sin(t)','fontsize',16);title('\it{从0to2\pi 的正弦曲线}','fontsize',16) x=get(h,'xdata');y=get(h,'ydata');imin=find(min(y)==y);imax=find(max(y)==y);text(x(imin),y(imin),...['\leftarrow最小值=',num2str(y(imin))],...'fontsize',16)text(x(imax),y(imax),...['\leftarrow最大值=',num2str(y(imax))],...'fontsize',16)实例7:条形图形function shili07h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例07');tiao1=[562 548 224 545 41 445 745 512];tiao2=[47 48 57 58 54 52 65 48];t=0:7;bar(t,tiao1)xlabel('X轴');ylabel('TIAO1值');h1=gca;h2=axes('position',get(h1,'position'));plot(t,tiao2,'linewidth',3)set(h2,'yaxislocation','right','color','none','xticklabel',[]) 实例8:区域图形function shili08h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例08');x=91:95;profits1=[88 75 84 93 77];profits2=[51 64 54 56 68];profits3=[42 54 34 25 24];profits4=[26 38 18 15 4];area(x,profits1,'facecolor',[0.5 0.9 0.6],...'edgecolor','b',...'linewidth',3)hold onarea(x,profits2,'facecolor',[0.9 0.85 0.7],...'edgecolor','y',...'linewidth',3)hold onarea(x,profits3,'facecolor',[0.3 0.6 0.7],...'edgecolor','r',...'linewidth',3)hold onarea(x,profits4,'facecolor',[0.6 0.5 0.9],...'edgecolor','m',...'linewidth',3)hold offset(gca,'xtick',[91:95])set(gca,'layer','top')gtext('\leftarrow第一季度销量') gtext('\leftarrow第二季度销量') gtext('\leftarrow第三季度销量') gtext('\leftarrow第四季度销量') xlabel('年','fontsize',16);ylabel('销售量','fontsize',16);实例9:饼图的绘制function shili09h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例09');t=[54 21 35;68 54 35;45 25 12;48 68 45;68 54 69];x=sum(t);h=pie(x);textobjs=findobj(h,'type','text');str1=get(textobjs,{'string'});val1=get(textobjs,{'extent'});oldext=cat(1,val1{:});names={'商品一:';'商品二:';'商品三:'};str2=strcat(names,str1);set(textobjs,{'string'},str2)val2=get(textobjs,{'extent'});newext=cat(1,val2{:});offset=sign(oldext(:,1)).*(newext(:,3)-oldext(:,3))/2; pos=get(textobjs,{'position'});textpos=cat(1,pos{:});textpos(:,1)=textpos(:,1)+offset;set(textobjs,{'position'},num2cell(textpos,[3,2]))实例10:阶梯图function shili10h0=figure('toolbar','none',...'position',[200 150 450 400],...'name','实例10');a=0.01;b=0.5;t=0:10;f=exp(-a*t).*sin(b*t);stairs(t,f)hold onplot(t,f,':*')hold offglabel='函数e^{-(\alpha*t)}sin\beta*t的阶梯图'; gtext(glabel,'fontsize',16)xlabel('t=0:10','fontsize',16)axis([0 10 -1.2 1.2])实例11:枝干图function shili11h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例11');x=0:pi/20:2*pi;y1=sin(x);y2=cos(x);h1=stem(x,y1+y2);hold onh2=plot(x,y1,'^r',x,y2,'*g');hold offh3=[h1(1);h2];legend(h3,'y1+y2','y1=sin(x)','y2=cos(x)') xlabel('自变量X');ylabel('函数值Y');title('正弦函数与余弦函数的线性组合'); 实例12:罗盘图function shili12h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例12');winddirection=[54 24 65 84256 12 235 62125 324 34 254];windpower=[2 5 5 36 8 12 76 14 10 8];rdirection=winddirection*pi/180;[x,y]=pol2cart(rdirection,windpower); compass(x,y);desc={'风向和风力','北京气象台','10月1日0:00到','10月1日12:00'};gtext(desc)实例13:轮廓图function shili13h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例13');[th,r]=meshgrid((0:10:360)*pi/180,0:0.05:1); [x,y]=pol2cart(th,r);z=x+i*y;f=(z.^4-1).^(0.25);contour(x,y,abs(f),20)axis equalxlabel('实部','fontsize',16);ylabel('虚部','fontsize',16);h=polar([0 2*pi],[0 1]);delete(h)hold oncontour(x,y,abs(f),20)实例14:交互式图形function shili14h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例14');axis([0 10 0 10]);hold onx=[];y=[];n=0;disp('单击鼠标左键点取需要的点'); disp('单击鼠标右键点取最后一个点'); but=1;while but==1[xi,yi,but]=ginput(1);plot(xi,yi,'bo')n=n+1;disp('单击鼠标左键点取下一个点');x(n,1)=xi;y(n,1)=yi;endt=1:n;ts=1:0.1:n;xs=spline(t,x,ts);ys=spline(t,y,ts);plot(xs,ys,'r-');hold off实例14:交互式图形function shili14h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例14');axis([0 10 0 10]);hold onx=[];y=[];n=0;disp('单击鼠标左键点取需要的点'); disp('单击鼠标右键点取最后一个点'); but=1;while but==1[xi,yi,but]=ginput(1);plot(xi,yi,'bo')n=n+1;disp('单击鼠标左键点取下一个点');x(n,1)=xi;y(n,1)=yi;endt=1:n;ts=1:0.1:n;xs=spline(t,x,ts);ys=spline(t,y,ts);plot(xs,ys,'r-');hold off实例15:变换的傅立叶函数曲线function shili15h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例15');axis equalm=moviein(20,gcf);set(gca,'nextplot','replacechildren')h=uicontrol('style','slider','position',...[100 10 500 20],'min',1,'max',20)for j=1:20plot(fft(eye(j+16)))set(h,'value',j)m(:,j)=getframe(gcf);endclf;axes('position',[0 0 1 1]);movie(m,30)实例16:劳伦兹非线形方程的无序活动function shili15h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例15');axis equalm=moviein(20,gcf);set(gca,'nextplot','replacechildren')h=uicontrol('style','slider','position',...[100 10 500 20],'min',1,'max',20)for j=1:20plot(fft(eye(j+16)))set(h,'value',j)m(:,j)=getframe(gcf);endclf;axes('position',[0 0 1 1]);movie(m,30)实例17:填充图function shili17h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例17');t=(1:2:15)*pi/8;x=sin(t);y=cos(t);fill(x,y,'r')axis square offtext(0,0,'STOP',...'color',[1 1 1],...'fontsize',50,...'horizontalalignment','center') 例18:条形图和阶梯形图function shili18h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例18');subplot(2,2,1)x=-3:0.2:3;y=exp(-x.*x);bar(x,y)title('2-D Bar Chart')subplot(2,2,2)x=-3:0.2:3;y=exp(-x.*x);bar3(x,y,'r')title('3-D Bar Chart')subplot(2,2,3)x=-3:0.2:3;y=exp(-x.*x);stairs(x,y)title('Stair Chart')subplot(2,2,4)x=-3:0.2:3;y=exp(-x.*x);barh(x,y)title('Horizontal Bar Chart')实例19:三维曲线图function shili19h0=figure('toolbar','none',...'position',[200 150 450 400],...'name','实例19');subplot(2,1,1)x=linspace(0,2*pi);y1=sin(x);y2=cos(x);y3=sin(x)+cos(x);z1=zeros(size(x));z2=0.5*z1;z3=z1;plot3(x,y1,z1,x,y2,z2,x,y3,z3)grid onxlabel('X轴');ylabel('Y轴');zlabel('Z轴');title('Figure1:3-D Plot')subplot(2,1,2)x=linspace(0,2*pi);y1=sin(x);y2=cos(x);y3=sin(x)+cos(x);z1=zeros(size(x));z2=0.5*z1;z3=z1;plot3(x,z1,y1,x,z2,y2,x,z3,y3)grid onxlabel('X轴');ylabel('Y轴');zlabel('Z轴');title('Figure2:3-D Plot')实例20:图形的隐藏属性function shili20h0=figure('toolbar','none',...'position',[200 150 450 300],...'name','实例20');subplot(1,2,1)[x,y,z]=sphere(10);mesh(x,y,z)axis offtitle('Figure1:Opaque')hidden onsubplot(1,2,2)[x,y,z]=sphere(10);mesh(x,y,z)axis offtitle('Figure2:Transparent') hidden off实例21PEAKS函数曲线function shili21h0=figure('toolbar','none',...'position',[200 100 450 450],...'name','实例21');[x,y,z]=peaks(30);subplot(2,1,1)x=x(1,:);y=y(:,1);i=find(y>0.8&y<1.2);j=find(x>-0.6&x<0.5);z(i,j)=nan*z(i,j);surfc(x,y,z)xlabel('X轴');ylabel('Y轴');zlabel('Z轴');title('Figure1:surfc函数形成的曲面') subplot(2,1,2)x=x(1,:);y=y(:,1);i=find(y>0.8&y<1.2);j=find(x>-0.6&x<0.5);z(i,j)=nan*z(i,j);surfl(x,y,z)xlabel('X轴');ylabel('Y轴');zlabel('Z轴');title('Figure2:surfl函数形成的曲面') 实例22:片状图function shili22h0=figure('toolbar','none',...'position',[200 150 550 350],...'name','实例22');subplot(1,2,1)x=rand(1,20);y=rand(1,20);z=peaks(x,y*pi);t=delaunay(x,y);trimesh(t,x,y,z)hidden offtitle('Figure1:Triangular Surface Plot'); subplot(1,2,2)x=rand(1,20);y=rand(1,20);z=peaks(x,y*pi);t=delaunay(x,y);trisurf(t,x,y,z)title('Figure1:Triangular Surface Plot'); 实例23:视角的调整function shili23h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例23');x=-5:0.5:5;[x,y]=meshgrid(x);r=sqrt(x.^2+y.^2)+eps;z=sin(r)./r;subplot(2,2,1)surf(x,y,z)xlabel('X-axis')ylabel('Y-axis')zlabel('Z-axis')title('Figure1')view(-37.5,30)subplot(2,2,2)surf(x,y,z)xlabel('X-axis')ylabel('Y-axis')zlabel('Z-axis')title('Figure2')view(-37.5+90,30) subplot(2,2,3)surf(x,y,z)xlabel('X-axis')ylabel('Y-axis')zlabel('Z-axis')title('Figure3')view(-37.5,60)subplot(2,2,4)surf(x,y,z)xlabel('X-axis')ylabel('Y-axis')zlabel('Z-axis')title('Figure4')view(180,0)实例24:向量场的绘制function shili24h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例24');subplot(2,2,1)z=peaks;ribbon(z)title('Figure1')subplot(2,2,2)[x,y,z]=peaks(15);[dx,dy]=gradient(z,0.5,0.5); contour(x,y,z,10)hold onquiver(x,y,dx,dy)hold offtitle('Figure2')subplot(2,2,3)[x,y,z]=peaks(15);[nx,ny,nz]=surfnorm(x,y,z);surf(x,y,z)hold onquiver3(x,y,z,nx,ny,nz)hold offtitle('Figure3')subplot(2,2,4)x=rand(3,5);y=rand(3,5);z=rand(3,5);c=rand(3,5);fill3(x,y,z,c)grid ontitle('Figure4')实例25:灯光定位function shili25h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例25');vert=[1 1 1;1 2 1;2 2 1;2 1 1;1 1 2;12 2;2 2 2;2 1 2];fac=[1 2 3 4;2 6 7 3;4 3 7 8;15 8 4;1 2 6 5;5 6 7 8];grid offsphere(36)h=findobj('type','surface');set(h,'facelighting','phong',...'facecolor',...'interp',...'edgecolor',[0.4 0.4 0.4],...'backfacelighting',...'lit')hold onpatch('faces',fac,'vertices',vert,...'facecolor','y');light('position',[1 3 2]);light('position',[-3 -1 3]);material shinyaxis vis3d offhold off实例26:柱状图function shili26h0=figure('toolbar','none',...'position',[200 50 450 450],...'name','实例26'); subplot(2,1,1)x=[5 2 18 7 39 8 65 5 54 3 2];bar(x)xlabel('X轴');ylabel('Y轴');title('第一子图');subplot(2,1,2)y=[5 2 18 7 39 8 65 5 54 3 2];barh(y)xlabel('X轴');ylabel('Y轴');title('第二子图');实例27:设置照明方式function shili27h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例27');subplot(2,2,1)sphereshading flatcamlight leftcamlight rightlighting flatcolorbaraxis offtitle('Figure1')subplot(2,2,2)sphereshading flatcamlight leftcamlight rightlighting gouraudcolorbaraxis offtitle('Figure2')subplot(2,2,3)sphereshading interpcamlight rightcamlight leftlighting phongaxis offtitle('Figure3')subplot(2,2,4)sphereshading flatcamlight leftcamlight rightlighting nonecolorbaraxis offtitle('Figure4')实例28:羽状图function shili28h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例28');subplot(2,1,1)alpha=90:-10:0;r=ones(size(alpha));m=alpha*pi/180;n=r*10;[u,v]=pol2cart(m,n);feather(u,v)title('羽状图')axis([0 20 0 10])subplot(2,1,2)t=0:0.5:10;y=exp(-x*t);feather(y)title('复数矩阵的羽状图')实例29:立体透视(1)function shili29h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例29');[x,y,z]=meshgrid(-2:0.1:2,...-2:0.1:2,...-2:0.1:2);v=x.*exp(-x.^2-y.^2-z.^2);grid onfor i=-2:0.5:2;h1=surf(linspace(-2,2,20),...linspace(-2,2,20),...zeros(20)+i);rotate(h1,[1 -1 1],30)dx=get(h1,'xdata');dy=get(h1,'ydata');dz=get(h1,'zdata');delete(h1)slice(x,y,z,v,[-2 2],2,-2)hold onslice(x,y,z,v,dx,dy,dz)hold offaxis tightview(-5,10)drawnowend实例30:立体透视(2)function shili30h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例30');[x,y,z]=meshgrid(-2:0.1:2,...-2:0.1:2,...-2:0.1:2);v=x.*exp(-x.^2-y.^2-z.^2); [dx,dy,dz]=cylinder;slice(x,y,z,v,[-2 2],2,-2)for i=-2:0.2:2h=surface(dx+i,dy,dz);rotate(h,[1 0 0],90)xp=get(h,'xdata');yp=get(h,'ydata');zp=get(h,'zdata');delete(h)hold onhs=slice(x,y,z,v,xp,yp,zp);axis tightxlim([-3 3])view(-10,35)drawnowdelete(hs)hold offend实例31:表面图形function shili31h0=figure('toolbar','none',...'position',[200 150 550 250],...'name','实例31');subplot(1,2,1)x=rand(100,1)*16-8;y=rand(100,1)*16-8;r=sqrt(x.^2+y.^2)+eps;z=sin(r)./r;xlin=linspace(min(x),max(x),33); ylin=linspace(min(y),max(y),33); [X,Y]=meshgrid(xlin,ylin);Z=griddata(x,y,z,X,Y,'cubic'); mesh(X,Y,Z)axis tighthold onplot3(x,y,z,'.','Markersize',20) subplot(1,2,2)k=5;n=2^k-1;theta=pi*(-n:2:n)/n;phi=(pi/2)*(-n:2:n)'/n;X=cos(phi)*cos(theta);Y=cos(phi)*sin(theta);Z=sin(phi)*ones(size(theta)); colormap([0 0 0;1 1 1])C=hadamard(2^k);surf(X,Y,Z,C)axis square实例32:沿曲线移动的小球h0=figure('toolbar','none',...'position',[198****8468],...'name','实例32');h1=axes('parent',h0,...'position',[0.15 0.45 0.7 0.5],...'visible','on');t=0:pi/24:4*pi;y=sin(t);plot(t,y,'b')n=length(t);h=line('color',[0 0.5 0.5],...'linestyle','.',...'markersize',25,...'erasemode','xor');k1=uicontrol('parent',h0,...'style','pushbutton',...'position',[80 100 50 30],...'string','开始',...'callback',[...'i=1;',...'k=1;,',...'m=0;,',...'while 1,',...'if k==0,',...'break,',...'end,',...'if k~=0,',...'set(h,''xdata'',t(i),''ydata'',y(i)),',...'drawnow;,',...'i=i+1;,',...'if i>n,',...'m=m+1;,',...'i=1;,',...'end,',...'end,',...'end']);k2=uicontrol('parent',h0,...'style','pushbutton',...'position',[180 100 50 30],...'string','停止',...'callback',[...'k=0;,',...'set(e1,''string'',m),',...'p=get(h,''xdata'');,',...'q=get(h,''ydata'');,',...'set(e2,''string'',p);,',...'set(e3,''string'',q)']); k3=uicontrol('parent',h0,...'style','pushbutton',...'position',[280 100 50 30],...'string','关闭',...'callback','close');e1=uicontrol('parent',h0,...'style','edit',...'position',[60 30 60 20]);t1=uicontrol('parent',h0,...'style','text',...'string','循环次数',...'position',[60 50 60 20]);e2=uicontrol('parent',h0,...'style','edit',...'position',[180 30 50 20]);t2=uicontrol('parent',h0,...'style','text',...'string','终点的X坐标值',...'position',[155 50 100 20]);e3=uicontrol('parent',h0,...'style','edit',...'position',[300 30 50 20]);t3=uicontrol('parent',h0,...'style','text',...'string','终点的Y坐标值',...'position',[275 50 100 20]);实例33:曲线转换按钮h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例33');x=0:0.5:2*pi;y=sin(x);h=plot(x,y);grid onhuidiao=[...'if i==1,',...'i=0;,',...'y=cos(x);,',...'delete(h),',...'set(hm,''string'',''正弦函数''),',...'h=plot(x,y);,',...'grid on,',...'else if i==0,',...'i=1;,',...'y=sin(x);,',...'set(hm,''string'',''余弦函数''),',...'delete(h),',...'h=plot(x,y);,',...'grid on,',...'end,',...'end'];hm=uicontrol(gcf,'style','pushbutton',...'string','余弦函数',...'callback',huidiao);i=1;set(hm,'position',[250 20 60 20]);set(gca,'position',[0.2 0.2 0.6 0.6])title('按钮的使用')hold on实例34:栅格控制按钮h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例34');x=0:0.5:2*pi;y=sin(x);plot(x,y)huidiao1=[...'set(h_toggle2,''value'',0),',...'grid on,',...];huidiao2=[...'set(h_toggle1,''value'',0),',...'grid off,',...];h_toggle1=uicontrol(gcf,'style','togglebutton',...'string','grid on',...'value',0,...'position',[20 45 50 20],...'callback',huidiao1);h_toggle2=uicontrol(gcf,'style','togglebutton',...'string','grid off',...'value',0,...'position',[20 20 50 20],...'callback',huidiao2);set(gca,'position',[0.2 0.2 0.6 0.6])title('开关按钮的使用')实例35:编辑框的使用h0=figure('toolbar','none',...'position',[200 150 350 250],...'name','实例35');f='Please input the letter';huidiao1=[...'g=upper(f);,',...'set(h2_edit,''string'',g),',...];huidiao2=[...'g=lower(f);,',...'set(h2_edit,''string'',g),',...];h1_edit=uicontrol(gcf,'style','edit',...'position',[100 200 100 50],...'HorizontalAlignment','left',...'string','Please input the letter',...'callback','f=get(h1_edit,''string'');',...'background','w',...'max',5,...'min',1);h2_edit=uicontrol(gcf,'style','edit',...'HorizontalAlignment','left',...'position',[100 100 100 50],...'background','w',...'max',5,...'min',1);h1_button=uicontrol(gcf,'style','pushbutton',...'string','小写变大写',...'position',[100 45 100 20],...'callback',huidiao1);h2_button=uicontrol(gcf,'style','pushbutton',...'string','大写变小写',...'position',[100 20 100 20],...'callback',huidiao2);实例36:弹出式菜单h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例36');x=0:0.5:2*pi;y=sin(x);h=plot(x,y);grid onhm=uicontrol(gcf,'style','popupmenu',...'string',...'sin(x)|cos(x)|sin(x)+cos(x)|exp(-sin(x))',...'position',[250 20 50 20]);set(hm,'value',1)huidiao=[...'v=get(hm,''value'');,',...'switch v,',...'case 1,',...'delete(h),',...'y=sin(x);,',...'h=plot(x,y);,',...'grid on,',...'case 2,',...'delete(h),',...'y=cos(x);,',...'h=plot(x,y);,',...'grid on,',...'case 3,',...'delete(h),',...'y=sin(x)+cos(x);,',...'h=plot(x,y);,',...'grid on,',...'case 4,',...'delete(h),',...'y=exp(-sin(x));,',...'h=plot(x,y);,',...'grid on,',...'end'];set(hm,'callback',huidiao)set(gca,'position',[0.2 0.2 0.6 0.6]) title('弹出式菜单的使用')实例37:滑标的使用h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例37');[x,y]=meshgrid(-8:0.5:8);r=sqrt(x.^2+y.^2)+eps;z=sin(r)./r;h0=mesh(x,y,z);h1=axes('position',...[0.2 0.2 0.5 0.5],...'visible','off');htext=uicontrol(gcf,...'units','points',...'position',[20 30 45 15],...'string','brightness',...'style','text');hslider=uicontrol(gcf,...'units','points',...'position',[10 10 300 15],...'min',-1,...'max',1,...'style','slider',...'callback',...'brighten(get(hslider,''value''))'); 实例38:多选菜单h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例38');[x,y]=meshgrid(-8:0.5:8);r=sqrt(x.^2+y.^2)+eps;z=sin(r)./r;h0=mesh(x,y,z);hlist=uicontrol(gcf,'style','listbox',...'string','default|spring|summer|autumn|winter',...'max',5,...'min',1,...'position',[20 20 80 100],...'callback',[...'k=get(hlist,''value'');,',...'switch k,',...'case 1,',...'colormap default,',...'case 2,',...'colormap spring,',...'case 3,',...'colormap summer,',...'case 4,',...'colormap autumn,',...'case 5,',...'colormap winter,',...'end']);实例39:菜单控制的使用h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例39');x=0:0.5:2*pi;y=cos(x);h=plot(x,y);grid onset(gcf,'toolbar','none')hm=uimenu('label','example');huidiao1=[...'set(hm_gridon,''checked'',''on''),',...'set(hm_gridoff,''checked'',''off''),',...'grid on'];huidiao2=[...'set(hm_gridoff,''checked'',''on''),',...'set(hm_gridon,''checked'',''off''),',...'grid off'];hm_gridon=uimenu(hm,'label','grid on',...'checked','on',...'callback',huidiao1);hm_gridoff=uimenu(hm,'label','grid off',...'checked','off',...'callback',huidiao2);实例40:UIMENU菜单的应用h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例40');h1=uimenu(gcf,'label','函数');h11=uimenu(h1,'label','轮廓图',...'callback',[...'set(h31,''checked'',''on''),',...'set(h32,''checked'',''off''),',...'[x,y,z]=peaks;,',...'contour3(x,y,z,30)']);h12=uimenu(h1,'label','高斯分布',...'callback',[...'set(h31,''checked'',''on''),',...'set(h32,''checked'',''off''),',...'mesh(peaks);,',...'axis tight']);。
(完整word版)MatLab代码大全

第2章图像获取2.3.2 二维连续傅里叶变换例2.2figure(1); %建立图形窗口1[u,v] = meshgrid(-1:0.01:1); %生成二维频域网格F1 = abs(sinc(u.*pi));F2 = abs(sinc(v.*pi));F=F1.*F2; %计算幅度频谱F=|F(u,v)|surf(u,v,F); %显示幅度频谱,如图2.3(b)shading interp; %平滑三维曲面上的小格axis off; %关闭坐标系figure(2); %建立图形窗口2F1=histeq(F); %扩展F的对比度以增强视觉效果imshow(F1); %用图像来显示幅度频谱,如图2.3(c)第3章图像变换3.4.4 二维FFT的MATLAB实现例3.2 简单图像及其傅里叶变换MATLAB程序:%建立简单图像d并显示之d = zeros(32,32); %图像大小32⨯32d(13:20,13:20) = 1; %中心白色方块大小为8⨯8figure(1); %建立图形窗口1imshow(d,'notruesize');%显示图像d如图3.5(a)所示%计算傅里叶变换并显示之D = fft2(d); %计算图像d的傅里叶变换,fft2(d) = fft(fft(d).').'figure(2); %建立图形窗口2imshow(abs(D),[-1 5],'notruesize'); %显示图像d的傅里叶变换谱如3.5(b)所示例3.3 MATLAB图像及其傅里叶变换谱MATLAB程序:figure(1);load imdemos saturn2; %装入MA TLAB图像saturn2imshow(saturn2); %显示图像saturn2如图3.6(a)所示figure(2);S= fftshift(fft2(saturn2)); %计算傅里叶变换并移位imshow(log(abs(S)),[ ]); %显示傅里叶变换谱如3.6(b)所示例3.4 真彩图像及其傅里叶变换谱MATLAB程序:figure(1);A=imread('image1.jpg'); %装入真彩图像,见图1.1(b)B=rgb2gray(A); %将真彩图像转换为灰度图像imshow(B); %显示灰度图像如图3.7(a)所示C=fftshift(fft2(B)); %计算傅里叶变换并移位figure(2);imshow(log(abs(C)),[ ]); %显示傅里叶变换谱如3.7(b)所示3.5.4 离散余弦变换的MATLAB实现例3.5 计算并显示真彩图像余弦变换的MATLAB程序如下:RGB=imread('image2.jpg'); %装入真彩图像figure(1);imshow(RGB); %显示彩色图像GRAY=rgb2gray(RGB); %将真彩图像转换为灰度图像figure(2);imshow(GRAY); %显示灰度图像如图3.10(a)所示DCT=dct2(GRAY); %进行余弦变换figure(3);imshow(log(abs(DCT)),[ ]); %显示余弦变换如图3.10(b)所示。
matlab常见经典平差程序

matlab常见经典平差程序希望本人编写的经典平差可以对matlab初学者以及测绘专业的学生有用By cowboyfunction void()sprintf('请选择平差类型')sprintf('1: 参数平差\n2: 条件平差\n3:具有条件的参数平差\n4: 具有参数的条件平差\n') a=input('请输入对应号码\n');switch (a)case 1% 参数平差V=AX-Lsprintf('1:参数平差V=AX-L')n=input('请输入n=');t=input('请输入t=');A=input('请输入系数矩阵A=');L=input('请输入常数项矩阵L=');P=diag(input('请输入权阵P='))%P=input('请输入权阵P=');N=A'*P*A;U=A'*P*L;sprintf('开始计算')X=inv(N)*(U);V=A*X-L;Qx=inv(N);u=sqrt((V'*P*V)/(n-t));fid=fopen('data.txt','wt'); %写入文件路径,文件输出fprintf(fid,'=====================参数平差: V=AX-L =====================\n');fprintf(fid,'输出n,t\n') ;fprintf(fid,'%12.5f %12.5f\n',n ,t) ;%n,t fprintf(fid,'输出矩阵A\n')[m,n]=size(A); %Afor i=1:1:mfor j=1:1:nif j==nfprintf(fid,'%12.5f\n',A(i,j));elsefprintf(fid,'%12.5f\t',A(i,j));endendendfprintf(fid,'\n');fprintf(fid,'输出矩阵L\n') ;[m,n]=size(L); %Lfor i=1:1:mfor j=1:1:nif j==nfprintf(fid,'%12.5f\n',L(i,j));elsefprintf(fid,'%12.5f\t',L(i,j));endendendfprintf(fid,'\n');fprintf(fid,'输出矩阵P\n') ;[m,n]=size(P); %P for i=1:1:mfor j=1:1:nif j==nfprintf(fid,'%12.5f\n',P(i,j));elsefprintf(fid,'%12.5f\t',P(i,j));endendendfprintf(fid,'\n');fprintf(fid,'输出矩阵X\n'); [m,n]=size(X); %X for i=1:1:m for j=1:1:nif j==nfprintf(fid,'%12.5f\n',X(i,j));elsefprintf(fid,'%12.5f\t',X(i,j));endendendfprintf(fid,'\n');fprintf(fid,'输出矩阵V\n'); [m,n]=size(V); %V for i=1:1:m for j=1:1:nif j==nfprintf(fid,'%12.5f\n',V(i,j));elsefprintf(fid,'%12.5f\t',V(i,j));endendendfprintf(fid,'\n');fprintf(fid,'输出矩阵Qx\n'); [m,n]=size(Qx); %Qx for i=1:1:mfor j=1:1:nif j==nfprintf(fid,'%12.5f\n',Qx(i,j)); elsefprintf(fid,'%12.5f\t',Qx(i,j));endendendfprintf(fid,'\n');fprintf(fid,'输出单位权中误差u\n'); [m,n]=size(u); %u for i=1:1:m for j=1:1:nif j==nfprintf(fid,'%12.5f\n',u(i,j));elsefprintf(fid,'%12.5f\t',u(i,j));endendendfprintf(fid,'\n');fclose(fid);case 2% 条件平差BV+W=0sprintf('2:条件平差BV+W=0')n=input('请输入n=');t=input('请输入t=');B=input('请输入系数矩阵B=');W=input('请输入自由项向量W='); P=input('请输入权阵P='); sprintf('开始计算')K=-inv((B*inv(P)*B'))*W;V=inv(P)*B'*K;u=sqrt((V'*P*V)/(n-t));Ql=inv(P)-inv(P)*B'*inv(B*inv(P)*B')*B*inv(P);fid=fopen('data.txt','wt'); %写入文件路径,文件输出fprintf(fid,'=====================条件平差: BV+W=0 =====================\n'); fprintf(fid,'输出n,t\n') ;fprintf(fid,'%12.5f %12.5f\n',n ,t) ;%n,tfprintf(fid,'输出矩阵B\n');[m,n]=size(B); %Bfor i=1:1:mfor j=1:1:nif j==nfprintf(fid,'%12.5f\n',B(i,j));elsefprintf(fid,'%12.5f\t',B(i,j));endendendfprintf(fid,'\n');fprintf(fid,'输出矩阵W\n');[m,n]=size(W); %Wfor i=1:1:mfor j=1:1:nif j==nfprintf(fid,'%12.5f\n',W(i,j));elsefprintf(fid,'%12.5f\t',W(i,j));endendendfprintf(fid,'\n');fprintf(fid,'输出矩阵P\n'); [m,n]=size(P); %Pfor i=1:1:mfor j=1:1:nif j==nfprintf(fid,'%12.5f\n',P(i,j)); elsefprintf(fid,'%12.5f\t',P(i,j));endendendfprintf(fid,'\n');fprintf(fid,'输出矩阵V\n'); [m,n]=size(V); %Vfor i=1:1:mfor j=1:1:nif j==nfprintf(fid,'%12.5f\n',V(i,j)); elsefprintf(fid,'%12.5f\t',V(i,j)); endendendfprintf(fid,'\n');fprintf(fid,'输出矩阵Qx\n'); [m,n]=size(Ql); %Ql for i=1:1:m for j=1:1:nif j==nfprintf(fid,'%12.5f\n',Ql(i,j));elsefprintf(fid,'%12.5f\t',Ql(i,j));endendendfprintf(fid,'\n');fprintf(fid,'输出单位权中误差u\n');[m,n]=size(u); %u for i=1:1:mfor j=1:1:nif j==nfprintf(fid,'%12.5f\n',u(i,j));elsefprintf(fid,'%12.5f\t',u(i,j));endendendfprintf(fid,'\n');fclose(fid);case 3% 具有条件的参数平差% 参数平差V=AX-L% 条件平差BX+W=0sprintf('3:具有条件的参数平差V=AX-L;BX+W=0') n=input('请输入n=');t=input('请输入t=');r=input('请输入r=');A=input('请输入误差方程系数矩阵A=');L=input('请输入常数项矩阵L=');B=input('请输入条件方程系数矩阵B=');W=input('请输入自由项向量W=');P=input('请输入权阵P=');sprintf('开始计算')K=-(inv(B*inv(A'*P*A)*B'))*(W+B*inv(A'*P*A)*A'*P*L)X=(inv(A'*P*A))*(B'*K+A'*P*L)N=(A'*P*A)M=B*inv(N)*B'Qx=inv(N)-inv(N)*B'*inv(M)*B*inv(N)u=sqrt((V'*P*V)/(n-t+r))fid=fopen('data.txt','wt'); %写入文件路径,文件输出fprintf(fid,'=====================具有条件的参数平差: V=AX-L;BX+W=0 ===========================\n');fprintf(fid,'输出n,t,r\n') ;fprintf(fid,'%12.5f %12.5f %12.5f\n',n ,t,r); %n,t,rfprintf(fid,'输出矩阵A\n');[m,n]=size(A); %Afor i=1:1:mfor j=1:1:nif j==nfprintf(fid,'%12.5f\n',A(i,j));elsefprintf(fid,'%12.5f\t',A(i,j));endendendfprintf(fid,'\n');fprintf(fid,'输出矩阵L\n');[m,n]=size(L); %Lfor i=1:1:mfor j=1:1:nif j==nfprintf(fid,'%12.5f\n',L(i,j)); elsefprintf(fid,'%12.5f\t',L(i,j)); endendendfprintf(fid,'\n');fprintf(fid,'输出矩阵B\n'); [m,n]=size(B); %Bfor i=1:1:mfor j=1:1:nif j==nfprintf(fid,'%12.5f\n',B(i,j)); fprintf(fid,'%12.5f\t',B(i,j)); endendendfprintf(fid,'\n');fprintf(fid,'输出矩阵W\n'); [m,n]=size(W); %W for i=1:1:m for j=1:1:nif j==nfprintf(fid,'%12.5f\n',W(i,j)); elsefprintf(fid,'%12.5f\t',W(i,j)); endendendfprintf(fid,'\n');fprintf(fid,'输出矩阵P\n'); [m,n]=size(P); %P for i=1:1:m for j=1:1:nif j==nfprintf(fid,'%12.5f\n',P(i,j)); elsefprintf(fid,'%12.5f\t',P(i,j)); endendendfprintf(fid,'\n');fprintf(fid,'输出矩阵X\n'); [m,n]=size(X); %X for i=1:1:m for j=1:1:nif j==nfprintf(fid,'%12.5f\n',X(i,j)); elsefprintf(fid,'%12.5f\t',X(i,j)); endendendfprintf(fid,'\n');fprintf(fid,'输出矩阵V\n'); [m,n]=size(V); %Vfor j=1:1:nif j==nfprintf(fid,'%12.5f\n',V(i,j)); elsefprintf(fid,'%12.5f\t',V(i,j));endendendfprintf(fid,'\n');fprintf(fid,'输出矩阵Qx\n'); [m,n]=size(Qx); %Qx for i=1:1:m for j=1:1:nif j==nfprintf(fid,'%12.5f\n',Qx(i,j)); elsefprintf(fid,'%12.5f\t',Qx(i,j)); endendendfprintf(fid,'\n');fprintf(fid,'输出单位权中误差\n'); [m,n]=size(u); %u for i=1:1:m for j=1:1:nif j==nfprintf(fid,'%12.5f\n',u(i,j));elsefprintf(fid,'%12.5f\t',u(i,j));endendendfprintf(fid,'\n');fclose(fid);case 4% 具有参数的条件平差% 具有参数的条件方程式BV+AX+W=0sprintf('4:具有参数的条件平差BV+AX+W=0 ')n=input('请输入n=');t=input('请输入t=');r=input('请输入r=');B=input('请输入条件方程中V的系数矩阵B=');A=input('请输入条件方程中X的系数矩阵A=');W=input('请输入条件方程自由向量W=');P=input('请输入权阵P=');sprintf('开始计算')N=(B*inv(P)*B')M=(A'*inv(N)*A)X=-inv(M)*A'*inv(N)*WK=-inv(N)*(A*X+W)V=inv(P)*B'*KQx=inv(M)u=sqrt((V'*P*V)/(r-t))fid=fopen('data.txt','wt'); %写入文件路径,文件输出fprintf(fid,'=====================具有参数的条件平差: BV+AX+W=0 ================================\n');fprintf(fid,'输出n,t,r\n') ;fprintf(fid,'%12.5f %12.5f %12.5f\n',n ,t,r) ; %n,t,rfprintf(fid,'输出矩阵B\n');[m,n]=size(B); %Bfor i=1:1:mfor j=1:1:nif j==nfprintf(fid,'%12.5f\n',B(i,j));elsefprintf(fid,'%12.5f\t',B(i,j)); endendendfprintf(fid,'\n');fprintf(fid,'输出矩阵A\n'); [m,n]=size(A); %Afor i=1:1:mfor j=1:1:nif j==nfprintf(fid,'%12.5f\n',A(i,j)); elsefprintf(fid,'%12.5f\t',A(i,j)); endendendfprintf(fid,'\n');fprintf(fid,'输出矩阵W\n'); [m,n]=size(W); %Wfor i=1:1:mfor j=1:1:nif j==nfprintf(fid,'%12.5f\n',W(i,j)); elsefprintf(fid,'%12.5f\t',W(i,j)); endendendfprintf(fid,'\n');fprintf(fid,'输出矩阵P\n');[m,n]=size(P); %P for i=1:1:m for j=1:1:nif j==nfprintf(fid,'%12.5f\n',P(i,j)); elsefprintf(fid,'%12.5f\t',P(i,j)); endendendfprintf(fid,'\n');fprintf(fid,'输出矩阵X\n'); [m,n]=size(X); %X for i=1:1:m for j=1:1:nif j==nfprintf(fid,'%12.5f\n',X(i,j)); elsefprintf(fid,'%12.5f\t',X(i,j)); endendendfprintf(fid,'\n');fprintf(fid,'输出矩阵V\n'); [m,n]=size(V); %V for i=1:1:m for j=1:1:nif j==nfprintf(fid,'%12.5f\n',V(i,j)); elsefprintf(fid,'%12.5f\t',V(i,j)); endendendfprintf(fid,'\n');fprintf(fid,'输出矩阵Qx\n'); [m,n]=size(Qx); %Qx for i=1:1:m for j=1:1:nif j==nfprintf(fid,'%12.5f\n',Qx(i,j)); elsefprintf(fid,'%12.5f\t',Qx(i,j)); endendendfprintf(fid,'\n');fprintf(fid,'输出单位权中误差\n'); [m,n]=size(u); %u for i=1:1:m for j=1:1:nif j==nfprintf(fid,'%12.5f\n',u(i,j));elsefprintf(fid,'%12.5f\t',u(i,j));endendendfprintf(fid,'\n');fclose(fid);otherwisedisp('代号输入有误')end。
MATLAB简单程序大全

MATLAB简单程序大全求特征值特征向量A=[2 3 4;1 5 9;8 5 2]det(A)A'rank(A)inv(A)rref(A)eig(A)%求特征值和特征向量卫星运行问题h=200,H=51000,R=6378;a=(h+H+2*R)/2;c=(H-h)/2;b=(a^2-c^2)^(1/2);e=c/a;f=sqrt(1-exp(2).*cos(t)^2);l=int(f,t,0,pi/2)L=4*a.*l动态玫瑰线n=3;N=10000;theta=2*pi*(0:N)/N;r=cos(n*theta);x=r.*cos(theta);y=r.*sin(theta);comet(x,y)二重积分syms x yf=x^2*sin(y);int(int(f,x,0,1),y,0,pi)ezmesh(f,[0,1,0,pi])函数画图syms x;f=exp(-0.2*x)*sin(0.5*x);ezplot(f,[0,8*pi])玫瑰线theta=0:0.01:2*pi;r=cos(3*theta);polar(theta,r,'r')求x^2+y^2=1和x^2+z^2=1所围成的体积syms x y z Rr=1;Z=sqrt(1-x^2);y0=Z;V=8*int(int(Z,y,0,y0),x,0,1)求导数及图像f='1/(5+4*cos(x))';subplot(1,2,1);ezplot(f)f1=diff(f)subplot(1,2,2);ezplot(f1)绕x轴旋转t=(0:20)*pi/10;r=exp(-.2*t).*sin(.5*t);theta=t;x=t'*ones(size(t));y=r'*cos(theta);z=r'*sin(theta);mesh(x,y,z)colormap([0 0 0])某年是否闰年year=input('input year:=');n1=year/4;n2=year/100;n3=year/400;if n1==fix(n1)&n2~=fix(n2)disp('是闰年')elseif n1==fix(n1)&n3==fix(n3) disp('是闰年')elsedisp('不是闰年')End玫瑰线的绘制theta=0:0.001:2*pi;rho=2*cos(3*theta);figure(1);polar(theta,rho)x=rho.*cos(theta);y=rho.*sin(theta);figure(2);comet(x,y)相遇问题function k=moto(A,B)if nargin==0,A=0;B=100;end va=10;vb=8;vc=60;f=1;k=0;while(B-A)>0.2if f==1tk=(B-A)/(vb+vc);elsetk=(B-A)/(vc+va);endA=A+va*tk;B=B-vb*tk;f=-f;k=k+1;End数学实验syms x; x=1:100;f(x)=exp(-0.2*x).*sin(0.5*x); syms x; x=1:100;f(x)=exp(-0.2*x).*sin(0.5*x); plot(x,f(x))syms x; x=0:0.1:2*pi;f(x)=exp(-0.2*x)*sin(0.5*x);plot(x,f(x))syms x; x=0:0.1:2*pi;f(x)=exp(-0.2*x).*sin(0.5*x);plot(x,f(x))syms x; x=0:0.1:2*pi;f(x)=exp(-0.2*x).*sin(0.5*x); zplot(x,f(x))clcf=sym('sin(x)/x');limit(f)f1=sym('(1+a/x)^x')limit(f1)f1=sym('(1+a/x)^x'),x=inf;limit(f1)clcf1=sym('(1+1/x)^x');limit(f1,'inf')f1=sym('(1+1/x)^x');limit(f1,'inf')clcf1=sym('(1+1/x)^x');limit(f1,'x',inf)Clc相遇问题A=0;B=100;va=10;vb=8;vc=60;f=1;k=0;plot(A,0,'ro',B,0,'go'),hold on while(B-A)>0.2if f==1tk=(B-A)/(vb+vc);elsetk=(B-A)/(vc+va);endA=A+va*tk;B=B-vb*tk;plot(A,0,'R.',B,0,'g.'),pause(1)f=-f;k=k+1;endk,tk,A,B数学实验f=[0 9 13.5;0.1 0 0;0 0.2 0]x=[100;100;100]y=xfor n=1:3x=f*xy=[y,x]endyfigure(1),bar(y(1,:))figure(2),bar(y(2,:))figure(3),bar(y(3,:))[p,d]=eig(f)a=p(:,1)b=a/sum(a)x1=b*300y1=x1for n=1:3x1=f*x1y1=[y1,x1]endy1figure(4),bar(y1(1,:))figure(5),bar(y1(2,:))figure(6),bar(y1(3,:))杨辉三角function Y=yang(n)if nargin==0,n=3;endY=eye(n);Y(:,1)=ones(n,1);for k=3:nY(k,2:k-1)=Y(k-1,1:k-2)+Y(k-1,2:k-1);End实例2 寻找水仙花数一、实验内容数153有一个特殊的性质,即153=1^3+5^3+3^3,那么有没有其他这样的数呢?我们怎样利用MATLAB程序设计快速寻求这些水仙花数呢?二、实验分析把任一数各位数字的立方和求出,再求所得的各位数字的立方和,依次进行下去,总可以得到一个水仙花数.如把76按这样的运算进行下去:333333333333+=++=++=+++= 76559,559979,9791801,1801514, 333333333333++=++=++=++= 514190,190730,730370,370370结果得到一个水仙花数370。
一些简单的算法MATLAB代码

一些简单的算法MATLAB代码以下是一些简单的MATLAB代码示例:1. 二分查找算法(Binary Search Algorithm):```function index = binarySearch(arr, low, high, target) if low > highindex = -1;return;endmid = floor((low + high) / 2);if arr(mid) == targetindex = mid;elseif arr(mid) > targetindex = binarySearch(arr, low, mid - 1, target);elseindex = binarySearch(arr, mid + 1, high, target);endend```2. 冒泡排序算法(Bubble Sort Algorithm):```function sortedArr = bubbleSort(arr)n = length(arr);for i = 1:n-1for j = 1:n-iif arr(j) > arr(j+1)temp = arr(j);arr(j) = arr(j+1);arr(j+1) = temp;endendendsortedArr = arr;end```3. 素数判断算法(Prime Number Check Algorithm):```function isPrime = isPrimeNumber(n)if n <= 1isPrime = false;return;endfor i = 2:sqrt(n)if mod(n, i) == 0isPrime = false;return;endendisPrime = true;end```4. 进制转换算法(Number Base Conversion Algorithm):```function result = decToBase(n, base)result = "";while n > 0remainder = mod(n, base);if base > 10 && remainder > 9remainder = char(chron(remainder - 10) + 'A');endresult = string(remainder) + result;n = floor(n / base);endend```5. 斐波那契数列算法(Fibonacci Sequence Algorithm):```function fib = fibonacci(n)if n <= 0fib = [];return;elseif n == 1fib = [1];return;endfib = [1, 1];for i = 3:nfib(i) = fib(i-1) + fib(i-2); endend```。
Matlab100个实例程序

程序代码:(代码标记[code]...[/code] ) 1-32是:图形应用篇33-66是:界面设计篇67-84是:图形处理篇85-100是:数值分析篇实例1:三角函数曲线(1)function shili01h0=figure('toolbar','none',...'position',[198****0300],...'name','实例01');h1=axes('parent',h0,...'visible','off');x=-pi:0.05:pi;y=sin(x);plot(x,y);xlabel('自变量X');ylabel('函数值Y');title('SIN( )函数曲线');grid on实例2:三角函数曲线(2)function shili02h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例02');x=-pi:0.05:pi;y=sin(x)+cos(x);plot(x,y,'-*r','linewidth',1);grid onxlabel('自变量X');ylabel('函数值Y');title('三角函数');实例3:图形的叠加function shili03h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例03');x=-pi:0.05:pi;y1=sin(x);y2=cos(x);plot(x,y1,...'-*r',...x,y2,...'--og');grid onxlabel('自变量X');ylabel('函数值Y');title('三角函数');实例4:双y轴图形的绘制function shili04h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例04');x=0:900;a=1000;b=0.005;y1=2*x;y2=cos(b*x);[haxes,hline1,hline2]=plotyy(x,y1,x,y2,'semilogy','plot'); axes(haxes(1))ylabel('semilog plot');axes(haxes(2))ylabel('linear plot');实例5:单个轴窗口显示多个图形function shili05h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例05');t=0:pi/10:2*pi;[x,y]=meshgrid(t);subplot(2,2,1)plot(sin(t),cos(t))axis equalsubplot(2,2,2)z=sin(x)-cos(y);plot(t,z)axis([0 2*pi -2 2])subplot(2,2,3)h=sin(x)+cos(y);plot(t,h)axis([0 2*pi -2 2])subplot(2,2,4)g=(sin(x).^2)-(cos(y).^2);plot(t,g)axis([0 2*pi -1 1])实例6:图形标注function shili06h0=figure('toolbar','none',...'position',[200 150 450 400],...'name','实例06');t=0:pi/10:2*pi;h=plot(t,sin(t));xlabel('t=0到2\pi','fontsize',16);ylabel('sin(t)','fontsize',16);title('\it{从0to2\pi 的正弦曲线}','fontsize',16) x=get(h,'xdata');y=get(h,'ydata');imin=find(min(y)==y);imax=find(max(y)==y);text(x(imin),y(imin),...['\leftarrow最小值=',num2str(y(imin))],... 'fontsize',16)text(x(imax),y(imax),...['\leftarrow最大值=',num2str(y(imax))],...'fontsize',16)实例7:条形图形function shili07h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例07');tiao1=[562 548 224 545 41 445 745 512];tiao2=[47 48 57 58 54 52 65 48];t=0:7;bar(t,tiao1)xlabel('X轴');ylabel('TIAO1值');h1=gca;h2=axes('position',get(h1,'position'));plot(t,tiao2,'linewidth',3)set(h2,'yaxislocation','right','color','none','xticklabel',[])实例8:区域图形function shili08h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例08');x=91:95;profits1=[88 75 84 93 77];profits2=[51 64 54 56 68];profits3=[42 54 34 25 24];profits4=[26 38 18 15 4];area(x,profits1,'facecolor',[0.5 0.9 0.6],...'edgecolor','b',...'linewidth',3)hold onarea(x,profits2,'facecolor',[0.9 0.85 0.7],...'edgecolor','y',...'linewidth',3)hold onarea(x,profits3,'facecolor',[0.3 0.6 0.7],... 'edgecolor','r',...'linewidth',3)hold onarea(x,profits4,'facecolor',[0.6 0.5 0.9],... 'edgecolor','m',...'linewidth',3)hold offset(gca,'xtick',[91:95])set(gca,'layer','top')gtext('\leftarrow第一季度销量')gtext('\leftarrow第二季度销量')gtext('\leftarrow第三季度销量')gtext('\leftarrow第四季度销量')xlabel('年','fontsize',16);ylabel('销售量','fontsize',16);实例9:饼图的绘制function shili09h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例09');t=[54 21 35;68 54 35;45 25 12;48 68 45;68 54 69];x=sum(t);h=pie(x);textobjs=findobj(h,'type','text');str1=get(textobjs,{'string'});val1=get(textobjs,{'extent'});oldext=cat(1,val1{:});names={'商品一:';'商品二:';'商品三:'}; str2=strcat(names,str1);set(textobjs,{'string'},str2)val2=get(textobjs,{'extent'});newext=cat(1,val2{:});offset=sign(oldext(:,1)).*(newext(:,3)-oldext(:,3))/2; pos=get(textobjs,{'position'});textpos=cat(1,pos{:});textpos(:,1)=textpos(:,1)+offset;set(textobjs,{'position'},num2cell(textpos,[3,2]))实例10:阶梯图function shili10h0=figure('toolbar','none',...'position',[200 150 450 400],...'name','实例10');a=0.01;b=0.5;t=0:10;f=exp(-a*t).*sin(b*t);stairs(t,f)hold onplot(t,f,':*')hold offglabel='函数e^{-(\alpha*t)}sin\beta*t的阶梯图'; gtext(glabel,'fontsize',16)xlabel('t=0:10','fontsize',16)axis([0 10 -1.2 1.2])实例11:枝干图function shili11h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例11');x=0:pi/20:2*pi;y1=sin(x);y2=cos(x);h1=stem(x,y1+y2);hold onh2=plot(x,y1,'^r',x,y2,'*g');h3=[h1(1);h2];legend(h3,'y1+y2','y1=sin(x)','y2=cos(x)') xlabel('自变量X');ylabel('函数值Y');title('正弦函数与余弦函数的线性组合');实例12:罗盘图function shili12h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例12');winddirection=[54 24 65 84256 12 235 62125 324 34 254];windpower=[2 5 5 36 8 12 76 14 10 8];rdirection=winddirection*pi/180;[x,y]=pol2cart(rdirection,windpower); compass(x,y);desc={'风向和风力','北京气象台','10月1日0:00到','10月1日12:00'};gtext(desc)实例13:轮廓图function shili13h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例13');[th,r]=meshgrid((0:10:360)*pi/180,0:0.05:1); [x,y]=pol2cart(th,r);z=x+i*y;f=(z.^4-1).^(0.25);contour(x,y,abs(f),20)xlabel('实部','fontsize',16);ylabel('虚部','fontsize',16);h=polar([0 2*pi],[0 1]);delete(h)hold oncontour(x,y,abs(f),20)实例14:交互式图形function shili14h0=figure('toolbar','none',...'position',[200 150 450 250],... 'name','实例14');axis([0 10 0 10]);hold onx=[];y=[];n=0;disp('单击鼠标左键点取需要的点'); disp('单击鼠标右键点取最后一个点'); but=1;while but==1[xi,yi,but]=ginput(1);plot(xi,yi,'bo')n=n+1;disp('单击鼠标左键点取下一个点'); x(n,1)=xi;y(n,1)=yi;endt=1:n;ts=1:0.1:n;xs=spline(t,x,ts);ys=spline(t,y,ts);plot(xs,ys,'r-');hold off实例15:变换的傅立叶函数曲线function shili15h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例15');axis equalm=moviein(20,gcf);set(gca,'nextplot','replacechildren')h=uicontrol('style','slider','position',... [100 10 500 20],'min',1,'max',20) for j=1:20plot(fft(eye(j+16)))set(h,'value',j)m(:,j)=getframe(gcf);endclf;axes('position',[0 0 1 1]);movie(m,30)实例16:劳伦兹非线形方程的无序活动function shili15h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例15');axis equalm=moviein(20,gcf);set(gca,'nextplot','replacechildren')h=uicontrol('style','slider','position',... [100 10 500 20],'min',1,'max',20) for j=1:20plot(fft(eye(j+16)))set(h,'value',j)m(:,j)=getframe(gcf);endclf;axes('position',[0 0 1 1]);movie(m,30)实例17:填充图function shili17h0=figure('toolbar','none',...'position',[200 150 450 250],... 'name','实例17');t=(1:2:15)*pi/8;x=sin(t);y=cos(t);fill(x,y,'r')axis square offtext(0,0,'STOP',...'color',[1 1 1],...'fontsize',50,...'horizontalalignment','center')实例18:条形图和阶梯形图function shili18h0=figure('toolbar','none',...'position',[200 150 450 250],... 'name','实例18');subplot(2,2,1)x=-3:0.2:3;y=exp(-x.*x);bar(x,y)title('2-D Bar Chart')subplot(2,2,2)x=-3:0.2:3;y=exp(-x.*x);bar3(x,y,'r')title('3-D Bar Chart')subplot(2,2,3)x=-3:0.2:3;y=exp(-x.*x);stairs(x,y)title('Stair Chart')subplot(2,2,4)x=-3:0.2:3;y=exp(-x.*x);barh(x,y)title('Horizontal Bar Chart')实例19:三维曲线图function shili19h0=figure('toolbar','none',...'position',[200 150 450 400],... 'name','实例19');subplot(2,1,1)x=linspace(0,2*pi);y1=sin(x);y2=cos(x);y3=sin(x)+cos(x);z1=zeros(size(x));z2=0.5*z1;z3=z1;plot3(x,y1,z1,x,y2,z2,x,y3,z3) grid onxlabel('X轴');ylabel('Y轴');zlabel('Z轴');title('Figure1:3-D Plot')subplot(2,1,2)x=linspace(0,2*pi);y1=sin(x);y2=cos(x);y3=sin(x)+cos(x);z1=zeros(size(x));z2=0.5*z1;z3=z1;plot3(x,z1,y1,x,z2,y2,x,z3,y3) grid onxlabel('X轴');zlabel('Z轴');title('Figure2:3-D Plot')实例20:图形的隐藏属性function shili20h0=figure('toolbar','none',...'position',[200 150 450 300],... 'name','实例20');subplot(1,2,1)[x,y,z]=sphere(10);mesh(x,y,z)axis offtitle('Figure1:Opaque')hidden onsubplot(1,2,2)[x,y,z]=sphere(10);mesh(x,y,z)axis offtitle('Figure2:Transparent') hidden off实例21:PEAKS函数曲线function shili21h0=figure('toolbar','none',...'position',[200 100 450 450],... 'name','实例21');[x,y,z]=peaks(30);subplot(2,1,1)x=x(1,:);y=y(:,1);i=find(y>0.8&y<1.2);j=find(x>-0.6&x<0.5);z(i,j)=nan*z(i,j);surfc(x,y,z)xlabel('X轴');ylabel('Y轴');title('Figure1:surfc函数形成的曲面')subplot(2,1,2)x=x(1,:);y=y(:,1);i=find(y>0.8&y<1.2);j=find(x>-0.6&x<0.5);z(i,j)=nan*z(i,j);surfl(x,y,z)xlabel('X轴');ylabel('Y轴');zlabel('Z轴');title('Figure2:surfl函数形成的曲面')实例22:片状图function shili22h0=figure('toolbar','none',...'position',[200 150 550 350],...'name','实例22');subplot(1,2,1)x=rand(1,20);y=rand(1,20);z=peaks(x,y*pi);t=delaunay(x,y);trimesh(t,x,y,z)hidden offtitle('Figure1:Triangular Surface Plot');subplot(1,2,2)x=rand(1,20);y=rand(1,20);z=peaks(x,y*pi);t=delaunay(x,y);trisurf(t,x,y,z)title('Figure1:Triangular Surface Plot');实例23:视角的调整function shili23h0=figure('toolbar','none',...'position',[200 150 450 350],... 'name','实例23');x=-5:0.5:5;[x,y]=meshgrid(x);r=sqrt(x.^2+y.^2)+eps;z=sin(r)./r;subplot(2,2,1)surf(x,y,z)xlabel('X-axis')ylabel('Y-axis')zlabel('Z-axis')title('Figure1')view(-37.5,30)subplot(2,2,2)surf(x,y,z)xlabel('X-axis')ylabel('Y-axis')zlabel('Z-axis')title('Figure2')view(-37.5+90,30)subplot(2,2,3)surf(x,y,z)xlabel('X-axis')ylabel('Y-axis')zlabel('Z-axis')title('Figure3')view(-37.5,60)subplot(2,2,4)surf(x,y,z)xlabel('X-axis')ylabel('Y-axis')zlabel('Z-axis')title('Figure4')view(180,0)实例24:向量场的绘制function shili24h0=figure('toolbar','none',...'position',[200 150 450 350],... 'name','实例24');subplot(2,2,1)z=peaks;ribbon(z)title('Figure1')subplot(2,2,2)[x,y,z]=peaks(15);[dx,dy]=gradient(z,0.5,0.5); contour(x,y,z,10)hold onquiver(x,y,dx,dy)hold offtitle('Figure2')subplot(2,2,3)[x,y,z]=peaks(15);[nx,ny,nz]=surfnorm(x,y,z);surf(x,y,z)hold onquiver3(x,y,z,nx,ny,nz)hold offtitle('Figure3')subplot(2,2,4)x=rand(3,5);y=rand(3,5);z=rand(3,5);c=rand(3,5);fill3(x,y,z,c)grid ontitle('Figure4')实例25:灯光定位function shili25h0=figure('toolbar','none',...'position',[200 150 450 250],... 'name','实例25');vert=[1 1 1;1 2 1;2 2 1;2 1 1;1 1 2;12 2;2 2 2;2 1 2];fac=[1 2 3 4;2 6 7 3;4 3 7 8;15 8 4;1 2 6 5;5 6 7 8];grid offsphere(36)h=findobj('type','surface');set(h,'facelighting','phong',...'facecolor',...'interp',...'edgecolor',[0.4 0.4 0.4],...'backfacelighting',...'lit')hold onpatch('faces',fac,'vertices',vert,... 'facecolor','y');light('position',[1 3 2]);light('position',[-3 -1 3]); material shinyaxis vis3d offhold off实例26:柱状图function shili26h0=figure('toolbar','none',...'position',[200 50 450 450],...'name','实例26');subplot(2,1,1)x=[5 2 18 7 39 8 65 5 54 3 2];bar(x)xlabel('X轴');ylabel('Y轴');title('第一子图');subplot(2,1,2)y=[5 2 18 7 39 8 65 5 54 3 2];barh(y)xlabel('X轴');ylabel('Y轴');title('第二子图');实例27:设置照明方式function shili27h0=figure('toolbar','none',...'position',[200 150 450 350],... 'name','实例27');subplot(2,2,1)sphereshading flatcamlight leftcamlight rightlighting flatcolorbaraxis offtitle('Figure1')subplot(2,2,2)sphereshading flatcamlight leftcamlight rightlighting gouraudcolorbaraxis offtitle('Figure2')subplot(2,2,3)sphereshading interpcamlight rightcamlight leftlighting phongcolorbaraxis offtitle('Figure3')subplot(2,2,4)sphereshading flatcamlight leftcamlight rightlighting nonecolorbaraxis offtitle('Figure4')实例28:羽状图function shili28h0=figure('toolbar','none',...'position',[200 150 450 350],... 'name','实例28');subplot(2,1,1)alpha=90:-10:0;r=ones(size(alpha));m=alpha*pi/180;n=r*10;[u,v]=pol2cart(m,n);feather(u,v)title('羽状图')axis([0 20 0 10])subplot(2,1,2)t=0:0.5:10;x=0.05+i;y=exp(-x*t);feather(y)title('复数矩阵的羽状图')实例29:立体透视(1)function shili29h0=figure('toolbar','none',...'position',[200 150 450 250],... 'name','实例29');[x,y,z]=meshgrid(-2:0.1:2,...-2:0.1:2,...-2:0.1:2);v=x.*exp(-x.^2-y.^2-z.^2); grid onfor i=-2:0.5:2;h1=surf(linspace(-2,2,20),...linspace(-2,2,20),...zeros(20)+i);rotate(h1,[1 -1 1],30)dx=get(h1,'xdata');dy=get(h1,'ydata');dz=get(h1,'zdata');delete(h1)slice(x,y,z,v,[-2 2],2,-2)hold onslice(x,y,z,v,dx,dy,dz)hold offaxis tightview(-5,10)drawnowend实例30:立体透视(2)function shili30h0=figure('toolbar','none',...'position',[200 150 450 250],... 'name','实例30');[x,y,z]=meshgrid(-2:0.1:2,...-2:0.1:2,...-2:0.1:2);v=x.*exp(-x.^2-y.^2-z.^2); [dx,dy,dz]=cylinder;slice(x,y,z,v,[-2 2],2,-2)for i=-2:0.2:2h=surface(dx+i,dy,dz);rotate(h,[1 0 0],90)xp=get(h,'xdata');yp=get(h,'ydata');zp=get(h,'zdata');delete(h)hold onhs=slice(x,y,z,v,xp,yp,zp);axis tightxlim([-3 3])view(-10,35)drawnowdelete(hs)hold offend实例31:表面图形function shili31h0=figure('toolbar','none',...'position',[200 150 550 250],...'name','实例31');subplot(1,2,1)x=rand(100,1)*16-8;y=rand(100,1)*16-8;r=sqrt(x.^2+y.^2)+eps;z=sin(r)./r;xlin=linspace(min(x),max(x),33); ylin=linspace(min(y),max(y),33); [X,Y]=meshgrid(xlin,ylin);Z=griddata(x,y,z,X,Y,'cubic'); mesh(X,Y,Z)axis tighthold onplot3(x,y,z,'.','Markersize',20)subplot(1,2,2)k=5;n=2^k-1;theta=pi*(-n:2:n)/n;phi=(pi/2)*(-n:2:n)'/n;X=cos(phi)*cos(theta);Y=cos(phi)*sin(theta);Z=sin(phi)*ones(size(theta)); colormap([0 0 0;1 1 1])C=hadamard(2^k);surf(X,Y,Z,C)axis square实例32:沿曲线移动的小球h0=figure('toolbar','none',...'position',[198****8468],... 'name','实例32');h1=axes('parent',h0,...'position',[0.15 0.45 0.7 0.5],... 'visible','on');t=0:pi/24:4*pi;y=sin(t);plot(t,y,'b')n=length(t);h=line('color',[0 0.5 0.5],...'linestyle','.',...'markersize',25,...'erasemode','xor');k1=uicontrol('parent',h0,...'style','pushbutton',...'position',[80 100 50 30],...'string','开始',...'callback',[...'i=1;',...'k=1;,',...'m=0;,',...'while 1,',...'if k==0,',...'break,',...'end,',...'if k~=0,',...'set(h,''xdata'',t(i),''ydata'',y(i)),',...'drawnow;,',...'i=i+1;,',...'if i>n,',...'m=m+1;,',...'i=1;,',...'end,',...'end,',...'end']);k2=uicontrol('parent',h0,...'style','pushbutton',...'position',[180 100 50 30],...'string','停止',...'callback',[...'k=0;,',...'set(e1,''string'',m),',...'p=get(h,''xdata'');,',...'q=get(h,''ydata'');,',...'set(e2,''string'',p);,',...'set(e3,''string'',q)']);k3=uicontrol('parent',h0,...'style','pushbutton',...'position',[280 100 50 30],... 'string','关闭',...'callback','close');e1=uicontrol('parent',h0,...'style','edit',...'position',[60 30 60 20]);t1=uicontrol('parent',h0,...'style','text',...'string','循环次数',...'position',[60 50 60 20]);e2=uicontrol('parent',h0,...'style','edit',...'position',[180 30 50 20]);t2=uicontrol('parent',h0,...'style','text',...'string','终点的X坐标值',...'position',[155 50 100 20]);e3=uicontrol('parent',h0,...'style','edit',...'position',[300 30 50 20]);t3=uicontrol('parent',h0,...'style','text',...'string','终点的Y坐标值',...'position',[275 50 100 20]);实例33:曲线转换按钮h0=figure('toolbar','none',...'position',[200 150 450 250],... 'name','实例33');x=0:0.5:2*pi;y=sin(x);h=plot(x,y);grid on'if i==1,',...'i=0;,',...'y=cos(x);,',...'delete(h),',...'set(hm,''string'',''正弦函数''),',...'h=plot(x,y);,',...'grid on,',...'else if i==0,',...'i=1;,',...'y=sin(x);,',...'set(hm,''string'',''余弦函数''),',...'delete(h),',...'h=plot(x,y);,',...'grid on,',...'end,',...'end'];hm=uicontrol(gcf,'style','pushbutton',... 'string','余弦函数',...'callback',huidiao);i=1;set(hm,'position',[250 20 60 20]);set(gca,'position',[0.2 0.2 0.6 0.6]) title('按钮的使用')hold on实例34:栅格控制按钮h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例34');x=0:0.5:2*pi;y=sin(x);plot(x,y)huidiao1=[...'set(h_toggle2,''value'',0),',...'grid on,',...];'set(h_toggle1,''value'',0),',...'grid off,',...];h_toggle1=uicontrol(gcf,'style','togglebutton',... 'string','grid on',...'value',0,...'position',[20 45 50 20],...'callback',huidiao1);h_toggle2=uicontrol(gcf,'style','togglebutton',... 'string','grid off',...'value',0,...'position',[20 20 50 20],...'callback',huidiao2);set(gca,'position',[0.2 0.2 0.6 0.6])title('开关按钮的使用')实例35:编辑框的使用h0=figure('toolbar','none',...'position',[200 150 350 250],...'name','实例35');f='Please input the letter';huidiao1=[...'g=upper(f);,',...'set(h2_edit,''string'',g),',...];huidiao2=[...'g=lower(f);,',...'set(h2_edit,''string'',g),',...];h1_edit=uicontrol(gcf,'style','edit',...'position',[100 200 100 50],...'HorizontalAlignment','left',...'string','Please input the letter',...'callback','f=get(h1_edit,''string'');',...'background','w',...'max',5,...'min',1);h2_edit=uicontrol(gcf,'style','edit',...'HorizontalAlignment','left',...'position',[100 100 100 50],...'background','w',...'max',5,...'min',1);h1_button=uicontrol(gcf,'style','pushbutton',... 'string','小写变大写',...'position',[100 45 100 20],...'callback',huidiao1);h2_button=uicontrol(gcf,'style','pushbutton',... 'string','大写变小写',...'position',[100 20 100 20],...'callback',huidiao2);实例36:弹出式菜单h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例36');x=0:0.5:2*pi;y=sin(x);h=plot(x,y);grid onhm=uicontrol(gcf,'style','popupmenu',...'string',...'sin(x)|cos(x)|sin(x)+cos(x)|exp(-sin(x))',... 'position',[250 20 50 20]);set(hm,'value',1)huidiao=[...'v=get(hm,''value'');,',...'switch v,',...'case 1,',...'delete(h),',...'y=sin(x);,',...'h=plot(x,y);,',...'grid on,',...'case 2,',...'delete(h),',...'y=cos(x);,',...'h=plot(x,y);,',...'grid on,',...'case 3,',...'delete(h),',...'y=sin(x)+cos(x);,',...'h=plot(x,y);,',...'grid on,',...'case 4,',...'delete(h),',...'y=exp(-sin(x));,',...'h=plot(x,y);,',...'grid on,',...'end'];set(hm,'callback',huidiao)set(gca,'position',[0.2 0.2 0.6 0.6]) title('弹出式菜单的使用')实例37:滑标的使用h0=figure('toolbar','none',...'position',[200 150 450 250],... 'name','实例37');[x,y]=meshgrid(-8:0.5:8);r=sqrt(x.^2+y.^2)+eps;z=sin(r)./r;h0=mesh(x,y,z);h1=axes('position',...[0.2 0.2 0.5 0.5],...'visible','off');htext=uicontrol(gcf,...'units','points',...'position',[20 30 45 15],...'string','brightness',...'style','text');hslider=uicontrol(gcf,...'units','points',...'position',[10 10 300 15],...'min',-1,...'max',1,...'style','slider',...'callback',...'brighten(get(hslider,''value''))');实例38:多选菜单h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例38');[x,y]=meshgrid(-8:0.5:8);r=sqrt(x.^2+y.^2)+eps;z=sin(r)./r;h0=mesh(x,y,z);hlist=uicontrol(gcf,'style','listbox',...'string','default|spring|summer|autumn|winter',... 'max',5,...'min',1,...'position',[20 20 80 100],...'callback',[...'k=get(hlist,''value'');,',...'switch k,',...'case 1,',...'colormap default,',...'case 2,',...'colormap spring,',...'case 3,',...'colormap summer,',...'case 4,',...'colormap autumn,',...'case 5,',...'colormap winter,',...'end']);实例39:菜单控制的使用h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例39');x=0:0.5:2*pi;y=cos(x);h=plot(x,y);grid onset(gcf,'toolbar','none')hm=uimenu('label','example');huidiao1=[...'set(hm_gridon,''checked'',''on''),',...'set(hm_gridoff,''checked'',''off''),',...'grid on'];huidiao2=[...'set(hm_gridoff,''checked'',''on''),',...'set(hm_gridon,''checked'',''off''),',...'grid off'];hm_gridon=uimenu(hm,'label','grid on',... 'checked','on',...'callback',huidiao1);hm_gridoff=uimenu(hm,'label','grid off',... 'checked','off',...'callback',huidiao2);实例40:UIMENU菜单的应用h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例40');h1=uimenu(gcf,'label','函数');h11=uimenu(h1,'label','轮廓图',...'callback',[...'set(h31,''checked'',''on''),',...'set(h32,''checked'',''off''),',...'[x,y,z]=peaks;,',...'contour3(x,y,z,30)']);h12=uimenu(h1,'label','高斯分布',...。
matlab十个简单案例编写

matlab十个简单案例编写1. 求解线性方程组线性方程组是数学中常见的问题之一,而MATLAB提供了用于求解线性方程组的函数。
例如,我们可以使用"linsolve"函数来求解以下线性方程组:2x + 3y = 74x - 2y = 2代码如下所示:A = [2, 3; 4, -2];B = [7; 2];X = linsolve(A, B);disp(X);解释:上述代码定义了一个2x2的矩阵A和一个2x1的矩阵B,分别表示线性方程组的系数矩阵和常数向量。
然后,使用linsolve函数求解线性方程组,结果存储在X中,并通过disp函数打印出来。
运行代码后,可以得到x=2和y=1的解。
2. 求解非线性方程除了线性方程组外,MATLAB还可以用于求解非线性方程。
例如,我们可以使用"fzero"函数求解以下非线性方程:x^2 + 2x - 3 = 0代码如下所示:fun = @(x) x^2 + 2*x - 3;x0 = 0;x = fzero(fun, x0);disp(x);解释:上述代码定义了一个匿名函数fun,表示非线性方程。
然后,使用fzero函数传入fun和初始值x0来求解非线性方程的根,并通过disp函数打印出来。
运行代码后,可以得到x=1的解。
3. 绘制函数图像MATLAB提供了强大的绘图功能,可以帮助我们可视化函数的形状和特征。
例如,我们可以使用"plot"函数绘制以下函数的图像:y = cos(x)代码如下所示:x = linspace(0, 2*pi, 100);y = cos(x);plot(x, y);解释:上述代码首先使用linspace函数生成一个从0到2π的100个等间距点的向量x,然后计算对应的cos值,并存储在向量y中。
最后,使用plot函数将x和y作为横纵坐标绘制出函数图像。
运行代码后,可以看到cos函数的周期性波动图像。
(图论)matlab模板程序

第一讲:图论模型程序一:可达矩阵算法%根据邻接矩阵A〔有向图〕求可达矩阵P〔有向图〕function P=dgraf<A>n=size<A,1>;P=A;for i=2:nP=P+A^i;endP<P~=0>=1; %将不为0的元素变为1P;程序二:无向图关联矩阵和邻接矩阵互换算法F表示所给出的图的相应矩阵W表示程序运行结束后的结果f=0表示把邻接矩阵转换为关联矩阵f=1表示把关联矩阵转换为邻接矩阵%无向图的关联矩阵和邻接矩阵的相互转换function W=incandadf<F,f>if f==0 %邻接矩阵转换为关联矩阵m=sum<sum<F>>/2; %计算图的边数n=size<F,1>;W=zeros<n,m>;k=1;for i=1:nfor j=i:nif F<i,j>~=0W<i,k>=1; %给边的始点赋值为1W<j,k>=1; %给边的终点赋值为1k=k+1;endendendelseif f==1 %关联矩阵转换为邻接矩阵m=size<F,2>;n=size<F,1>;W=zeros<n,n>;for i=1:ma=find<F<:,i>~=0>;W<a<1>,a<2>>=1; %存在边,则邻接矩阵的对应值为1 W<a<2>,a<1>>=1;endelsefprint<'Please imput the right value of f'>;W;程序三:有向图关联矩阵和邻接矩阵互换算法%有向图的关联矩阵和邻接矩阵的转换function W=mattransf<F,f>if f==0 %邻接矩阵转换为关联矩阵m=sum<sum<F>>;n=size<F,1>;W=zeros<n,m>;k=1;for i=1:nfor j=i:nif F<i,j>~=0 %由i发出的边,有向边的始点W<i,k>=1; %关联矩阵始点值为1W<j,k>=-1; %关联矩阵终点值为-1k=k+1;endendendelseif f==1 %关联矩阵转换为邻接矩阵m=size<F,2>;n=size<F,1>;W=zeros<n,n>;for i=1:ma=find<F<:,i>~=0>; %有向边的两个顶点if F<a<1>,i>==1W<a<1>,a<2>>=1; %有向边由a<1>指向a<2>elseW<a<2>,a<1>>=1; %有向边由a<2>指向a<1>endendelsefprint<'Please imput the right value of f'>;endW;第二讲:最短路问题程序0:最短距离矩阵W表示图的权值矩阵D表示图的最短距离矩阵%连通图中各项顶点间最短距离的计算function D=shortdf<W>%对于W<i,j>,若两顶点间存在弧,则为弧的权值,否则为inf;当i=j时W<i,j>=0 n=length<W>;m=1;while m<=nfor i=1:nfor j=1:nif D<i,j>>D<i,m>+D<m,j>D<i,j>+D<i,m>+D<m,j>; %距离进行更新 endendendm=m+1;endD;程序一:Dijkstra算法〔计算两点间的最短路〕function [l,z]=Dijkstra<W>n = size <W,1>;for i = 1 :nl<i>=W<1,i>;z<i>=0;endi=1;while i<=nfor j =1 :nif l<i>>l<j>+W<j,i>l<i>=l<j>+W<j,i>;z<i>=j-1;if j<ii=j-1;endendendi=i+1;end程序二:floyd算法〔计算任意两点间的最短距离〕function [d,r]=floyd<a>n=size<a,1>;d=a;for i=1:nfor j=1:nr<i,j>=j;endendr;for k=1:nfor i=1:nfor j=1:nif d<i,k>+d<k,j><d<i,j>d<i,j>=d<i,k>+d<k,j>; r<i,j>=r<i,k>;endendendend程序三:n2short.m 计算指定两点间的最短距离function [P u]=n2short<W,k1,k2>n=length<W>;U=W;m=1;while m<=nfor i=1:nfor j=1:nif U<i,j>>U<i,m>+U<m,j>U<i,j>=U<i,m>+U<m,j>;endendendm=m+1;endu=U<k1,k2>;P1=zeros<1,n>;k=1;P1<k>=k2;V=ones<1,n>*inf;kk=k2;while kk~=k1for i=1:nV<1,i>=U<k1,kk>-W<i,kk>;if V<1,i>==U<k1,i>P1<k+1>=i;kk=i;k=k+1;endendendk=1;wrow=find<P1~=0>;for j=length<wrow>:-1:1P<k>=P1<wrow<j>>;k=k+1;endP;程序四、n1short.m<计算某点到其它所有点的最短距离> function[Pm D]=n1short<W,k>n=size<W,1>;D=zeros<1,n>;for i=1:n[P d]=n2short<W,k,i>;Pm{i}=P;D<i>=d;end程序五:pass2short.m<计算经过某两点的最短距离> function [P d]=pass2short<W,k1,k2,t1,t2>[p1 d1]=n2short<W,k1,t1>;[p2 d2]=n2short<W,t1,t2>;[p3 d3]=n2short<W,t2,k2>;dt1=d1+d2+d3;[p4 d4]=n2short<W,k1,t2>;[p5 d5]=n2short<W,t2,t1>;[p6 d6]=n2short<W,t1,k2>;dt2=d4+d5+d6;if dt1<dt2d=dt1;P=[p1 p2<2:length<p2>> p3<2:length<p3>>]; elsed=dt1;p=[p4 p5<2:length<p5>> p6<2:length<p6>>]; endP;d;第三讲:最小生成树程序一:最小生成树的Kruskal算法function [T c]=krusf<d,flag>if nargin==1n=size<d,2>;m=sum<sum<d~=0>>/2;b=zeros<3,m>;k=1;for i=1:nfor j=<i+1>:nif d<i,j>~=0b<1,k>=i;b<2,k>=j;b<3,k>=d<i,j>;k=k+1;endendendelseb=d;endn=max<max<b<1:2,:>>>;m=size<b,2>;[B,i]=sortrows<b',3>;B=B';c=0;T=[];k=1;t=1:n;for i=1:mif t<B<1,i>>~=t<B<2,i>>T<1:2,k>=B<1:2,i>;c=c+B<3,i>;k=k+1;tmin=min<t<B<1,i>>,t<B<2,i>>>; tmax=max<t<B<1,i>>,t<B<2,i>>>; for j=1:nif t<j>==tmaxt<j>=tmin;endendendif k==nbreak;endendT;c;程序二:最小生成树的Prim算法function [T c]=Primf<a>l=length<a>;a<a==0>=inf;k=1:l;listV<k>=0;listV<1>=1;e=1;while <e<l>min=inf;for i=1:lif listV<i>==1for j=1:lif listV<j>==0 & min>a<i,j>min=a<i,j>;b=a<i,j>;s=i;d=j;endendendendlistV<d>=1;distance<e>=b;source<e>=s;destination<e>=d;e=e+1;endT=[source;destination];for g=1:e-1c<g>=a<T<1,g>,T<2,g>>;endc;第四讲:Euler图和Hamilton图程序一:Fleury算法〔在一个Euler图中找出Euler环游〕注:包括三个文件;fleuf1.m, edf.m, flecvexf.mfunction [T c]=fleuf1<d>%注:必须保证是Euler环游,否则输出T=0,c=0n=length<d>;b=d;b<b==inf>=0;b<b~=0>=1;m=0;a=sum<b>;eds=sum<a>/2;ed=zeros<2,eds>;vexs=zeros<1,eds+1>;matr=b;for i=1:nif mod<a<i>,2>==1m=m+1;endendif m~=0fprintf<'there is not exit Euler path.\n'>T=0;c=0;endif m==0vet=1;flag=0;t1=find<matr<vet,:>==1>;for ii=1:length<t1>ed<:,1>=[vet,t1<ii>];vexs<1,1>=vet;vexs<1,2>=t1<ii>;matr<vexs<1,2>,vexs<1,1>>=0;flagg=1;tem=1;while flagg[flagg ed]=edf<matr,eds,vexs,ed,tem>;tem=tem+1;if ed<1,eds>~=0 & ed<2,eds>~=0T=ed;T<2,eds>=1;c=0;for g=1:edsc=c+d<T<1,g>,T<2,g>>;endflagg=0;break;endendendendfunction[flag ed]=edf<matr,eds,vexs,ed,tem>flag=1;for i=2:eds[dvex f]=flecvexf<matr,i,vexs,eds,ed,tem>;if f==1flag=0;break;endif dvex~=0ed<:,i>=[vexs<1,i> dvex];vexs<1,i+1>=dvex;matr<vexs<1,i+1>,vexs<1,i>>=0;elsebreak;endendfunction [dvex f]=flecvexf<matr,i,vexs,eds,ed,temp> f=0;edd=find<matr<vexs<1,i>,:>==1>;dvex=0;dvex1=[];ded=[];if length<edd>==1dvex=edd;elsedd=1;dd1=0;kkk=0;for kk=1:length<edd>m1=find<vexs==edd<kk>>;if sum<m1>==0dvex1<dd>=edd<kk>;dd=dd+1;dd1=1;elsekkk=kkk+1;endendif kkk==length<edd>tem=vexs<1,i>*ones<1,kkk>;edd1=[tem;edd];for l1=1:kkklt=0;ddd=1;for l2=1:edsif edd1<1:2,l1>==ed<1:2,l2>lt=lt+1;endendif lt==0ded<ddd>=edd<l1>;ddd=ddd+1;endendendif temp<=length<dvex1>dvex=dvex1<temp>;elseif temp>length<dvex1> & temp<=length<ded>dvex=ded<temp>;elsef=1;endend程序二:Hamilton改良圈算法〔找出比较好的Hamilton路〕function [C d1]= hamiltonglf<v>%d表示权值矩阵%C表示算法最终找到的Hamilton圈.%v =[ 51 67;37 84;41 94;2 99;18 54;4 50;24 42;25 38;13 40;7 64;22 60;25 62;18 40;41 26];n=size<v,1>;subplot<1,2,1>hold on;plot <v<:,1>,v<:,2>,'*'>; %描点for i=1:nstr1='V';str2=num2str<i>;dot=[str1,str2];text<v<i,1>-1,v<i,2>-2,dot>; %给点命名endplot <v<:,1>,v<:,2>>;%连线plot<[v<n,1>,v<1,1>],[v<n,2>,v<1,2>]>;for i =1:nfor j=1:nd<i,j>=sqrt<<v<i,1>-v<j,1>>^2+<v<i,2>-v<j,2>>^2>;endendd2=0;for i=1:nif i<nd2=d2+d<i,i+1>;elsed2=d2+d<n,1>;endendtext<10,30,num2str<d2>>;n=size<d,2>;C=[linspace<1,n,n> 1];for nnn=1:20C1=C;if n>3for m=4:n+1for i=1:<m-3>for j=<i+2>:<m-1>if<d<C<i>,C<j>>+d<C<i+1>,C<j+1>><d<C<i>,C<i+1>>+d<C<j>,C<j+1>>>C1<1:i>=C<1:i>;for k=<i+1>:jC1<k>=C<j+i+1-k>;endC1<<j+1>:m>=C<<j+1>:m>;endendendendelseif n<=3if n<=2fprint<'It does not exist Hamilton circle.'>; elsefprint<'Any cirlce is the right answer.'>;endendC=C1;d1=0;for i=1:nd1=d1+d<C<i>,C<i+1>>;endd1;endsubplot<1,2,2>;hold on;plot <v<:,1>,v<:,2>,'*'>; %描点for i=1:nstr1='V';str2=num2str<i>;dot=[str1,str2];text<v<i,1>-1,v<i,2>-2,dot>; %给点命名endv2=[v;v<1,1>,v<1,2>];plot<v<C<:>,1>,v<C<:>,2>,'r'>;text<10,30,num2str<d1>>;第五讲:匹配问题与算法程序一:较大基础匹配算法function J=matgraf<W>n=size<W,1>;J=zeros<n,n>;while sum<sum<W>>~=0a=find<W~=0>;t1=mod<a<1>,n>;if t1==0t1=n;endif a<1>/n>floor<a<1>/n>t2=floor<a<1>/n>+1;elset2=floor<a<1>/n>;endJ<t1,t2>=1,J<t2,t1>=1;W<t1,:>=0;W<t2,:>=0;W<:,t1>=0;W<:,t2>=0;endJ;程序二:匈牙利算法〔完美匹配算法,包括三个文件fc01,fc02,fc03〕function [e,s]=fc01<a,flag>if nargin==1flag=0;endb=a;if flag==0cmax=max<max<b>'>;b=cmax-b;endm=size<b>;for i =1:m<1>b<i,:>=b<i,:>-min<b<i,:>>;endfor j=1:m<2>b<:,j>=b<:,j>-min<b<:,j>>;endd=<b==0>;[e,total]=fc02<d>;while total~=m<1>b=fc03<b,e>;d=<b==0>;[e,total]=fc02<d>;endinx=sub2ind<size<a>,e<:,1>,e<:,2>>;e=[e,a<inx>];s=sum<a<inx>>;function [e,total]=fc02<d>total=0;m=size<d>;e=zeros<m<1>,2>;t=sum<sum<d>'>;nump=sum<d'>;while t~=0[s,inp]=sort<nump>;inq=find<s>;ep=inp<inq<1>>;inp=find<d<ep,:>>;numq=sum<d<:,inp>>;[s,inq]=sort<numq>;eq=inp<inq<1>>;total=total+1;e<total,:>=[ep,eq];inp=find<d<:,eq>>;nump<inp>=nump<inp>-1;nump<ep>=0;t=t-sum<d<ep,:>>-sum<d<:,eq>>+1;d<ep,:>=0*d<ep,:>;d<:,eq>=0*d<:,eq>;endfunction b=fc03<b,e>m=size<b>;t=1;p=ones<m<1>,1>;q=zeros<m<1>,1>;inp=find<e<:,1>~=0>;p<e<inp,1>>=0;while t~=0tp=sum<p+q>;inp=find<p==1>;n=size<inp>;for i=1:n<1>inq=find<b<inp<i>,:>==0>;q<inq>=1;endinp=find<q==1>;n=size<inp>;for i=1:n<1>if all<e<:,2>-inp<i>>==0inq=find<<e<:,2>-inp<i>>==0>;p<e<inq>>=1;endendtq=sum<p+q>;t=tq-tp;endinp=find<p==1>;inq=find<q==0>;cmin=min<min<b<inp,inq>>'>;inq=find<q==1>;b<inp,:>=b<inp,:>-cmin;b<:,inq>=b<:,inq>+cmin;第六讲:最大流最小费用问题程序一:2F算法<Ford-Fulkerson算法>,求最大流%C=[0 5 4 3 0 0 0 0;0 0 0 0 5 3 0 0;0 0 0 0 0 3 2 0;0 0 0 0 0 0 2 0; %0 0 0 0 0 0 0 4;0 0 0 0 0 0 0 3;0 0 0 0 0 0 0 5;0 0 0 0 0 0 0 0 ] function [f wf]=fulkersonf<C,f1>%C表示容量%f1表示当前流量,默认为0%f表示最大流±íʾ×î´óÁ÷%wf表示最大流的流量n=length<C>;if nargin==1;f=zeros<n,n>;elsef=f1;endNo=zeros<1,n>;d=zeros<1,n>;while <1>No<1>=n+1;d<1>=Inf;while <1>pd=1;for <i=1:n>if <No<i>>for <j=1:n>if <No<j>==0 & f<i,j><C<i,j>>No<j>=i;d<j>=C<i,j>-f<i,j>;pd=0;if <d<j>>d<i>>d<j>=d<i>;endelseif <No<j>==0 & f<j,i>>0>No<j>=-i;d<j>=f<j,i>;pd=0;if <d<j>>d<i>>d<j>=d<i>;endendendendendif <No<n>|pd>break;endendif <pd>break;enddvt=d<n>;t=n;while <1>if<No<t>>0>f<No<t>,t>=f<No<t>,t>+dvt;elseif <No<t><0>f<No<t>,t>=f<No<t>,t>-dvt;endif <No<t>==1>for <i=1:n>No<i>=0;d<i>=0;endbreakendt=No<t>;endendwf=0;for <j=1:n>wf=wf+f<1,j>;endf;wf;程序二:Busacker-Gowan算法<求最大流最小费用>%C=[0 15 16 0 0;0 0 0 13 14;0 11 0 17 0;0 0 0 0 8;0 0 0 0 0] %b=[0 4 1 0 0;0 0 0 6 1;0 2 0 3 0;0 0 0 0 2;0 0 0 0 0]%function [f wf zwf]=BGf<C,b>%C表示弧容量矩阵%b表示弧上单位流量的费用%f表示最大流最小费用矩阵%wf最大流量%zwf表示最小费用n=size<C,2>;wf=0;wf0=inf;f=zeros<n,n>;while <1>a=ones<n,n>*inf;for <i=1:n>a<i,i>=0;endfor <i=1:n>for <j=1:n>if<C<i,j>>0 & f<i,j>==0>a<i,j>=b<i,j>;elseif <C<i,j>>0 & f<i,j>==C<i,j>>a<j,i>=-b<i,j>;elseif <C<i,j>>0>a<i,j>=b<i,j>;a<j,i>=-b<i,j>;endendendfor <i=2:n>p<i>=inf;s<i>=i;endfor <k=1:n>pd=1;for <i=2:n>for <j=1:n>if <p<i>>p<j>+a<j,i>>p<i>=p<j>+a<j,i>;s<i>=j;pd=0; endendendif <pd>break;endendif <p<n>==inf>break;enddvt=inf;t=n;while <1>if <a<s<t>,t>>0>dvtt=C<s<t>,t>-f<s<t>,t>;elseif <a<s<t>,t><0>dvtt=f<t,s<t>>;endif <dvt>dvtt>dvt=dvtt;endif <s<t>==1>break;endt=s<t>;endpd=0;if <wf+dvt>=wf0>dvt=wf0-wf;pd=1;endt=n;while <1>if <a<s<t>,t>>0>f<s<t>,t>=f<s<t>,t>+dvt; elseif <a<s<t>,t><0>f<<t>,s<t>>=f<t,s<t>>-dvt; endif <s<t>==1>break;endt=s<t>;endif <pd>break;endwf=0;for <j=1:n>wf=wf+f<1,j>;endendzwf=0;for <i=1:n>for <j=1:n>zwf=zwf+b<i,j>*f<i,j>;endendf;。
matlab经典代码大全

哈哈哈MATLAB显示正炫余炫图:plot(x,y1,'* r',x,y2,'o b')定义【0,2π;t=0:pi/10:2*pi; 定义函数文件:function [返回变量列表]=函数名(输入变量列表) 顺序结构:选择结构1)if-else-end 语句其格式为:if 逻辑表达式程序模块 1 ;else程序模块 2 ;End图片读取:%选择图片路径[filename, pathname] = ... uigetfile({'*.jpg';'*.bmp';'*.gif'},'选择图片');% 合成路径+ 文件名str=[pathname,filename];%为什么pathname 和filename 要前面出现的位置相反才能运行呢???%读取图片im=imread(str);% 使用图片axes(handles.axes1);% 显示图片imshow(im);边缘检测:global imstr=get(hObject,'string');axes (handles.axes1);switch strcase ' 原图' imshow(im);case 'sobel'BW = edge(rgb2gray(im),'sobel'); imshow(BW);case 'prewitt'BW = edge(rgb2gray(im),'prewitt');imshow(BW);case 'canny'BW = edge(rgb2gray(im),'canny');imshow(BW); Canny 算子边缘定位精确性和抗噪声能力效果较好,是一个折中方案end;开闭运算:se=[1,1,1;1,1,1;1,1,1;1,1,1]; %Structuring ElementI=rgb2gray(im); imshow(I,[]);title('Original Image');I=double(I); [im_height,im_width]=size(I); [se_height,se_width]=size(se); halfheight=floor(se_height/2);halfwidth=floor(se_width/2);[se_origin]=floor((size(se)+1)/2); image_dilation=padarray(I,se_origin,0,'both'); %Image to be used for dilation image_erosion=padarray(I,se_origin,256,'both'); %Image to be used forerosion %%%%%%%%%%%%%%%%%%%%% Dilation %%%%%%%%%%%%%%%%%%%%%for k=se_origin(1)+1:im_height+se_origin(1)for kk=se_origin(2)+1:im_width+se_origin(2)dilated_image(k-se_origin(1),kk-se_origin(2))=max(max(se+image_dilation(k-se_origin(1):k+halfh eight-1,kk-se_origin(2):kk+halfwidth-1)));endend figure;imshow(dilated_image,[]);title('Image after Dilation'); %%%%%%%%%%%%%%%%% %%% Erosion %%%%%%%%%%%%%%%%%%%%se=se';for k=se_origin(2)+1:im_height+se_origin(2)for kk=se_origin(1)+1:im_width+se_origin(1)eroded_image(k-se_origin(2),kk-se_origin(1))=min(min(image_erosion(k-se_origin(2):k+halfwidth -1,kk-se_origin(1):kk+halfheight-1)-se));endend figure;imshow(eroded_image,[]);title('Image afterErosion'); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% Opening(Erosion first, thenDilation) %%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%se=se';image_dilation2=eroded_image; %Image to be used for dilationfor k=se_origin(1)+1:im_height-se_origin(1)for kk=se_origin(2)+1:im_width-se_origin(2)opening_image(k-se_origin(1),kk-se_origin(2))=max(max(se+image_dilation2(k-se_origin(1):k+hal fheight-1,kk-se_origin(2):kk+halfwidth-1)));endend figure;imshow(opening_image,[]);title('OpeningImage'); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% Closing(Dilation first, then Erosion) %%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% se=se';image_erosion2=dilated_image; %Image to be used for erosionfor k=se_origin(2)+1:im_height-se_origin(2)for kk=se_origin(1)+1:im_width-se_origin(1)closing_image(k-se_origin(2),kk-se_origin(1))=min(min(image_erosion2(k-se_origin(2):k+halfwidt h-1,kk-se_origin(1):kk+halfheight-1)-se));endend figure;imshow(closing_image,[]);title('Closing Image'); Warning: Image is too big to fit on screen; displaying at 31% scale.> In truesize>Resize1 at 308In truesize at 44In imshow at 161 图像的直方图归一化:I=imread(‘red.bmp');%读入图像figure;%打开新窗口[M,N]=size(I);%计算图像大小[counts,x]=imhist(I,32);%计算有32 个小区间的灰度直方图counts=counts/M/N;%计算归一化灰度直方图各区间的值stem(x,counts);%绘制归一化直方图图像平移:I=imread('shuichi.jpg');se=translate(strel(1),[180 190]);B=imdilate(I,se); figure;subplot(1,2,1),subimage(I);title('原图像'); subplot(1,2,2),subimage(B);title('平移后图像');图像的转置;A=imread('nir.bmp'); tform=maketform('affine',[0 1 0;1 0 0;0 0 1]);B=imtransform(A,tform,'nearest');figure;imshow(A);figure;imshow(B);imwrite(B,'nir 转置后图像.bmp');图像滤波:B = imfilter(A,H,option1,option2,...)或写作g = imfilter(f, w, filtering_mode, boundary_options, size_options)其中,f 为输入图像,w 为滤波掩模,g 为滤波后图像。
matlab经典编程例题30道

MATLAB是一款功能强大的数学软件,其编程功能也受到越来越多人的关注。
下面介绍的是30个matlab经典编程例题,可以帮助大家熟悉matlab的编程语法,提高matlab 编程技能。
1. 请编写一个程序,计算出两个数的和。
2. 请编写一个程序,计算出两个数的最大值和最小值。
3. 请编写一个程序,计算出一组数据的平均值和标准差。
4. 请编写一个程序,将一个矩阵转置。
5. 请编写一个程序,求出两个矩阵的乘积。
6. 请编写一个程序,求出一个矩阵的逆矩阵。
7. 请编写一个程序,求出一个矩阵的行列式。
8. 请编写一个程序,计算出一元二次方程的解。
9. 请编写一个程序,计算出两个数组的相似度。
10. 请编写一个程序,计算出一个矩阵的特征值和特征向量。
11. 请编写一个程序,求出两个矩阵的秩。
12. 请编写一个程序,求出一个矩阵的特定元素。
13. 请编写一个程序,求出一组数据的最高值和最低值。
14. 请编写一个程序,求出两个数组的交集。
15. 请编写一个程序,求出一个矩阵的行和列之和。
16. 请编写一个程序,使用循环语句计算出100以内所有奇数的和。
17. 请编写一个程序,使用循环语句计算出1到1000以内的和。
18. 请编写一个程序,使用递归函数计算出斐波那契数列的第n项。
19. 请编写一个程序,求出一个多项式的导数。
20. 请编写一个程序,求出一个函数的极值点。
21. 请编写一个程序,求出一个数组的非零元素个数。
22. 请编写一个程序,计算出函数的不定积分。
23. 请编写一个程序,计算出函数的定积分。
24. 请编写一个程序,求出一个矩阵的秩。
25. 请编写一个程序,求出函数的极限值。
26. 请编写一个程序,求出一个矩阵的特征值分解。
27. 请编写一个程序,求出一个矩阵的LU分解。
28. 请编写一个程序,求出一个矩阵的QR分解。
29. 请编写一个程序,求出三次多项式的根。
30. 请编写一个程序,求出一个函数的积分。
matlab 矩阵运算程序

matlab 矩阵运算程序MATLAB是一种强大的数学软件,主要用于数值计算、算法开发、数据可视化和数据分析等。
在MATLAB中,矩阵运算是非常常见的操作。
以下是一个简单的MATLAB矩阵运算程序示例:```matlab创建两个矩阵A和BA = [1, 2, 3;4, 5, 6;7, 8, 9];B = [9, 8, 7;6, 5, 4;3, 2, 1];矩阵加法C = A + B;disp('矩阵A和矩阵B的和:');disp(C);矩阵减法D = A - B;disp('矩阵A和矩阵B的差:'); disp(D);矩阵乘法E = A * B;disp('矩阵A和矩阵B的乘积:'); disp(E);矩阵转置T = transpose(A);disp('矩阵A的转置:');disp(T);求矩阵的行列式det_A = det(A);disp('矩阵A的行列式:');disp(det_A);求矩阵的逆矩阵inv_A = inv(A);disp('矩阵A的逆矩阵:');disp(inv_A);求矩阵的秩rank_A = rank(A);disp('矩阵A的秩:');disp(rank_A);求矩阵的特征值eig_A = eig(A);disp('矩阵A的特征值:');disp(eig_A);```以上程序演示了MATLAB中的一些基本矩阵运算,如加法、减法、乘法、转置、求行列式、求逆矩阵、求秩和求特征值等。
您可以根据实际需求修改矩阵A和B的值,然后运行该程序以观察结果。
需要注意的是,这里的矩阵运算都是在MATLAB环境下进行的。
如果要编写比MATLAB更快的矩阵运算程序,可以尝试使用如C、C++等编程语言,并链接到高性能的数学库,如Intel的Math Kernel Library(MKL)。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
MATLAB简单程序大全求特征值特征向量A=[2 3 4;1 5 9;8 5 2]det(A)A'rank(A)inv(A)rref(A)eig(A)%求特征值和特征向量卫星运行问题h=200,H=51000,R=6378;a=(h+H+2*R)/2;c=(H-h)/2;b=(a^2-c^2)^(1/2);e=c/a;f=sqrt(1-exp(2).*cos(t)^2);l=int(f,t,0,pi/2)L=4*a.*l动态玫瑰线n=3;N=10000;theta=2*pi*(0:N)/N;r=cos(n*theta);x=r.*cos(theta);y=r.*sin(theta);comet(x,y)二重积分syms x yf=x^2*sin(y);int(int(f,x,0,1),y,0,pi)ezmesh(f,[0,1,0,pi])函数画图syms x;f=exp(-0.2*x)*sin(0.5*x);ezplot(f,[0,8*pi])玫瑰线theta=0:0.01:2*pi;r=cos(3*theta);polar(theta,r,'r')求x^2+y^2=1和x^2+z^2=1所围成的体积syms x y z Rr=1;Z=sqrt(1-x^2);y0=Z;V=8*int(int(Z,y,0,y0),x,0,1)求导数及图像f='1/(5+4*cos(x))';subplot(1,2,1);ezplot(f)f1=diff(f)subplot(1,2,2);ezplot(f1)绕x轴旋转t=(0:20)*pi/10;r=exp(-.2*t).*sin(.5*t);theta=t;x=t'*ones(size(t));y=r'*cos(theta);z=r'*sin(theta);mesh(x,y,z)colormap([0 0 0])某年是否闰年year=input('input year:=');n1=year/4;n2=year/100;n3=year/400;if n1==fix(n1)&n2~=fix(n2)disp('是闰年')elseif n1==fix(n1)&n3==fix(n3)disp('是闰年')elsedisp('不是闰年')End玫瑰线的绘制theta=0:0.001:2*pi;rho=2*cos(3*theta);figure(1);polar(theta,rho)x=rho.*cos(theta);y=rho.*sin(theta);figure(2);comet(x,y)相遇问题function k=moto(A,B)if nargin==0,A=0;B=100;end va=10;vb=8;vc=60;f=1;k=0;while(B-A)>0.2if f==1tk=(B-A)/(vb+vc);elsetk=(B-A)/(vc+va);endA=A+va*tk;B=B-vb*tk;f=-f;k=k+1;End数学实验syms x; x=1:100;f(x)=exp(-0.2*x).*sin(0.5*x); syms x; x=1:100;f(x)=exp(-0.2*x).*sin(0.5*x); plot(x,f(x))syms x; x=0:0.1:2*pi;f(x)=exp(-0.2*x)*sin(0.5*x); plot(x,f(x))syms x; x=0:0.1:2*pi;f(x)=exp(-0.2*x).*sin(0.5*x);plot(x,f(x))syms x; x=0:0.1:2*pi;f(x)=exp(-0.2*x).*sin(0.5*x); zplot(x,f(x))clcf=sym('sin(x)/x');limit(f)f1=sym('(1+a/x)^x')limit(f1)f1=sym('(1+a/x)^x'),x=inf;limit(f1)clcf1=sym('(1+1/x)^x');limit(f1,'inf')f1=sym('(1+1/x)^x');limit(f1,'inf')clcf1=sym('(1+1/x)^x');limit(f1,'x',inf)Clc相遇问题A=0;B=100;va=10;vb=8;vc=60;f=1;k=0;plot(A,0,'ro',B,0,'go'),hold on while(B-A)>0.2if f==1tk=(B-A)/(vb+vc);elsetk=(B-A)/(vc+va);endA=A+va*tk;B=B-vb*tk;plot(A,0,'R.',B,0,'g.'),pause(1)f=-f;k=k+1;endk,tk,A,B数学实验f=[0 9 13.5;0.1 0 0;0 0.2 0]x=[100;100;100]y=xfor n=1:3x=f*xy=[y,x]endyfigure(1),bar(y(1,:))figure(2),bar(y(2,:))figure(3),bar(y(3,:))[p,d]=eig(f)a=p(:,1)b=a/sum(a)x1=b*300y1=x1for n=1:3x1=f*x1y1=[y1,x1]endy1figure(4),bar(y1(1,:))figure(5),bar(y1(2,:))figure(6),bar(y1(3,:))杨辉三角function Y=yang(n)if nargin==0,n=3;endY=eye(n);Y(:,1)=ones(n,1);for k=3:nY(k,2:k-1)=Y(k-1,1:k-2)+Y(k-1,2:k-1);End实例2 寻找水仙花数一、实验内容数153有一个特殊的性质,即153=1^3+5^3+3^3,那么有没有其他这样的数呢?我们怎样利用MATLAB程序设计快速寻求这些水仙花数呢?二、实验分析把任一数各位数字的立方和求出,再求所得的各位数字的立方和,依次进行下去,总可以得到一个水仙花数.如把76按这样的运算进行下去:33333333333376559,559979,9791801,1801514,+=++=++=+++=333333333333514190,190730,730370,370370++=++=++=++=结果得到一个水仙花数370。
三、实验程序(寻求水仙花数经典的程序)for a=1:9for b=0:9;for c=0:9;if a^3+b^3+c^3==a*100+b*10+c;sxh=a*100+b*10+cendendendend四、实验结果sxh =153,370, 371, 407。
实例4 住房贷款的等额本息还款法计算一、实验内容当前在银行放贷项目中,住房按揭贷款占相当大的比重。
这一贷款促进了房地产事业的迅速发展,它不但改善了居民的居住条件,也促进了建筑行业、钢铁行业和其他相关行业的发展,与此同时,也创造了大量的就业机会。
在这中间,银行利率起了巨大的杠杆作用,过高的利率会使按揭购房减少,房价下跌;过低的利率会使按揭贷款购房者数量增加,当房源供不应求时,受供求关系的影响,房价将上涨。
假设某购房者向银行贷款的金额为p0,银行的月利率为a ,贷款期限为 n 个月,求每月的还款金额为r.二、实验分析我们用等比级数求解:第一月末贷款的本息之和为(1)0(1)p p a r =+-,第二月末贷款的本息之和为:2(2)(1)(1)0(1)(1)p p a r p a r a r =+-=+-+-,第三月末贷款的本息之和为32(3)(2)(1)0(1)(1)(1)p p a r p a r a r a r =+-=+-+-+-, 第n 月末贷款的本息之和为1()(1)(1)0(1)[(1)...(1)1]n n p n p n a r p a r a a -=-+-=+-+++++, 考虑第n 个月还清贷款,则p(n)=0,求得0*(1)/[(1)1]n n r p a a a =++-.假设银行贷款20万,月利率0.465%,借款期限为10年,即为120个月,问每月应还金额和10年内共计支付多少利息。
三、实验程序n=120;p0=2e+6;a=0.00465;r=p0*a*(1+a)^n/((1+a)^n-1) %每月还款金额interest=r*n-p0 %10年内应支付的利息四、实验结果r =2.1785e+004, 即每个月还款2178.5元。
interest =6.1415e+005,即10应支付的利息为61415元。