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 visualización (últimos 30 días)
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

Respuestas (1)

John D'Errico
John D'Errico el 3 de Ag. de 2020
Editada: John D'Errico el 3 de Ag. de 2020
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.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by