%Animates a horizontal cycloida curve with wheel
FPS = 10;
period = 2;
N = 5;
R1= 3;
R2 = 5;
dtheta = (2*pi/period)/FPS;
xmax = R2+R1*N*(2*pi);
clf;
for i = 0:FPS*period*N
    theta = [0:0.1:i*dtheta];                  
    x = R2*cos(-theta + 3*pi/2) + R1*theta;
    y = R2*sin(-theta + 3*pi/2) + R1;           %cycloida curve from 0 till now
    t=[0:0.1:2*pi];
    c1x = R1*theta(end) + R1*cos(t);            %rolling circle
    c1y = R1 + R1*sin(t);
    c2x = R1*theta(end) + R2*cos(t);            %attached circle
    c2y = R1 + R2*sin(t);
    plot(x,y,c1x,c1y,c2x,c2y)                   %plot cycloida and circles
    hold on
    plot(x(end), y(end),'o');                   %plot the dot that draws the cycloida
    plot([-R2-R1 xmax],[0 0]);                  %line on which the circle rolls
    axis([-R2 xmax -(R2+R1) (R2+R1)]);
    axis equal;
    hold off
    pause(1/FPS);
end;