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

269 ビュー (過去 30 日間)
Andrew Ayman 2020 年 4 月 9 日
コメント済み: Noor Fatima 2023 年 2 月 5 日
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])
##### 16 件のコメント表示 15 件の古いコメント非表示 15 件の古いコメント
Noor Fatima 2023 年 2 月 5 日
Thanku Walter roberson

サインインしてコメントする。

### 採用された回答

Torsten 2020 年 4 月 9 日

function area = tri_area(b,h)
area = 0.5*b*h;
end
From the command window
A = tri_area(3,2)
##### 21 件のコメント表示 20 件の古いコメント非表示 20 件の古いコメント
Walter Roberson 2022 年 12 月 26 日
When you use the debugger and format long g then what are some sample inputs, and what is the calculated output, and what does the grading program say that the output should be?

サインインしてコメントする。

### その他の回答 (3 件)

Imane Tahar 2020 年 11 月 19 日
function area = tri_area(b,h)
area = (b*h)/2
end
##### 0 件のコメント表示 -1 件の古いコメント非表示 -1 件の古いコメント

サインインしてコメントする。

Siya Desai 2021 年 4 月 4 日

function
function [area] = tri_area (b,h)
tri_area = (0.5)*(b)*(h)
tri_area(2,3) %any random input
##### 1 件のコメント表示 なし非表示 なし
Walter Roberson 2021 年 4 月 4 日
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)
end

サインインしてコメントする。

Pelden Chodon 2021 年 5 月 27 日
function [area, tri_area] = tri_area(b,h) ;
area = (0.5)*(b)*(h);
v = area(:);
tri_area = sum(v);
end
% Test that your function runs as expected before pressing Submit
[area, tri_area] = tri_area(2,3)
##### 1 件のコメント表示 なし非表示 なし
DGM 2023 年 2 月 4 日
You're declaring a variable with the same name as the function you're calling. This will either throw an error or silently cause other errors, depending on where you put the function.
For scalar inputs, summing area serves no purpose. For nonscalar inputs, the results are either wrong, or will throw an error.
% summing a scalar accomplishes nothing
[allareas totalarea] = tri_area(2,3)
allareas = 3
totalarea = 3
% some nonscalar inputs return the wrong results
[allareas totalarea] = tri_area([2 2; 2 2],[3 3; 3 3])
allareas = 2×2
6 6 6 6
totalarea = 24
% some nonscalar inputs return the wrong number of wrong results
[allareas totalarea] = tri_area([2 2],[3;3])
allareas = 6
totalarea = 6
% some nonscalar inputs fail completely
[allareas totalarea] = tri_area([2 2],[3 3])
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each element of the matrix
individually, use TIMES (.*) for elementwise multiplication.

Error in solution>tri_area (line 10)
allareas = (0.5)*(b)*(h); % these parentheses serve no purpose
function [allareas, totalarea] = tri_area(b,h)
allareas = (0.5)*(b)*(h); % these parentheses don't do anything
v = allareas(:);
totalarea = sum(v);
end
As this is all largely copy-pasted from other bad answers posted above, I should point out that much of these problems were already openly explained before you posted this. See also: function, array vs matrix operations

サインインしてコメントする。

### カテゴリ

Find more on Biotech and Pharmaceutical in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!