您的位置:首页 > 编程语言 > MATLAB

有趣的MATLAB小程序

2016-06-28 17:35 288 查看
1.弹性蹦球演示程序

figure(1);%定义函数

axis([-5.1,5,-0.05,1.05]);%绘制二维图形

hold on;%保持当前图形及轴系所有的特性

axis('off');%覆盖坐标刻度,并填充背景

%通过填充绘出台阶及两边的挡板

fill([4.12,4.22,4.22,4.12],[-0.05,-0.05,1.05,1.05],'y');

fill([-5,-3.2,-3.2,-5],[-0.05,-0.05,0,0],'g');

fill([-3.2,-2.8,-2.8,-3.2],[-0.05,-0.05,0.2,0.2],'g');

fill([-3.2,-1.4,-1.4,-3.2],[0.2,0.2,0.25,0.25],'g');

fill([-1.4,-1,-1,-1.4],[0.2,0.2,0.45,0.45],'g');

fill([-1.4,0.4,0.4,-1.4],[0.45,0.45,0.5,0.5],'g');

fill([0.4,0.8,0.8,0.4],[0.45,0.45,0.7,0.7],'g');

fill([0.4,2.0,2.0,0.4],[0.7,0.7,0.75,0.75],'g');

fill([2.0,2.3,2.3,2.0],[-0.05,-0.05,0.75,0.75],'g');

fill([2.3,4.12,4.12,2.3],[-0.05,-0.05,0,0],'g');

%x2=line([-5,5],[0.25,0.25],'color','g','linestyle','-', 'markersize',50)%设置台阶边框线,颜色,擦试方式

%line([-5,5],[0.5,0.5],'color','b','linestyle','-', 'markersize',50)%设置球与地面接触面的颜色,擦试方式

%line([-5,5],[0.75,0.75],'color','b','linestyle','-', 'markersize',50)%设置球与地面接触面的颜色,擦试方式

head=line(-5,1,'color','r','linestyle','.','erasemode','xor', 'markersize',60);%设置小球颜色,大小,线条和擦试方式

%body=line(-5,1,'color','b','linestyle','-','erasemode','none'); %描绘轨迹线

%设置初始条件

while 1

t=4;

dt=0.001;

w=0;

dw=0.001;

w=0;%设置球弹起的初始位置

%设置球弹起的高度

while t<=4.12

    t=dt+t;

if w<=1

    w=dw+w;

else

    w=-1;

end

y=(-w*w)+1;

set(head,'xdata',t,'ydata',y);%设置球的运动

%set(body,'xdata',t,'ydata',y);%描绘轨迹线

drawnow;

end %结束程序

w=0;%设置球弹起的初始位置

%设置球弹起的高度

while t>=2.11

    t=t-dt;

if w<=1

    w=dw+w;

else

    w=-1;

end

y=(-w*w)+1;

set(head,'xdata',t,'ydata',y);%设置球的运动

%set(body,'xdata',t,'ydata',y);%描绘轨迹线

drawnow;

end %结束程序

w=0;%设置球弹起的初始位置

  %设置球弹起的高度

while t>=1.11

    t=t-dt;

if w<=1

    w=dw+w;

else

    w=-1;

end

y=(-w*w)/4+1;

set(head,'xdata',t,'ydata',y);%设置球的运动

%set(body,'xdata',t,'ydata',y);%描绘轨迹线

drawnow;

end %结束程序

w=-0.71;%设置球弹起的初始位置

  %设置球弹起的高度

while t>=-0.62

    t=t-dt;

if w<=1

    w=dw+w;

else

    w=-1;

end

y=(-w*w)/2+1;

set(head,'xdata',t,'ydata',y);%设置球的运动

%set(body,'xdata',t,'ydata',y);%描绘轨迹线

drawnow;

end %结束程序

w=-0.71;%设置球弹起的初始位置

  %设置球弹起的高度

while t>=-2.31

    t=t-dt;

if w<=1

    w=dw+w;

else

    w=-1;

end

y=(-w*w)/2+0.75;

set(head,'xdata',t,'ydata',y);%设置球的运动

%set(body,'xdata',t,'ydata',y);%描绘轨迹线

drawnow;

end %结束程序

w=-0.71;%设置球弹起的初始位置

  %设置球弹起的高度

