MATLAB Answers

write a function called tri_area returns the area of a triangle with base b and height h

1,577 views (last 30 days)
Andrew Ayman
Andrew Ayman on 9 Apr 2020
Commented: Walter Roberson on 21 Jun 2020 at 21:28
hello this is my function code and command window code and there is a message of invalid expression at line 2 and i dont know what is the wrong can anyone help me
function [area] = tri_area([b,h]);
tri_area([b,h])=(0.5)*(b)*(h)
area=tri_area([b,h])
end
%command window
area = tri_area[3,2])

  7 Comments

Show 4 older comments
Abdullah Javed
Abdullah Javed on 27 May 2020
Make a little change in your code and it will work.
function [area] = tri_area(b,h)
tri_area(b,h)=(0.5)*(b)*(h)
area=tri_area(b,h)
end
Naveen Gehlot
Naveen Gehlot on 6 Jun 2020 at 19:35
function [area, tri_area]= tri_area (b, h)
area=(1/2)*(b)*(h)
vca = area(:);
tri_area = sum(vca);
end
use this function for randow inputs
it will works.

Sign in to comment.

Accepted Answer

Torsten
Torsten on 9 Apr 2020
Edited: darova on 9 Apr 2020
function area = tri_area(b,h)
area = 0.5*b*h;
end
From the command window
A = tri_area(3,2)

  14 Comments

Sign in to comment.

More Answers (1)

Ramakant Gupta
Ramakant Gupta on 15 May 2020
Edited: Walter Roberson on 2 Jun 2020 at 23:09
function area = tri_area(b,h)
area = 0.5*b*h;
end