Opening the book…
The first step is to create a logical function, which is a function that returns a logical value. The following function takes three input variables, a, b, and c, and returns true (1) if they form a Pythagorean triple and false (0) otherwise.
function res = is_pythagorean(a, b, c)
if a^2 + b^2 == c^2
res = 1;
else
res = 0;
end
end
We can use this function like so:
>> is_pythagorean(3, 4, 5)
ans = 1
But we can write the same function more concisely, like this:
function res = is_pythagorean(a, b, c)
res = a^2 + b^2 == c^2;
end
The result of the equality operator is a logical value, which we can assign directly to res.
Put this function in a file called is_pythagorean.m, so we can use it as part of our program.
This page titled 6.4: Logical Functions was last modified on Thu, 17 Nov 2022 18:18:56 GMT and is shared under a CC BY-NC 4.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) via source content that was edited to the style and standards of the LibreTexts platform.
The book could not be loaded just now.