How can I create a function with random imputs? I have to create a function called tri_area that returns the area of a triangle with abse b and heigh h, where b and h are input arguments of the function in that order
1 回表示 (過去 30 日間)
古いコメントを表示
function area = tri_area(b,h)
b=2
h=3
area=(b*h)/2;
end
%They ask for ans answer when h=3 and b=2 AND OTHER with random inputs, and I down know how to do that
0 件のコメント
回答 (1 件)
John D'Errico
2020 年 8 月 3 日
編集済み: John D'Errico
2020 年 8 月 3 日
I think you may be confused. I believe what is asked is for a function with ARBITRARY inputs, in the sense it can be called with any arbitrary pair of numbers. I have often seen the word random used as was done, in a way that is perhaps confusing to some, when really all they intended was as a synonym of the word arbitrary.
function area = tri_area(b,h)
area=(b*h)/2;
end
Don't assign values to b and h inside the function. PASS THEM IN. Now, use the function, as you wrote it.
>> area = tri_area(2,3)
area =
3
>> area = tri_area(5,pi)
area =
7.85398163397448
>> area = tri_area(5,sqrt(2))
area =
3.53553390593274
>> area = tri_area(2.35,17.4545644)
area =
20.50911317
As you see, it now works for any set of base and heights.
You don't want to re-set the values of those parameters passed in. That was your only problem.
参考
カテゴリ
Help Center および File Exchange で Detection についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!