MATLAB Answers

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

323 views (last 30 days)
Andrew Ayman
Andrew Ayman on 9 Apr 2020
Answered: Pelden Chodon on 27 May 2021
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]);
%command window
area = tri_area[3,2])
Christine Mizzi
Christine Mizzi on 27 Aug 2020
What is the purpose for writing two output arguments in the code? i.e. [area, tri_area]
If the user is calling the area of a triangle wouldn't that be only one output argument?

Sign in to comment.

Accepted Answer

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

Sign in to comment.

More Answers (6)

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

Siya Desai
Siya Desai on 4 Apr 2021
Edited: Walter Roberson on 4 Apr 2021
function [area] = tri_area (b,h)
tri_area = (0.5)*(b)*(h)
code to call your function
tri_area(2,3) %any random input
  1 Comment
Walter Roberson
Walter Roberson on 4 Apr 2021
result = tri_area(2,3) %any random input
tri_area = 3
Output argument "area" (and maybe others) not assigned during call to "solution>tri_area".
function [area] = tri_area (b,h)
tri_area = (0.5)*(b)*(h)

Sign in to comment.

Pelden Chodon
Pelden Chodon on 27 May 2021
function [area, tri_area] = tri_area(b,h) ;
area = (0.5)*(b)*(h);
v = area(:);
tri_area = sum(v);
% Test that your function runs as expected before pressing Submit
[area, tri_area] = tri_area(2,3)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!