MATLAB question Please answer ASAP. Number 2 will be greatly appreciated! 1. Wri
ID: 3717585 • Letter: M
Question
MATLAB question
Please answer ASAP. Number 2 will be greatly appreciated!
1. Write a user-defined function that determines the location of the center and the radius of a circle that passes through three given points in a plane. The function also creates a plot that shows the circle and the three points. Divide the problem into three functions called by the main function. Input_the_data.m asks the user to input the three points. Do_calculations.m takes the three points as an input and finds the center and radius of the circle. It also outputs these results. If no answer is possible, it will output an error message. Plot_the_ result.m plots the circle and the three points from data supplied to it. Make sure the plot has a title, has both axis labeled, and has grid lines. Te the frnction with serapnts ou male up repolint A(7, 1.2), B(0.5, 2.6), and C(-2.4,-1.4). 2. Extra Credit. Repeat the same problem in three dimensions.Explanation / Answer
% Circle3Pts.m
function [R,C] = Circle3Pts(A,B,C1)
X1 = A(1);
X2 = B(1);
X3 = C1(1);
Y1 = A(2);
Y2 = B(2);
Y3 = C1(2);
M_R = (Y2-Y1)./(X2-X1);
M_T = (Y3-Y2)./(X3-X2);
% check for errors...
S1 = isinf(M_R);
S2 = isinf(M_T);
S3 = isequaln(M_R,M_T) | isnan(M_R) | isnan(M_T);
% calculating center
C = (M_R.*M_T.*(Y3-Y1)+M_R.*(X2+X3)-M_T.*(X1+X2))./(2*(M_R-M_T));
C(S1) = (M_T(S1).*(Y3(S1)-Y1(S1))+(X2(S1)+X3(S1)))/2;
C(S2) = ((X1(S2)+X2(S2))-M_R(S2).*(Y3(S2)-Y1(S2)))/2;
C(S3) = NaN;
% y coordinates circle points
C(2,:) = -1./M_R.*(C-(X1+X2)/2)+(Y1+Y2)/2;
S4 = 1;
M_R==0;
C(2,S4) = -1./M_T(S4).*(C(S4)-(X2(S4)+X3(S4))/2)+(Y2(S4)+Y3(S4))/2;
C(2,S3) = NaN; % if failure, then return NaN
% clculating radius
R = sqrt((C(1,:)-X1).^2+(C(2,:)-Y1).^2);
R(S3) = Inf; %if fails
end
%calling Circle3Pts method..
%main.m
A = [7; 1.2];
B = [0.5; 2.6];
C = [-2.4; -1.4];
[R C] = Circle3Pts(A,B,C);
disp(R);
disp(C);
%plotting results
hold on
val = 0:pi/50:2*pi;
xunit = R * cos(val) + A(1);
yunit = R * sin(val) + B(1);
h = plot(xunit, yunit);
hold off
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.