一天5个Matlab程序[1]


目录

代码

在matlab的editor(command windows里面输入Editor即可,从Editor转到command windows可以按快捷键ctrl+0)键入代码,保存后运行即可。

1.三角函数曲线

function exmaple_l 
h0=figure('toolbar','none','position',[198 56 350 200],'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.图形叠加

function exmaple_2
h0=figure('tool bar','none','position',[200 150 450 350],'name,'实例03');
x=-pi:0.05:pi;
yl=sin(x);
y2=cos(x);
plot(x,y1,'-*r’,x,y2,'--og');
grid on
xlabel('自变量X');
ylabel('函数值Y');
title('三角函数');

3.双y轴图形的绘制

function shili04
h0-figure('tool bar','none','position',[200 150 450 250],name',实例04');
x=0:900;
a=1000;
b=0.005;
y1=2*x;
y2=cos(b*x);
[haxes,hlinel,hline2]=plotyy(x,y1,x,y2,'semilogy','plot');
axes(haxes(1))
ylabel('semilog plot');
axes(haxes(2))
ylabel('linear plot');

4.区域图形绘制

function example_08
h0=figure('toolbar','none','position',[200,150,450,250],'name','example_08');
x=91:95;
profits1=[88,75,84,93,77];
profits2=[51,65,54,56,68];
profits3=[42,54,35,25,24];
profits4=[26,38,18,15,4];
% area函数将每个矩阵中的同一行的元素相叠加,一个矩阵中有多少行,就有多少个area的颜色
area(x,profits1,'facecolor',[0.5 0.9 0.6],...
    'edgecolor','b','linewidth',3);
hold on 
area(x,profits2,'facecolor',[0.9 0.85 0.7],...
    'edgecolor','y','linewidth',3);
area(x,profits3,'facecolor',[0.3 0.6 0.7],...
    'edgecolor','r','linewidth',3);
area(x,profits4,'facecolor',[0.6 0.5 0.9],...
    'edgecolor','m','linewidth',3);
hold off
set(gca,'xtick',[91:95]);
set(gca,'layer','top');
gtext('\leftarrow第一季度销量');
gtext('\leftarrow第二季度销量');
gtext('\leftarrow第三季度销量');
gtext('\leftarrow第四季度销量');
xlabel('年','fontsize',16);
ylabel('销售量','fontsize',16);

5.灰色预测【统计预测模型】

syms a b
c=[a,b]';
promt='data input:';
%A=input(promt);
A=[94.12,106.66,108.24,116.5,150.8];
B=cumsum(A);
n=length(A);
for i=1:(n-1)
    C(i)=(B(i)+B(i+1))/2;
end
E=[-C;ones(1,n-1)];
D=A;D(1)=[];
D=D';
c=(E*E')\E*D;
c=c';
a=c(1);b=c(2);
F=[];F(1)=A(1);
for i=2:(n+2)
    F(i)=(A(1)-b/a)*exp(-a*(i-1))+b/a;
end
G=[];G(1)=A(1);
for i=2:(n+2)
    G(i)=F(i)-F(i-1);
end
a 
b
G
t1=1985:1989;
t2=1985:1991;
plot(t1,A,'*');
hold on
plot(t2,G,'r');
datacursormode on

文章作者: Roc-May
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Roc-May !
  目录