while t>=-4

    t=t-dt;

if w<=1

    w=dw+w;

else

    w=-1;

end

y=(-w*w)/2+0.5;

set(head,'xdata',t,'ydata',y);%设置球的运动

%set(body,'xdata',t,'ydata',y);%描绘轨迹线

drawnow;

end %结束程序

for i=0:0.01:200%设置延时

    y=i+2;

    if(y>199)

    end

end

end

2.嫦娥奔月演示程序

figure('name','嫦娥一号与月亮、地球关系');%设置标题名字

s1=[0:.01:2*pi];

hold on;axis equal;%建立坐标系

axis off   % 除掉Axes

r1=10;%月亮到地球的平均距离

r2=3;%嫦娥一号到月亮的平均距离

w1=1;%设置月亮公转角速度

w2=12%设置嫦娥一号绕月亮公转角速度

t=0;%初始时刻为0

pausetime=.002;%设置暂停时间

sita1=0;sita2=0;%设置开始它们都在水平线上

set(gcf,'doublebuffer','on') %消除抖动

plot(-20,18,'color','r','marker','.','markersize',40);

text(-17,18,'地球');%对地球进行标识

p1=plot(-20,16,'color','b','marker','.','markersize',20);

text(-17,16,'月亮');%对月亮进行标识

p1=plot(-20,14,'color','w','marker','.','markersize',13);

text(-17,14,'嫦娥一号');%对嫦娥一号进行标识

plot(0,0,'color','r','marker','.','markersize',60);%画地球

plot(r1*cos(s1),r1*sin(s1));%画月亮公转轨道

set(gca,'xlim',[-20 20],'ylim',[-20 20]);

p1=plot(r1*cos(sita1),r1*sin(sita1),'color','b','marker','.','markersize',30);%画月亮初始位置

l1=plot(r1*cos(sita1)+r2*cos(s1),r1*sin(sita1)+r2*sin(s1));%画嫦娥一号绕月亮公转轨道

p2x=r1*cos(sita1)+r2*cos(sita2);p2y=r1*sin(sita1)+r2*sin(sita2);

p2=plot(p2x,p2y,'w','marker','.','markersize',20);%画嫦娥一号的初始位置

orbit=line('xdata',p2x,'ydata',p2y,'color','r');%画嫦娥一号的运动轨迹

while 1

set(p1,'xdata',r1*cos(sita1),'ydata',r1*sin(sita1));%设置月亮的运动过程

set(l1,'xdata',r1*cos(sita1)+r2*cos(s1),'ydata',r1*sin(sita1)+r2*sin(s1));%设置嫦娥一号绕月亮的公转轨道的运动过程

ptempx=r1*cos(sita1)+r2*cos(sita2);ptempy=r1*sin(sita1)+r2*sin(sita2);

set(p2,'xdata',ptempx,'ydata',ptempy);%设置嫦娥一号的运动过程

p2x=[p2x ptempx];p2y=[p2y ptempy];

set(orbit,'xdata',p2x,'ydata',p2y);%设置嫦娥一号运动轨迹的显示过程

sita1=sita1+w1*pausetime;%月亮相对地球转过的角度

sita2=sita2+w2*pausetime;%嫦娥一号相对月亮转过的角度

pause(pausetime);  %暂停一会

drawnow

end

3.曲柄连杆机构充气球动画演示程序

hf=figure('name','打气筒吹气球');

set(hf,'color','g');

axis([-10,10,-4,4]);

hold on

axis off;   %除掉坐标

xa0=-2.5;%活塞左顶点坐标

xa1=-1.8;%活塞右顶点坐标

xb0=-2;%连杆左顶点坐标

xb1=5;%连杆右顶点坐标

x3=5.6;%转轮坐标

y3=0;%转轮坐标

x4=xb1;%设置连杆头的初始位置横坐标

y4=0;%设置连杆头的初始位置纵坐标

x5=xa1;

y5=0;

x6=x3;%设置连轴初始横坐标

y6=0;%设置连轴初始纵坐标

a=0.7;

b=0.7

c=0.7

a1=line([xa0;xa1],[0;0],'color','m','linestyle','-','linewidth',20);   %设置活塞

