Please use MATLAB to solve this problem. I need a MATLAB script New Script Given
ID: 1711332 • Letter: P
Question
Please use MATLAB to solve this problem.
I need a MATLAB script
New Script Given the coordinates of three points (x1, y1), (x2, y2), and (x3, y3), it is possible to find the coordinates of the center of the circle (Cx, Cy) that passes through the three points by solving the following simultaneous equations: 2[(x_1 - x_2) (x_2 - x_3) (y_1 - y_2) (y_2 - y_3)] [C_x C_y] = [(x^2_1 + y^2_1) - (x^2_2 + y^2_2) (x^2_2 + y^2_2) - x^2_3 + y^2_3)] Write a program in script file that calculates the coordinates of the center and the radius of a circle that passes through the three given points. When executed the program asks the user to enter the coordinates of the three points. The program then calculates the center and radius and displays the results in the following format: "The coordinates of the center are (xx.x, xx.x) and the radius is xx.x" Execute the program entering the following three points: (10, 5, 4), (2, 8, 6), and (-4, -7)Explanation / Answer
% Entry of input data
prompt = ' Enter the coordinates of p1:' ;
p1 = input(prompt)
prompt1 = ' Enter the coordinates of p2 :' ;
p2 = input(prompt1)
prompt2 = 'Enter the coordinates of p3 :' ;
p3 = input(prompt2)
% calculation of c and r of the circle that passes through three given points
p1 = rand(1,2);
p2 = rand(1,2);
p3 = rand(1,2);
[ c r] = calc_circle(p1,p2,p3);
figure(1)
cla
axis equal
hold on
if r == -1
disp('collinear')
else
rectangle('position',[ c(1) - r, c(2) - r, 2*r , 2*r ] ,'curvature', [1,1],'Edgecolor','g')
end
plot(p1(1),p1(2),'*')
plot(p2(1),p2(2),'*')
plot(p3(1),p3(2),'*')
function [c r] = calc_circle(pt1,pt2,pt3)
delta_a = pt2 - pt1;
delta_b = pt3 - pt2;
ax_is_0 = abs(delta_a(1)) <= 0.000000001;
bx_is_0 = abs(delta_b(1)) <= 0.000000001;
% check whether both lines are vertical - collinear
if (ax_is_0 && bx_is_0)
c = [0 0];
r = -1;
return
end
% make sure delta gradients are not vertical
% re-arrange points to change deltas
if (ax_is_0)
[c r] = calc_circle(pt1, pt3, pt2);
return
end
if (bx_is_0)
[c r] = calc_circle(pt2, pt1, pt3);
return
end
grad_a = delta_a(2) / delta_a(1);
grad_b = delta_b(2) / delta_b(1);
% check whether the given points are collinear
if (abs(grad_a-grad_b) <= 0.000000001)
c = [0 0];
r = -1;
return
end
% swap grads and points if grad_a is 0
if abs(grad_a) <= 0.000000001
tmp = grad_a;
grad_a = grad_b;
grad_b = tmp;
tmp = pt1;
pt1 = pt3;
pt3 = tmp;
end
% calculate 'c' - where the lines perpendicular to the
'c' of segments a and b intersect.
c(1) = ( grad_a*grad_b*(pt1(2)-pt3(2)) +
grad_b*(pt1(1)+pt2(1)) - grad_a*(pt2(1)+pt3(1)) ) /
(2*(grad_b-grad_a));
c(2) = ((pt1(1)+pt2(1))/2 - c(1)) / grad_a +
(pt1(2)+pt2(2))/2;
% calculate radius
r = norm(c - pt1);
fprintf('CO-ORDINATES OF CENTER ARE [%d %d]', c);
fprintf('RADIUS OF CIRCLE IS %d', r);
% End of program