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

조회 수: 543(최근 30일)
Andrew Ayman 9 Apr 2020
답변: Imane Tahar 19 Nov 2020 17:48
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])

#### 댓글 수: 10

표시 이전 댓글 수: 7
do you mind explaining to me line 3 & 4
Thanks !
Walter Roberson 10 Aug 2020
Assuming that Nur Ameera Nabila Abdul Rahim is addressing Naveen Gehlot then:
area=(1/2)*(b)*(h)
is just the standard formula for the area of a triangle.
vca = area(:);
tri_area = sum(vca);
That is one way of calculating the total area, which is something that might potentially be done if the user asked to calculate the area for multiple triangles at the same time. However, if the user did ask to calculate for multiple triangles at the same time, it is fairly likely that area=(1/2)*(b)*(h) would be the wrong formula, and that it should instead be
area=(1/2)*(b).*(h);
or better
area = (1/2) .* b(:) .* h(:);
Christine Mizzi 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?

댓글을 달려면 로그인하십시오.

### 채택된 답변

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

#### 댓글 수: 17

표시 이전 댓글 수: 14
tahmina zaman mitoshi 10 Aug 2020
can anyone please tell the random input?
Walter Roberson 10 Aug 2020
The random input is 7 and 9.3
Walter Roberson 10 Aug 2020
Have you considered adding disp statements so you can see what parameters are being passed for the random input case?

댓글을 달려면 로그인하십시오.

### 추가 답변(4개)

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

#### 댓글 수: 2

Walter Roberson 2 Jun 2020
How does this differ from Torsten's Answer?
Maybe he wanted to test his first answer xD in the forum.

댓글을 달려면 로그인하십시오.

Eshan Pansare 28 Aug 2020
How to solve the random inputs part?

#### 댓글 수: 0

댓글을 달려면 로그인하십시오.

prudhvi gandham 6 Nov 2020 6:58
function area = tri_area(b,h)
area = 0.5*b*h;
end

#### 댓글 수: 0

댓글을 달려면 로그인하십시오.

Imane Tahar 19 Nov 2020 17:48
function area = tri_area(b,h)
area = (b*h)/2
end

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!

Translated by