a8=line([-2.7;2.3],[0.3;0.3],'color','b','linestyle','-','linewidth',5);%设置打气筒

a9=line([-2.6;2.3],[-0.3;-0.3],'color','b','linestyle','-','linewidth',5);%设置打气筒

a10=line([-2.6;-2.6],[-0.1;-0.37],'color','b','linestyle','-','linewidth',5);%设置打气筒

a11=line([2.0;2.0],[0.1;0.37],'color','b','linewidth',5);%设置打气筒

a12=line([2.0;2.0],[-0.1;-0.37],'color','b','linewidth',5);%设置打气筒

a13=line([-2.6;-2.6],[0.1;0.37],'color','b','linestyle','-','linewidth',5);%设置打气筒

a14=line([-2.7;-2.7],[0.1;-0.1],'color','b','linestyle','-','linewidth',9);%设置气筒嘴

a16=line([-3.2;-3.2],[0.1;-0.1],'color','r','linestyle','-','linewidth',25);%设置气筒嘴

a2=line([xb0;xb1],[0;0],'color','m','linewidth',5);%设置连杆

a5=line(x5,y5,'color','black','linestyle','.','markersize',25);%设置连杆活塞连接头

a4=line(x4,y4,'color','black','linestyle','.','markersize',25);%设置连杆连接头

a6=line([xb1;x3],[0;0],'color','b','linestyle','-','linewidth',7);%设置连杆连接轴

a7=line(x3,0,'color','m','linestyle','.','markersize',50);%设置运动中心

a3=line(x3,y3,'color' ,[0.5 0.6 0.3],'linestyle','.','markersize',85);%设置手轮

len1=6.8;%连杆长

len2=0.7;%活塞长   

r=1.3;%运动半径

dd=0.01;

d=-4;

plot(d,0,'color','r','marker','.','markersize',10);

pausetime=.0001

s=0;

ds=1;

t=0;

dt=0.015*pi;

   while t<=15.68

             t=t+dt;

          drawnow;

    lena1=sqrt((len1)^2-(r*sin(2*t))^2);%连杆在运动过程中横轴上的有效长度

    rr1=r*cos(t);%半径在运动过程中横轴上的有效长度

    xaa1=x3-sqrt(len1^2-(sin(2*t)*r)^2)-(r*cos(2*t));%活塞在运动过程中的右顶点坐标位置

    xaa0=xaa1-2;%%活塞在运动过程中的左顶点坐标位置

    x55=x3-cos(2*t)*r;%连杆在运动过程中横坐标位置

    y55=y3-sin(2*t)*r*0.32;%连杆在运动过程中纵坐标位置

    set(a4,'xdata',x55,'ydata',y55);%设置连杆顶点运动

    set(a1,'xdata',[xaa1-0.2;xaa1],'ydata',[0;0]);%设置活塞运动

    set(a2,'xdata',[xaa1;x55],'ydata',[0;y55]);

    set(a5,'xdata',xaa1);%设置活塞与连杆连接头的运动

    set(a6,'xdata',[x55;x3],'ydata',[y55;0]);

       if (sign(y55-y3)>0)

         s=s+ds;

         d=d-dd;

         if s>200

         s=0;

         d=0;

      end

        set(gcf,'doublebuffer','on');%消除震动  

        plot(d,0,'color','r','marker','.','markersize',s);%画气球

        set(gcf,'doublebuffer','on') %消除抖动   

     pause(pausetime);  %暂停一会

     drawnow;

       end

   end

  x=[-8.0 -7.0 -6.0 -5.5 -4.5 -3.5 -3.7 -3  -4 -5.5 -6.5 -7.5 -9.1 -7.3 ];

  y=[-1.8 -1.2 -2.8 -1.5 -2.8 -0.5 -0.7 0.7 0.2 1.5 0.8  1.2 1.9  0.5];

     fill(x,y,'r');   

     text(-6,0,'pa','fontsize',22);

4.打桩机动画演示程序

clear;   %清除工作区

clc;     %清除命令区

