Write a MATLAB user defined function hw53triangle.m that accepts as input, a sin
ID: 1841819 • Letter: W
Question
Write a MATLAB user defined function hw53triangle.m that accepts as input, a single array containing the co-ordinates of 3 points in the form of a 3x3 array: [x1 y1; x2 y2; x3 y3]. Your function then must calculate the following properties of the triangle: Perimeter p = a + b + c where a, b, c are the lengths of the 2 sides.
Area A = ( )( ) where s = p/2. Radius of its circumcircle R = abc/(4A). Radius of its incircle r = A/s. Your function must be of the following form: Function [triperi, triarea, circumrad, inrad] = hw53triangle (xyzarray) Note: 1. Check that the input is a 3x3 array and that the corner points are not co-linear (sum of the
lengths of any two sides should be larger than the length of the third side).
2. The user may call your function with any number of output arguments. Be sure to compute the above quantities only if the user asks for them in the output (Hint: use nargout) For example, the user may call your function in any of the following ways:
>> xyz = rand (3,3);
>> hw53triangle(xyz)
>> [peri, myarea] = hw53triangle(xyz)
>> [peri, myarea, myrad1] = hw53triangle(xyz)
>> [peri, myarea, myrad1, myrad2] = hw53triangle(xyz)
Explanation / Answer
First input code can be written as
A=[0 0; 0 4; 4 0];
[peri,myarea]=hw53triangle(A)
[peri,myarea,myrad1]=hw53triangle(A)
[peri,myarea,myrad1,myrad2]=hw53triangle(A)
in place of A, you can give your own choice of points
now for function use this code.NOTE THAT THESE TWO FILES SHOULD BE IN THE SAME DIRECTORY and THESE TWO ARE DIFFERENT MATLAB SCRIPT FILES
function [peri,myarea,myrad1,myrad2 ] = hw53triangle( A )
a=[A(1) A(4); A(2) A(5)];
a1=pdist(a,'euclidean');
b=[A(2) A(5); A(3) A(6)];
b1=pdist(b,'euclidean');
c=[A(3) A(6);A(1) A(4)];
c1=pdist(c,'euclidean');
P=a1+b1+c1;
d=[A(1);A(2);A(3)];
e=[A(4);A(5);A(6)];
A=polyarea(d,e);
r1=a1*b1*c1/(4*A);
s=P/2;
r2=A/s;
peri=P;
myarea=A;
myrad1=r1;
myrad2=r2;
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.