using MATLAB The area of a triangle ABC can be calculated by Area = 1/2 * AB×AC,
ID: 2076889 • Letter: U
Question
using MATLAB
The area of a triangle ABC can be calculated by
Area = 1/2 * AB×AC,
where || || is the magnitude of the vector, AB is the distance vector from point A to point B and AC is the distance
vector from point A to point C. Write a MATLAB user-defined function, [Area] =
LN_TriArea(A,B,C), to calculate the area of the triangle using the equation above. The input A,
B, and C are 1D arrays containing the XY or XYZ coordinates of the vertices of the triangle, e.g. A = [2,3] or
A = [1 2 0]. In your function, create two sub-functions, vec = MyDistance(vertex1, vertex2) and
out = MyCross(vec1,vec2). The MyDistance( ) function returns the distance vector between two
points given by vertex1 and vertex2. The MyCross( ) function computes the cross product of two
vectors given by vec1 and vec2. You CANNOT use MATLAB built-in functions to calculate the cross
product. Be sure to check for invalid inputs and display proper warning messages. Your program must include
proper comment lines.
Using your function above, write down the function call that you use and the answer for the are calculation of a
triangle formed by 3 points: (0,0,0), (5.5, 0, 0) and (5.5, -4, 0).
thank you.
Explanation / Answer
%% MAIN CODE:-----------
% define co-ordinates of vertices
A = [1 2 3];
B=[0 0 0];
C = [1 1 0];
[Area] = LN_TriArea(A,B,C);
********************************************************
%% Function file 1
function [Area]=LN_TriArea(A,B,C)
AB = MyDistance(A,B);
AC = MyDistance(A, C);
[temp] = MyCross(AB,AC);
Area = 1/2*sqrt(temp*temp');
end
%% *************Function 2******************
function [vec] = MyDistance(vertex1, vertex2)
vec = vertex2-vertex1;
end
%% ************Function 3***************************
function [out] = MyCross(vec1,vec2)
out= zeros(size(vec1));
out(1,1)= vec1(2)*vec2(3)-vec2(2)*vec1(3);
out(1,2)= vec1(3)*vec2(1)-vec2(3)*vec1(1);
out(1,3)= vec1(1)*vec2(2)-vec2(1)*vec1(2);
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.