%% Problem Set B: Problem 21
% First we set up the parametrizations of the cycloid and trochioid; and then we plot them.

clear all; close all
syms t s x y u v; cycl = [2*t - 2*sin(t), 2 - 2*cos(t)]; 
troch = [2*s - sin(s), 2 - cos(s)];
x = cycl(1); y = cycl(2);
u = troch(1); v = troch(2);
figure; fplot(x, y, [0 4*pi])
hold on; fplot(u, v, [0 4*pi])
axis([0 25 0 4])

%
% It looks from the picture as if there are 4 intersection points, with t and s in each case just above or below a multiple of 2*pi. So the following finds the intersection points and the values of t and s corresponding to them.

sol1 = vpasolve(cycl == troch, [t, s], [.1 .1]);
disp(['t = ', num2str(double(sol1.t)), '; s = ', ...
    num2str(double(sol1.s))])
disp(['[x, y] = ', num2str(double(subs(cycl, t, sol1.t)))])  
sol2 = vpasolve(cycl == troch, [t, s], [2*pi-.1, 2*pi-.1]);
disp(['t = ', num2str(double(sol2.t)), '; s = ', ...
    num2str(double(sol2.s))])
disp(['[x, y] = ', num2str(double(subs(cycl, t, sol2.t)))]) 
sol3 = vpasolve(cycl == troch, [t, s], [2*pi+.1, 2*pi+.1]);
disp(['t = ', num2str(double(sol3.t)), '; s = ', ...
    num2str(double(sol3.s))])
disp(['[x, y] = ', num2str(double(subs(cycl, t, sol3.t)))]) 
sol4 = vpasolve(cycl == troch, [t, s], [4*pi-.1, 4*pi-.1]);
disp(['t = ', num2str(double(sol4.t)), '; s = ', ...
    num2str(double(sol4.s))])
disp(['[x, y] = ', num2str(double(subs(cycl, t, sol4.t)))])

%
% Now let's mark the four points of intersection on the graph.

hold on;
P1 = double(subs(cycl, t, sol1.t));
P2 = double(subs(cycl, t, sol2.t));
P3 = double(subs(cycl, t, sol3.t));
P4 = double(subs(cycl, t, sol4.t));
plot(P1(1), P1(2), '.k', 'MarkerSize', 15)
plot(P2(1), P2(2), '.k', 'MarkerSize', 15)
plot(P3(1), P3(2), '.k', 'MarkerSize', 15)
plot(P4(1), P4(2), '.k', 'MarkerSize', 15)