How do I implement this function? What is an example I can use?
显示 更早的评论
function h = pythagorean(legs)
% hypotenuse = pythagorean(legs)
h = (legs(1)^2 + legs(2)^2)^ 0.5;
end
采纳的回答
legs=[5 7];
h = pythagorean(legs) ; % just call it
function h = pythagorean(legs)
% hypotenuse = pythagorean(legs)
h = (legs(1)^2 + legs(2)^2)^ 0.5;
end
10 个评论
sides = input('Enter the two legs of a right triangle: ');
hypot = pythagorean(sides);
fprintf('The hypotenuse of a right triangle with legs %d and %d is %d\n', sides(1), sides(2), hypot);
function h = pythagorean(legs)
% hypotenuse = pythagorean(legs)
h = (legs(1)^2 + legs(2)^2)^ 0.5;
end
How can I combine these two together to create a working function?
yes why not?
sides = input('Enter the two legs of a right triangle: ','s'); % change this line
and give input as below:
[2 3] % an example
Just copy and paste the below in a script and run it
sides = input('Enter the two legs of a right triangle: ','s'); % give input as [2 3] an example
hypot = pythagorean(sides);
fprintf('The hypotenuse of a right triangle with legs %d and %d is %d\n', sides(1), sides(2), hypot);
function h = pythagorean(legs)
% hypotenuse = pythagorean(legs)
h = (legs(1)^2 + legs(2)^2)^ 0.5;
end
Great. I didn't know about using the vector as an answer. :)
FYI you can check the answer of your function using the hypot function.
Agree with Steven Lord the builtin function has higher precision , @TSmith if my answer worked make sure to accept the answer.
I thought I did accept, I am sorry. Thanks to both of you! :) I figured out how to use the hypot function. All you have to do is type "hypot(A B)"
Your welcome :) , I assumed that this was your assignment from school .
I have no more assignments this semester. This is all practice for my final exam. :)
So good luck :)
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
