The area of a triangle ABC can be calculated by: A = 1/2 |AB times AC| where AB
ID: 2082005 • Letter: T
Question
The area of a triangle ABC can be calculated by: A = 1/2 |AB times AC| where AB is the vector from vertex A to vertex B and AC is the vector from vertex A to vertex C. Write a user-defined MATLAB function that determines the area of a triangle given its vertices' coordinates. For the function name and arguments, use [Area] = TriArea(A, B, C). The input arguments A, B, and C, are vectors, each with the coordinates of the corresponding vertex. Write the code of TriArea such that it has two sublimations-one that determines the vectors AB and AC and another that executes the cross product. (If available, use the user-defined functions from Problem 13). The function should work for a triangle in the x-y plane (each vertex is defined by two coordinates) or for a triangle in space (each vertex is defined by the coordinates). Use the function to determine the areas of triangles with the following vertices: (a) A = (1, 2), B = (10, 3), C = (6, 11) (b) A = (-1.5, -4.2, -3) B = (-5.1.6.3.2). C = (12.1, 0, -1.5),Explanation / Answer
Script file
-------------------------------------------------------
clear, clc
disp('1st question')
A=[1,2]; B=[10,3]; C=[6,11];
Area = triarea(A,B,C);
fprintf('Area of the triangle is %.1f ',Area)
disp('2nd question')
A=[-1.5, -4.2, -3]; B=[-5.1, 6.3, 2]; C=[12.1, 0, -0.5];
Area = triarea(A,B,C);
fprintf('Area of the triangle is %.1f ',Area)
----------------------------------------------------
Function file
---------------------------------------------------------------
function Area = triarea(A,B,C)
[AB AC] = sides(A,B,C);
Area = sqrt(sum(crosspro(AB,AC).^2))/2;
end
---------------------------------------------------------------
----------------------------------------------------------------
function [AB AC] = sides(A,B,C)
AB = B-A;
AC = C-A;
end
-----------------------------------------------------------
-----------------------------------------------------------
function q = crosspro(u,v)
x=length(u);
if x == 2
u(3)=0;
v(3)=0;
end
q(1)=u(2)*v(3)-u(3)*v(2);
q(2)=u(3)*v(1)-u(1)*v(3);
q(3)=u(1)*v(2)-u(2)*v(1);
end
---------------------------------------------------------
Command Window
1st question
Area of the triangle is 38.0
2nd question
Area of the triangle is 87.9
---------------------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.