% EExtra_2.m
% Solution to problem 2 of ConvExtra. JMS, Jun.2024

clear

% Find y(x)
yfunc = @(x)2*sqrt(x.^2-x.^4);
x = -1:1e-3:+1;
y = yfunc(x);

% Plot curve
figure(11)
plot(x,y,'b',x,-y,'b','LineWidth',2)
axis equal
xlabel('x (cm)')
ylabel('y (cm)')
grid on
set(gca,'FontSize',15)

% Find surve surface
S1 = 2*integral(x,y);
S1 = S1(end)

% Find curve surface by Monte Carlo
n = 4e6;
x = -1+2*rand(n,1);
y = -1+2*rand(n,1);
S2 = 4*sum(4*(x.^4-x.^2)+y.^2<0)/n

% Find intersections with y=x+1
% By visual ispection we see that one intersection occurs at x=-1
% and that another is between x=-0.8 and 0
x1 = bisectionRoot( @(x)yfunc(x)-x-1, -1, -0.8 )
x2 = bisectionRoot( @(x)yfunc(x)-x-1, -0.8, 0 )
figure(11), hold on
x = -1:1e-3:0;
plot(x,x+1,'r', x1,yfunc(x1),'.r', x2,yfunc(x2),'.r', ...
     'LineWidth',2, 'MarkerSize',30)
hold off
