% PlotCircle_5.m: Plot two circles (circles 1 and 3 with radii R1, R3) % tangent to each other at the origin with centers on the x axis % and a third circle (circle 2 with radius R2) tangent to and above % circles 1 and 3. % Input Radii R1, R2, and R3 of three circles R1 = input('Enter radius of circle 1: '); R2 = input('Enter radius of circle 2: '); R3 = input('Enter radius of circle 3: '); R4 = input('Enter radius of circle 4: '); clf; hold on; % do not erase drawn circles angle = linspace(0,2*pi,100); % vector of angles at which points are drawn % Find center of circle 2 (tangent to and above circles 1 and 3) g2 = ((R2+R3)^2+(R1+R3)^2-(R1+R2)^2)/(2*(R1+R3)); % temporary variable cx2 = R3-g2; % x coordinate of circle 2 center cy2 = (R2+R3)*sin(acos(g2/(R2+R3))); % y coordinate of circle 2 center % Find center of circle 4 (tangent to and below circles 1 and 3) g4 = ((R4+R3)^2+(R1+R3)^2-(R1+R4)^2)/(2*(R1+R3)); % temporary variable cx4 = R3-g4; % x coordinate of circle 4 center cy4 = -(R4+R3)*sin(acos(g4/(R4+R3))); % y coordinate of circle 4 center % Set up the circles x1 = -R1+R1*cos(angle); y1 = R1*sin(angle); % Coordinates of circle 1 x2 = cx2+R2*cos(angle); y2 = cy2+R2*sin(angle); % Coordinates of circle 2 x3 = R3+R3*cos(angle); y3 = R3*sin(angle); % Coordinates of circle 3 x4 = cx4+R4*cos(angle); y4 = cy4+R4*sin(angle); % Coordinates of circle 4 %Set up the lines xl = [-R1 cx2 R3 cx4 -R1 R3 cx2 cx4]; yl = [0 cy2 0 cy4 0 0 cy2 cy4]; % Draw circles and lines plot(x1,y1,'r',x2,y2,'g',x3,y3,'b',x4,y4,'m',xl,yl,'k'); axis equal; grid on;