Write a MatLab code to solve the following problem: Consider a similar problem, except now, make it that circle 1 occupies area in the plane, and circle 2 represents no occupied area. Then, as circle 1 and circle 2 overlap, it will happen that circle 2 carves out a portion of circle 1. Effectively, circle 2 is acting like a hole. Near the end of the posted notes, a combination of such type of circles is shown as an example of this effect. As in the first part, plot the area as a function of d, and include error bars on this plot. The program should have the capability to make either circle larger than the other.


The script should be similar to the following script:

function CalculateOverlapArea()
% Calculates the area of 2 circles in a plane that can overlap

% Inputs: R1, R2, d
% Procedure: Use Monte Carlo integration
% Output: Show a figure of overlapping circles according to input
    % parameters, and Show a graph of area VS distance of separation

% p-error = tolerance of the accuracy as percent error
% R1 = radius for circle 1
% R2 = radius for circle 2
% d = distance of separation b/t the two circles

R1 = input ('Enter radius of circle 1: ');
if (R1<0.1)
    error('1 = natural length scale: R1 should not be less than 0.01');
end
if (R1>10.0)
    error('1 = natural length scale: R1 should not be greater than 10.0');
end

R2 = input ('Enter radius of circle 2: ');
if (R2<0.1)
    error('1 = natural length scale: R2 should not be less than 0.01');
end
if (R2>10.0)
    error('1 = natural length scale: R2 should not be greater than 10.0');
end

d = input ('Enter separation distance: ');
if (d<0.0)
    error('The separation distance must be greater than 0');
end

% ————————— plot

GraphOverlappingCircles(R1,R2,d);

disp('The area of this object will be estimated');
disp('Method: Monte Carlo Integration');
disp('   ');
perror = input('Enter an acceptable percent error: ');
if (perror<0.1)
    error('Lowest reasonable value is 0.1, be less ambitious');
end
if (perror>20)
    error('20% is too crude of an estimate, be more ambitious');
end
% ————————— use method of Monte Carlo Integration
area = TotalArea(R1,R2,d,perror)
end

% ————————— subfunctions
function A = TotalArea(R1,R2,d,p)
[xmin, xmax, ymin, ymax] = GetBoxCoordinates(R1,R2,d); % rectangular box size
%           get area of box
Abox = (xmax - xmin)*(ymax - ymin);
%           genetate uniformly random points
% A/Npts = avg area per point
% Amin = total area of smallest circle
% p*Amin = avg area per point
% actual percent error in area should be less than p by using Npts
R = min(R1,R2);
Amin = pi*R*R;
avg_area_per_point = p*Amin/100;
Npts = 50*ceil(Abox / avg_area_per_point);
A = Abox;
end

function [xmin, xmax, ymin, ymax] = GetBoxCoordinates(R1,R2,d)
%            define rectangular box size
xmin = -R1;
xmax = d+R2;
ymax = max(R1,R2);
ymin = -ymax;
end


function GraphOverlappingCircles(R1,R2,d)
N=200;
t=2*pi/N*(1:N);
figure;
hold on
axis equal;
fill(R1*cos(t),R1*sin(t),'k');
fill(d + R2*cos(t), R2*sin(t),'k');
[xmin, xmax, ymin, ymax] = GetBoxCoordinates(R1,R2,d); % rectangular box size
x = zeros(5,1);
y = zeros(5,1);
% ————————— expand boundaries
xmin = xmin - 0.01;
xmax = xmax + 0.01;
ymin = ymin - 0.01;
ymax = ymax + 0.01;
% ————————— prepare arrays to plot expanded box
x(1) = xmin;
y(1) = ymin;
x(2) = xmin;
y(2) = ymax;
x(3) = xmax;
y(3) = ymax;
x(4) = xmax;
y(4) = ymin;
x(5) = xmin;
y(5) = ymin;
plot(x,y,'r');
end