How do I implement this function? What is an example I can use?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
function h = pythagorean(legs)
% hypotenuse = pythagorean(legs)
h = (legs(1)^2 + legs(2)^2)^ 0.5;
end
Accepted Answer
madhan ravi
on 7 Dec 2018
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 Comments
TSmith
on 7 Dec 2018
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?
madhan ravi
on 7 Dec 2018
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
madhan ravi
on 7 Dec 2018
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
TSmith
on 7 Dec 2018
Great. I didn't know about using the vector as an answer. :)
Steven Lord
on 7 Dec 2018
FYI you can check the answer of your function using the hypot function.
madhan ravi
on 7 Dec 2018
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)"
madhan ravi
on 7 Dec 2018
Your welcome :) , I assumed that this was your assignment from school .
TSmith
on 7 Dec 2018
I have no more assignments this semester. This is all practice for my final exam. :)
madhan ravi
on 7 Dec 2018
So good luck :)
More Answers (0)
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)