what is wrong with my function

조회 수: 38 (최근 30일)
mostafa Eldaly
mostafa Eldaly 2019년 2월 25일
댓글: Francisco Moto 2021년 1월 19일
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
SWARNENDU DUTTA
SWARNENDU DUTTA 2020년 8월 9일
function area = tri_area(b, h)
area = (0.5*b*h)
end
Francisco Moto
Francisco Moto 2021년 1월 19일
the area of a triangle is 0.5*b*h not b*h

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

채택된 답변

Sanket Dahat
Sanket Dahat 2019년 12월 1일
function area=tri_area(b,h)
area=b*h/2;
end

추가 답변 (3개)

Stephan
Stephan 2019년 2월 25일
You calculate the area of a rectangle. Think about the formula of triangle area...

Muhammad Haris Anwar
Muhammad Haris Anwar 2020년 3월 3일
function area = tri_area(b,h)
tri_area=0.5*b*h
end

Mrinal kant Priyadarshi
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
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 CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by