function Control1_1_JMS(model)
% Solution to exercise 1 of control 1.
% JMS, Oct.2021

% Set parameters
if ~exist('model')
  model = 'A';
end
if model=='A'
  a = 1.5;
  b = 1.0;
elseif model=='B'
  a = 1.5;
  b = 2.0;
elseif model=='C'
  a = 2;
  b = 1;
elseif model=='D'
  a = 1;
  b = 2;
else
  error('Control1_1 ERROR: unexpected model')
end
dphi = pi/180;

% Find parametric curve
phi = -pi:dphi:0;
x = a*cos(phi);
y = -b*sin(phi).*(1+cos(phi));

% Find curve area and barycenter using triangles
nphi = numel(phi);
r0 = [0;0];
Atot = 0;
Btot = [0;0];
for iphi = 2:nphi
  r1 = [x(iphi-1);y(iphi-1)];
  r2 = [x(iphi);y(iphi)];
  [A,~,B] = Triangle_JMS(r0,r1,r2);
  Atot = Atot+A;
  Btot = Btot+A*B;
end
Btot = Btot/Atot;
fprintf('Curve barycenter = [%f  %f] m\n',Btot)

% Find curve area as int(y(x)*dx)
A = integral_JMS(x,y);
Atot2 = A(end);
fprintf('Curve area (sum of triangles)  = %f m^2\n',Atot)
fprintf('Curve area (integral(y(x)*dx)) = %f m^2\n',Atot2)

% Plot curve and barycenter
figure(11)
plot(x,y,[-a,a],[0,0],Btot(1),Btot(2),'.r','LineWidth',1.5,'MarkerSize',30)
axis equal
xlabel('x (m)')
ylabel('y (m)')
grid on
set(gca,'FontSize',15)

end
