what is wrong with my function
7 次查看(过去 30 天)
显示 更早的评论
here is the Question that I want to Answre:
(Write a function called tri_area that returns the area of a triangle with base b and height h, where b and h are input arguments of the function in that order.)
function area = tri_area(b,h)
area = b*h;
end
2 个评论
采纳的回答
更多回答(3 个)
Stephan
2019-2-25
You calculate the area of a rectangle. Think about the formula of triangle area...
0 个评论
Mrinal kant Priyadarshi
2020-5-3
编辑:Mrinal kant Priyadarshi
2020-5-3
function area= tri_area(b,h)
tri_area(b,h)= (0.5)*(b)*(h)
area= tri_area(b,h)
end
1 个评论
Walter Roberson
2020-5-3
If b and h happen to be positive integers, then the assignment on the first line would work, creating an array that is b rows high and h columns, with all the values set to 0 except for the very bottom corner that would be set to (0.5)*(b)*(h) . You would then retrieve that location to create the output. This seems a bit of a waste to create that array.
If either b or h is not a positive integer, such as if b were 2.5 and h were 5, then the assignment on your second line would fail.
You have confused arrays and formulas. In MATLAB, you create formulas using @, such as
function area= tri_area(b,h)
TA = @(b,h) (0.5)*(b)*(h);
area = TA(b,h);
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!