Matlab动画制作

Matlab能够对复杂对象进行建模,同时也提供了丰富的绘图方法。当绘图采用实时刷新的方式时,能实现动画的效果。下面的实例将展示如何将绘图得到的动画效果变成视频文件,为其他软件所使用。

clear; clc; close all;

% Set initial conditions

v0 = 12; theta = 70; w = 10*pi;

m1 = 0.2; m2 = 0.3; L = 1;

% Necessary calculation

g = 10; th = theta*pi/180;

r1 = L*m2/(m1+m2); r2 = L*m1/(m1+m2);

t_end = 2*v0*sin(th)/g;

% Get result data

fn = 100;

t = linspace(0,t_end,fn);

xc = v0*cos(th)*t;            % Centroid coordinate

yc = v0*sin(th)*t-g*t.*t/2;

x1 = xc+r1*cos(w*t);            % Ball 1 coordinate

y1 = yc+r1*sin(w*t);         

x2 = xc-r2*cos(w*t);            % Ball 2 coordinate

y2 = yc-r2*sin(w*t);  

% Plot the result

fig = figure;

plot([-2,-2,xc(end)+2,xc(end)+2,-2],[-2,max(yc)+2,max(yc)+2,-2,-2]);

set(fig,'DoubleBuffer','on');

set(gca,'NextPlot','replace','Visible','off');

grid off; axis equal; axis off; hold on;

h12 = plot([x1(1);x2(1)],[y1(1);y2(1)],'o-b','LineWidth',2,'MarkerSize',5);

title('直杆斜抛运动过程','FontSize',16);

pause;

for i=1:fn-1

    F(i) = getframe(fig);

    set(h12,'XData',[x1(i);x2(i)],'YData',[y1(i);y2(i)]);

    plot([xc(i);xc(i+1)],[yc(i);yc(i+1)],'LineWidth',2);

    if floor((i-1)/10)==(i-1)/10

        plot([x1(i);x2(i)],[y1(i);y2(i)],'LineWidth',2);

        plot(x1(i),y1(i),'ko','MarkerSize',5);

        plot(x2(i),y2(i),'ro','MarkerSize',5);

    end

    drawnow;

end

% Save to avi file

mov = avifile('mov1.avi');

mov = addframe(mov,F);

mov = close(mov);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值