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!