MATLAB: How to put a function inside animation

animation

Hi, I have to program a bouncing ball using a function I created before called drawball, now I can't implement this function inside my program, it only shows me a big ball, here are the scripts I used:
function drawdisc(x0,y0,r,c)
t=0:0.01:2*pi;
x=r*cos(t)+x0;
y=r*sin(t)+y0;
fill(x,y,c)
end
the bouncing ball script:
clc;
initpos=0; %Ball's initial vertical position
initvel=20; %Ball's initial vertical velocity
r_ball=1; %Ball's radius
gravity=10; %Gravity's acceleration
c_bounce=0.85; %Bouncing's coefficient of elasticity
dt=0.0125; %Animation timestep;
pos=r_ball; %Ball's current vertical position

vel=initvel;%Ball's current vertical velocity

axis([0 20 0 25])
while 1
pos=pos+(vel*dt); %Ball's current vertical position
vel=vel-(gravity*dt); %Ball's current vertical velocity
if pos<0
vel=-vel*c_bounce; %Balls' current vertical velocity
end
drawball(1,pos,1,'b')
pause(0.01)
end
your help is appreciated!!

Best Answer

You define a function named "drawdisc" but you call upon "drawball" in your code.
You call upon drawball(1,pos,1,'b') in your code. It would make more sense to call upon drawball(1,pos,r_ball,'b')
If you the Image Processing Toolbox and a newer MATLAB you might want to consider using viscircles() instead of your own routine. You might also want to consider using scatter() with a marker size and with 'filled'. Or you might want to use plot() with 'o' as the marker and with a MarkerSize and MarkerFaceColor argument.