数学实验与Matlab
5. 常用矩阵函数 6. 数据的简单分析 ◆
【 rand('seed',1);A=rand(3,6),
Asort=sort(A), Amax=max(A), Asum=sum(A) 】
5
1.2.3 Matlab工作环境和编程
2.Matlab的基本设计
1.3应用、思考与练习
1.3.1 关系矩阵 1.3.2 投入产出 1.3.3 循环比赛的名次
【 A=[0 1 1 0; 0 0 1 1; 0 0 0 1; 1 0 0 0],
e=ones(4,1); c=A*e;
s=c' 】
★ 画矩阵结构图的gplot指令。
◆(3)
【 clf, A=[0 1 1 0;0 0 1 1;0 0 0 1;1 0 0 0]; xy=[0 1;0 0;-1 –0.5;1 –0.5];
graphy_plot(A,xy,1,0.5), % gplot(A,xy) 】
1.3.4 参考程序
graphy_plot.m
【 function y=graphy_plot(A,xy,l,p)
%画矩阵的有向结构图。 A为邻接矩阵,xy为顶点坐标,l控制参数,l=0,画无向图;
5
数学实验与Matlab
%l~=0,画有向图。p为控制箭头大小的参数。
6
a=-max(abs(xy(:,1)))*1.1;b=max(abs(xy(:,1)))*1.1; c=-max(abs(xy(:,2)))*1.1;d=max(abs(xy(:,2)))*1.1; if l=0
gplot(A,xy),axis([a b c d]),hold on, elseif l~=0
U=[];V=[];X=[];Y=[]; n=length(A(:,1)) ; for i=1:n
k=find(A(i,:)~=0); m=length(k); if(m~=0) for j=1:m
u(1)=(xy(k(j),1)-xy(i,1)); v(1)=(xy(k(j),2)-xy(i,2)); u(2)=eps; v(2)=eps; U=[u;U];V=[v;V];
X=[[xy(i,1) xy(k(j),1)];X]; Y=[[xy(i,2) xy(k(j),2)];Y]; end
text(xy(i,1),xy(i,2),['\\bullet\\leftarrow\\fontsize{16}\\it{V}',? um2str(i)]); hold on, end end
gplot(A,xy),axis([a b c d]),hold on,
h=quiver(X,Y,U,V,p);set(h,'color','red');hold on, plot(xy(:,1),xy(:,2),'k.','markersize',12),hold on, end , hold off 】
实验2.函数的可视化与Matlab作
2.1 实验与观察:函数的可视化
6
数学实验与Matlab
7
2.1.1 Matlab二维绘图命令
1.周期函数与线性p-周期函数 ◆ 观察 :
【 clf, x=linspace(0,8*pi,100);
F=inline('sin(x+cos(x+sin(x)))');
y1=sin(x+cos(x+sin(x))); y2=0.2*x+sin(x+cos(x+sin(x))); plot(x,y1,'k:',x,y2,'k-')
legend('sin(x+cos(x+sin(x))','0.2x+sin(x+cos(x+sin(x)))',2) 】
2. plot指令:绘制直角坐标的二维曲线 3. 图形的属性设置和屏幕控制
【 h=plot([0:0.1:2*pi],sin([0:0.1:2*pi])); grid on
set(h,'LineWidth',5,'color','red'); set(gca,'GridLineStyle','-','fontsize',16) 】
◆设置y坐标的刻度并加以说明,并改变字体的大小。 【 h=plot([0:0.1:2*pi],sin([0:0.1:2*pi]));grid on,
set(gca,'ytick',[-1 -0.5 0 0.5 1]), set(gca,'yticklabel','a|b|c|d|e'), set(gca,'fontsize',20) 】 4. 文字标注指令
【 plot(x,y1,'b',x,y2,'k-') ,
set(gca,'fontsize',15,'fontname','times New Roman'), %设置轴对象的字体
为times
% New Roman,字体的大小为15
title(' \\it{Peroid and linear peroid function}'); %加标题,注意文字用单引号' '
加上
%斜杠'\\'后可输入不同的设置,例如it{…}表示花括号里的文字为斜体;如果有多项设置, %则可用\\…\\…\\…连续输入。
xlabel('x from 0 to 8*pi it{t}\\'); ylabel('\\it{y}'); %说明坐标轴
7
数学实验与Matlab
text(x(49),y1(50)-0.4,'\\fontsize{15}\\bullet\\leftarrowThe period function {\\itf(x)}');
%在坐标(x(49),y1(50)-0.4)处作文字说明, 各项设置用\隔开。
%\\fontsize{15}\\bullet\\leftarrow的意义依次是:\\字体大小=15 \\ 画圆点 \\左箭头
8
text(x(14),y2(50)+1,'\\fontsize{15}The linear period function {\\itg(x)}\\rightarrow
\\bullet') %与上一语句类似,用右箭头 】
◆观察指令legend和num2str的用法:在
6 Peroid and linear peroid function同一张图上画出y?sin(t)和y?3e?0.5t, 这里t?[0,3?], 并进行适当的标注。
y543210-10510·? The period function f(x)The linear period function g(x)?· zxy2_2.m
【 clf,
t=0:0.1:3*pi;alpha=0:0.1:3*pi;
plot(t,sin(t),'r-');hold on; plot(alpha,3*exp(-0.5*alpha),'k:');
15x202530 图2.5 文字标注
set(gca,'fontsize',15,'fontname','times New Roman'),
xlabel('\\it{t(deg)}');ylabel('\\it{magnitude}');
title(' \\it{sine wave and {\\it{Ae}}^{-\\alpha{\\itt}}wave}'); %注意\\alpha的
意义
text(6,sin(6),'\\fontsize{15}The Value \\it{sin(t)} at
{\\itt}=6\\rightarrow\\bullet', 'HorizontalAlignment','right'), %上面的语句是一整行,如果要写成两行,必须使用续行号 ? ,例如要在
“ bullet',”
%后换行,需写“bullet', ?”后才能换行。
% 'HorizontalAlignment','right' 表示箭头所指的曲线对象在 文字的右边。
text(2,3*exp(-0.5*2),['\\fontsize{15}\\bullet\\leftarrow The Value of \\it{3e}^{-0.5 \\it{t}}=',num2str(3*exp(-0.5*2)),' at \\it{t} =2 ']); %num2str的用法:['string1' ,num2str,'string2'],注意方括号的使用。 %legend('\\itsin(t)','{\\itAe}^{-\\alphat}') % 请结合图形观察此命令的使用 】
8