figure('name','武工院打桩机);   %设置标题

axis ([0 ,10,0,10]);   %建立坐标系

hold on;

axis off;   %除掉坐标

text(3,9.8,'武工院打桩机','fontsize',20,'color','r');

%画打桩机支架

c1=line([1;5.5],[ 0.15;0.15],'color','k','linewidth',8);

c2=line([1.7;5.2],[ 1.5;0.15],'color','k','linewidth',4);

c3=line([1.8;5.2],[ 0.1;8],'color','k','linewidth',4);

c4=line([1.7;5.2],[ 1.5;8],'color','k','linewidth',2);

c5=line([1.2;1.8],[ 0.5;0.5],'color','k','linewidth',13);

fill([4.9,5.3,5.3,4.9],[8.0,8.0,0.3,0.3],[1,0.1,0.5]);

fill([4.4,4.6,5.6,5.8,5.6,4.6],[8.4,8.3,8.3,8.4,8.0,8.0],[1,0.1,0.5])

%画打桩机运动部分和水泥桩

b1=line([8;8],[1;6],'color','b','linewidth',6);

b2=line([5.5,5.5],[7.9,8],'color','k','linewidth',1);

b3=line([5.3,5.55],[7.9,7.9],'color','k','linewidth',3);

b4=line([5.5,5.5],[7.9,6.3],'color','k','linewidth',3);

b5=line([5.5,5.5],[7,6.6],'color','k','linewidth',10);

b6=line([5.5,5.5],[6.6,6.3],'color','k','linewidth',12);

pausetime=1.6;   %设置暂停时间

pause(pausetime);

%吊装水泥桩

s=0;   

ds=0.01;

pausetime1=.002;

while s<2.5    %水泥桩向左移动

      s=s+ds;

      set(b1,'xdata',[8-s;8-s],'ydata',[1;6]);

      pause(pausetime1);

end

pausetime2=1;

pause(pausetime2);

s=0;   

ds=0.01;

while s<1     %水泥桩向下移动

     s=s+ds;

     set(b1,'xdata',[5.5;5.5],'ydata',[1-s;6-s]);

     pause(pausetime1);

end

pause(pausetime2);

s=0;   

ds=0.01;

while s<1.3    %打桩机运动部分下移与水泥桩顶端相接触

      s=s+ds;

      set(b2,'xdata',[5.5;5.5],'ydata',[7.9-s,8]);

      set(b3,'xdata',[5.3;5.55],'ydata',[7.9-s,7.9-s]);

      set(b4,'xdata',[5.5;5.5],'ydata',[7.9-s,6.3-s]);

      set(b5,'xdata',[5.5;5.5],'ydata',[7-s,6.6-s]);

      set(b6,'xdata',[5.5;5.5],'ydata',[6.6-s,6.3-s]);

      pause(pausetime1);

end

%打桩机开始打桩

pausetime3=.4;

pause(pausetime2);

s=0;   

ds=0.06;  %设定打桩进度

while s<4

a=0;   

da=0.01;

pausetime4=.0002;                  

while a<.4   %两个子循环设置打桩机铁锺上下反复运动

     a=a+da;

       set(b5,'xdata',[5.5;5.5],'ydata',[5.7-s+a,5.3-s+a]);

     pause(pausetime4);

end

a=0;   

while a<.4

     a=a+da;

       set(b5,'xdata',[5.5;5.5],'ydata',[6.1-s-a,5.7-s-a]);

     pause(pausetime4);

end

s=s+ds;

%打桩机运动部分与水泥桩同时向下移动

set(b1,'xdata',[5.5;5.5],'ydata',[0;5-s]);

set(b2,'xdata',[5.5;5.5],'ydata',[6.6-s,8]);

set(b3,'xdata',[5.3;5.55],'ydata',[6.6-s,6.6-s]);

set(b4,'xdata',[5.5;5.5],'ydata',[6.6-s,5-s]);

set(b5,'xdata',[5.5;5.5],'ydata',[5.7-s,5.3-s]);

set(b6,'xdata',[5.5;5.5],'ydata',[5.3-s,5-s]);

pause(pausetime3);

end

5.电路演示程序

figure('name','基本电路的模拟');

axis([-3,12,0,10]);%建立坐标系

hold on  %保持当前图形的所有特性

axis('off'); %关闭所有轴标注和控制

%下面是画电池的过程

fill([-1.5,-1.5,1.5,1.5],[1,5,5,1],[0.5,1,1]);%确定坐标轴范围并填充

fill([-0.5,-0.5,0.5,0.5],[5,5.5,5.5,5],[0,0,0]); %确定坐标轴范围并填充

text(-0.5,1.5,'负极');%在坐标上标注说明文字

text(-0.5,3,'电池'); %在坐标上标注说明文字

text(-0.5,4.5,'正极'); %在坐标上标注说明文字

    %下面是画导电线路的过程

plot([0;0],[5.5;6.7],'color','r','linestyle','-','linewidth',4);%绘制二维图形线竖实心红色

plot([0;4],[6.7;6.7],'color','r','linestyle','-','linewidth',4); %绘制二维图形线  实心红色为导线

a=line([4;5],[6.7;7.7],'color','b','linestyle','-','linewidth',4,'erasemode','xor');%画开关蓝色

plot([5.2;9.2],[6.7;6.7],'color','r','linestyle','-','linewidth',4);%绘制图导线为红色

plot([9.2;9.2],[6.7;3.7],'color','r','linestyle','-','linewidth',4);% 绘制图导线竖线为红线

plot([9.2;9.7],[3.7;3.7],'color','r','linestyle','-','linewidth',4); % 绘制图导线横线为红色

plot([0;0],[1;0],'color','r','linestyle','-','linewidth',4);%如上画红色竖线

plot([0;10],[0;0],'color','r','linestyle','-','linewidth',4);%如上画横线

plot([10;10],[0;3],'color','r','linestyle','-','linewidth',4);%画竖线

%下面是画灯泡的过程

fill([9.8,10.2,9.7,10.3],[3,3,3.3,3.3],[0 0 0]);%确定填充范围

plot([9.7,9.7],[3.3,4.3],'color','b','linestyle','-','linewidth',0.5);%绘制灯泡外形线为蓝色

plot([10.3,10.3],[3.3,4.45],'color','b','linestyle','-','linewidth',0.5); %绘制灯泡外形线为蓝色

%以下为绘圆

x=9.7:pi/50:10.3;%绘圆

plot(x,4.3+0.1*sin(40*pi*(x-9.7)),'color','b','linestyle','-','linewidth',0.5); %绘圆

t=0:pi/60:2*pi; %绘圆

plot(10+0.7*cos(t),4.3+0.6*sin(t),'color','b'); %绘圆

    %下面是箭头及注释的显示

text(4.5,10,'电流运动方向'); %在坐标上标注说明文字

line([4.5;6.6],[9.4;9.4],'color','r','linestyle','-','linewidth',4,'erasemode','xor');%绘制箭头横线

line(6.7,9.4,'color','b','linestyle','>','erasemode','xor','markersize',10);% %绘制箭头三角形

pause(1);

%下面是开关闭合的过程

t=0;

y=7.7;

while y>6.7 %电路总循环控制开关动作条件

    x=4+sqrt(2)*cos(pi/4*(1-t));

    y=6.7+sqrt(2)*sin(pi/4*(1-t));

    set(a,'xdata',[4;x],'ydata',[6.7;y]);

    drawnow;

    t=t+0.1;

end

%下面是开关闭合后模拟大致电流流向的过程

pause(1);

light=line(10,4.3,'color','y','marker','.','markersize',40,'erasemode','xor');%画灯丝发出的光:黄色

%画电流的各部分

h=line([1;1],[5.2;5.6],'color','r','linestyle','-','linewidth',4,'erasemode','xor');

g=line(1,5.7,'color','b','linestyle','^','erasemode','xor','markersize',10);

%给循环初值

t=0;

m2=5.7;

n=5.7;

while n<6.3;%确定电流竖向循环范围

  m=1;

  n=0.05*t+5.7;

  set(h,'xdata',[m;m],'ydata',[n-0.5;n-0.1]);

  set(g,'xdata',m,'ydata',n);

  t=t+0.01;

drawnow;

end

t=0;

while t<2;%在转角处的停顿时间

    m=1.2-0.2*cos((pi/4)*t);

    n=6.3+0.2*sin((pi/4)*t);

   set(h,'xdata',[m-0.5;m-0.1],'ydata',[n;n]);

   set(g,'xdata',m,'ydata',n);

   t=t+0.05;

drawnow;

end

t=0;

while t<0.5 %在转角后的停顿时间

    t=t+0.5;

    g=line(1.2,6.5,'color','b','linestyle','^','markersize',10,'erasemode','xor');%绘制第二个箭头

    g=line(1.2,6.5,'color','b','linestyle','>','markersize',10,'erasemode','xor'); %绘制第二个箭头

    set(g,'xdata',1.2,'ydata',6.5);

drawnow;

end

pause(0.5);

t=0;

while m<8 % 确定第二个箭头的循环范围

    m=1.1+0.05*t;

    n=6.5;

    set(g,'xdata',m+0.1,'ydata',6.5);

    set(h,'xdata',[m-0.4;m],'ydata',[6.5;6.5]);

    t=t+0.05;

drawnow;

end

t=0;

while t<2 %%在转角后的停顿时间

    m=8.1+0.2*cos(pi/2-pi/4*t);

    n=6.3+0.2*sin(pi/2-pi/4*t);

    set(g,'xdata',m,'ydata',n);

    set(h,'xdata',[m;m],'ydata',[n+0.1;n+0.5]);

    t=t+0.05;

drawnow;

end

t=0;

while t<0.5 %在转角后的停顿时间

    t=t+0.5;

%绘制第三个箭头

    g=line(8.3,6.3,'color','b','linestyle','>','markersize',10,'erasemode','xor');

    g=line(8.3,6.3,'color','b','linestyle','v','markersize',10,'erasemode','xor');

    set(g,'xdata',8.3,'ydata',6.3);

drawnow;

end

pause(0.5);

t=0;

while n>1 %确定箭头的运动范围

    m=8.3;

    n=6.3-0.05*t;

    set(g,'xdata',m,'ydata',n);

    set(h,'xdata',[m;m],'ydata',[n+0.1;n+0.5]);

    t=t+0.04;

drawnow;

end

t=0;

while t<2%箭头的起始时间

    m=8.1+0.2*cos(pi/4*t);

    n=1-0.2*sin(pi/4*t);

    set(g,'xdata',m,'ydata',n);

    set(h,'xdata',[m+0.1;m+0.5],'ydata',[n;n]);

    t=t+0.05;

drawnow;

end

t=0;

while t<0.5

    t=t+0.5;

%绘制第四个箭头

    g=line(8.1,0.8,'color','b','linestyle','v','markersize',10,'erasemode','xor');

    g=line(8.1,0.8,'color','b','linestyle','<','markersize',10,'erasemode','xor');

    set(g,'xdata',8.1,'ydata',0.8);

drawnow;

end

pause(0.5);

t=0;

while m>1.2 %箭头的运动范围

    m=8.1-0.05*t;

    n=0.8;

    set(g,'xdata',m,'ydata',n);

    set(h,'xdata',[m+0.1;m+0.5],'ydata',[n;n]);

    t=t+0.04;

drawnow;

end

t=0;

while t<2 %停顿时间

    m=1.2-0.2*sin(pi/4*t);

    n=1+0.2*cos(pi/4*t);

    set(g,'xdata',m,'ydata',n);

    set(h,'xdata',[m;m+0.5],'ydata',[n-0.1;n-0.5]);

    t=t+0.05;

drawnow;

end

t=0;

while t<0.5 %画第五个箭头

    t=t+0.5;

    g=line(1,1,'color','b','linestyle','<','markersize',10,'erasemode','xor');

    g=line(1,1,'color','b','linestyle','^','markersize',10,'erasemode','xor');

    set(g,'xdata',1,'ydata',1);

drawnow;

end

t=0;

while n<6.3  %循环范围

    m=1;

    n=1+0.05*t;

    set(g,'xdata',m,'ydata',n);

    set(h,'xdata',[m;m],'ydata',[n-0.5;n-0.1]);

    t=t+0.04;

drawnow;

end

%下面是开关断开后的情况

t=0;

y=6.7;

while y<7.7 %开关的断开

    x=4+sqrt(2)*cos(pi/4*t);

    y=6.7+sqrt(2)*sin(pi/4*t);

    set(a,'xdata',[4;x],'ydata',[6.7;y]);

    drawnow;

    t=t+0.1;

end

pause(0.5);%开关延时作用

nolight=line(10,4.3,'color','y','marker','.','markersize',40,'erasemode','xor');

end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  matlab 练习程序