Search code examples
matlabperformanceanimationprocessing-efficiency

Matlab: Unable to play the animation of a Bouncing Ball


I created the following code which maps the trajectory of a bouncing ball. I'm trying to create a point that moves along the trajectory. The code works but the animation won't play.

Keep in mind I'm an undergraduate student, the code is not elegant. (If you have tips to improve my code or head me in a new direction, don't be afraid to suggest.)

How can I get the animation to work?

    syms xdis(t1) ydis(t1) ydis1(t1) xdis1(t1) xdis2(t1) ydis2(t1) xdis3(t1)...
    ydis3(t1) xdis4(t1) ydis4(t1) xdis5(t1) ydis5(t1) xdis6(t1) ydis6(t1)

dt = 0.05; 
tmax = 6; 
t = 0:dt:tmax ;

height=10;
velocity=12;
angle=0;
g=-9.8;
xpos = 0 ;                     
ypos = height;                 
xvel = velocity*cosd(angle);    
yvel = velocity*sind(angle);
xdis(t1)= xvel*t1+xpos;
ydis(t1)=yvel*t1+0.5*g*t1^2+ypos;

troot1=solve(ydis(t1)==0,t1);
troot11=vpa(troot1(1));
troot12=vpa(troot1(2));


dydis(t1)=diff(ydis(t1),t1);
p=solve(dydis(t1)==0,t1);
h=vpa(ydis(p));


i1=vpa(dydis(troot11));

yvel1=0.7*i1;
ypos1=0;
ydis1(t1)=yvel1*t1+0.5*g*t1^2;
xdis1(t1)=xvel*t1+xdis(vpa(troot12));

troot2=solve(ydis1(t1)==0,t1);
troot21=(troot2(1));
troot22=(troot2(2));

dydis1(t1)=diff(ydis1(t1),t1);
p1=solve(dydis1(t1)==0,t1);
i2=vpa(dydis1(troot21));
yvel2=0.7*i2;

ydis2(t1)=yvel2*t1+0.5*g*t1^2+ypos1;
xdis2(t1)=xvel*t1+vpa(xdis1(troot22));

troot3=solve(ydis2(t1)==0,t1);
troot31=troot3(1);
troot32=troot3(2);

dydis2(t1)=diff(ydis2(t1),t1);
p2=solve(dydis2(t1)==0,t1);
i3=vpa(dydis2(troot31));
yvel3=0.7*i3;

ydis3(t1)=yvel3*t1+0.5*g*t1^2+ypos1;
xdis3(t1)=xvel*t1+vpa(xdis2(troot32));

troot4=solve(ydis3(t1)==0,t1);
troot41=troot4(1);
troot42=troot4(2);

dydis3(t1)=diff(ydis3(t1),t1);
p2=solve(dydis3(t1)==0,t1);
i4=vpa(dydis3(troot41));
yvel4=0.7*i4

ydis4(t1)=yvel4*t1+0.5*g*t1^2+ypos1;
xdis4(t1)=xvel*t1+vpa(xdis3(troot42));

troot5=solve(ydis4(t1)==0,t1);
troot51=troot5(1);
troot52=troot5(2);

dydis4(t1)=diff(ydis4(t1),t1);
p2=solve(dydis4(t1)==0,t1);
i5=vpa(dydis4(troot51));
yvel5=0.7*i5;

ydis5(t1)=yvel5*t1+0.5*g*t1^2+ypos1;
xdis5(t1)=xvel*t1+vpa(xdis4(troot52));

troot6=solve(ydis5(t1)==0,t1);
troot61=troot6(1);
troot62=troot6(2);



o=troot12-0; %% xdis(1) ydis(1)
o1=troot22-troot21; %% xdis1 ydis1
o2=troot32-troot31; %% xdis2 ydis2
o3=troot42-troot41; %% xdis3 ydis3
o4=troot52-troot51; %% xdis4 xdis4
o5=troot62-troot61;

int1=o;
int2=o+o1;
int3=o+o1+o2;
int4=o+o1+o2+o3;
int5=o+o1+o2+o3+o4;
int6=o+o1+o2+o3+o4+o5;


kt=0.05;
kmax=6;
k=0;
while k<kmax
    if 0<=k && k<=int1
    s1(k/0.05+1)=vpa(xdis(k));
    s2(k/0.05+1)=vpa(ydis(k));
    elseif int1<k && k<=int2
    s1(k/0.05+1)=vpa(xdis1(k-int1));
    s2(k/0.05+1)=vpa(ydis1(k-int1));
    elseif int2<k && k<=int3
    s1(k/0.05+1)=vpa(xdis2(k-int2));
    s2(k/0.05+1)=vpa(ydis2(k-int2));
    elseif int3<k && k<=int4
    s1(k/0.05+1)=vpa(xdis3(k-int3));
    s2(k/0.05+1)=vpa(ydis3(k-int3));
    elseif int4<k && k<=int5
    s1(k/0.05+1)=vpa(xdis4(k-int4));
    s2(k/0.05+1)=vpa(ydis4(k-int4));
    elseif int5<k && k<=int6
    s1(k/0.05+1)=vpa(xdis5(k-int5));
    s2(k/0.05+1)=vpa(ydis5(k-int5));
    end
    k=k+kt;
end


figure(1)
for t=1:1:length(s2)
plots{t}=plot(s1(t),s2(t),'ro','MarkerSize',10);
axis([0 xvel*6 0 h])
pause(0.001)
end

Solution

  • you need to convert symbolic variable h to double if you want to use it inside the function axis

    figure(1)
    for t=1:1:length(s2)
        plots{t}=plot(s1(t),s2(t),'ro','MarkerSize',10);
        axis([0 xvel*6 0 double(h)])
        pause(0.001)
    end