MATLAB: How to write a function called triAreaN that computes the area of N triangles

areafunctionhomeworkmatlab functionmatricesmatrix arraymatrix manipulationtriangle

The input of the function is a 2xN array where each column of the array contains the base and height of a single triangle. The function returns a 1xN array where the i^th column is the area of the i^th triangle. Test whether the base and height are positive. if either one is negative, set the area of the triangle to zero.
function triAreaN(B1…Bi;H1…Hi);
%H = Height of triangle (B1…Bi)=('enter the base'); (H1…Hi)=('enter the height');
A = (1/2*(B1*H1):1/2*(B1*Bi)); A = ('display answer'); A(output) end
Line 1: invalid syntax at '<EOL>'. Possibly, a ),}, or ] is missing. Line 4: Terminate statement with semicolon to suppress output(in functions). Line 5: invalid syntax at '<EOL>'. Possibly, a ),}, or ] is missing. Line 8: The value assigned to variable A' might be unused.
Homework problem. he never showed us how to use matrices or array in function or how to assign the rows to be certain input values. Stuck here not sure where to go from this point.

Best Answer

  • function A = triAreaN(B,H)
    if numel(B)==numel(H),
    A = 0.5*(B.*H);
    error('B and H must be same length');