6.5: Logical Functions

Matlab and Octave Programming for STEM Applications · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    From LibreTexts, under CC BY-NC-SA 4.0, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    6.5: Logical Functions

    This is a modified version of Allen B. Downey's section. It was modified by Carey Smith to use parentheses for clarity and to remove possible ambiguity.

    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

    Put this function in a file called is_pythagorean.m, so we can use it as part of our program.


    This page titled 6.5: Logical Functions was last modified on Fri, 17 Feb 2023 05:30:30 GMT and is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith via source content that was edited to the style and standards of the LibreTexts platform.