Join Curt Frye for an in-depth discussion in this video Calculating values using built-in functions and variables, part of Learning Octave.

In the previous movie, I showed you how to use built-in mathematical and logical operators to evaluate expressions. In this movie, I will show you some useful built-in functions that you can use to calculate values. I'll start out with a very quick overview in PowerPoint, and then I'll move over to Octave so that you can see how it works inside of the programming environment. The first three functions, I'll cover deal with roots. We have square root, sqrt which finds the square root of x. So the square root of 9 is 3. Then we have nthroot, x of n, which finds the nthroot of x.

So, for example, the nthroot of 27, 3 finds the third or cube root of 27, and that is 3. The next four functions all deal with numbers that have a decimal component. Fix truncates the number, that is it just removes the decimal component. Ceil, which is short for ceiling, rounds the number up, regardless of the decimal component. So for example 4.001 would be rounded up to five. Floor does exactly the opposite. It rounds a number down. So 4.99, would be rounded down to 4.

And finally round, rounds a number to the closest integer. And the rule that it uses, is any decimal component of 0.5 or higher, is rounded to the next integer. So for example, 4.49 would be rounded down to 4 and 4.5 would be rounded up to 5. Other useful functions include max, which finds the largest number in a set, min which finds the smallest number. You can also calculate a factorial. So, for example, 5 factorial returns 120 because it's five times four times three times two times one, and you can also work with primes by generating a list of primes.

There are two ways to do that. The first is using the primes function, which returns primes up to a particular number. So for example if you wanted to list all the prime numbers less than eight, you would say primes, and then enclose eight in parenthesis and you would get a list, of two, three, five, seven. If you want to list the first x number of primes, then you could type in list primes and then x for the number that you want. So, for example, list primes three would return two, three and five. That's a quick overview of the functions. Now let me jump over to Octave and show you how to use them.

Now that I'm in Octave, I'll type in the expressions using built-in functions. So, for example, square root, sqrt of 16 and the 16 is in parenthesis. If I press Enter, I get 4. And it doesn't have to be a whole number. So for an example if I type in sqrt of 15 and enter. I get 3.8730. Nth root works in a similar fashion.

So nth root of (81,4) means that I am asking Octave to calculate the fourth root of 81. And when I press enter, we get the answer 3. And again, the next four functions all deal with numbers that have decimal components. So if I type fix 3.5 and enclosing 3.5 in parentheses, the fix function removes the decimal component so if I press enter, it returns a value of 3. Ceiling, ceil rounds up regardless of the decimal component so if I type in 6.001 enclosed in parenthesis and press enter, I get the number 7.

Floor does exactly the opposite so in a way it's like fix. Type in floor and 6.999 and enter. I get the answer of 6. And finally, round. I'll do two examples for this. Have round of 4.49. That rounds down 4. And round of 4.5, rounds up to 5. Max and min work on sets of values. So for example, if I were to assign the variable V, a series of values, and this is a one dimensional matrix or a vector.

So I would assign it the values: 1, 2, 3 and 4, enclosed in square brackets and press enter. You'll see that those numbers have now been assigned to V. And if I type in max of V. I get 4. And if I type in min of V, then press enter, I get 1. So max finds the largest value of 4 and min finds the smallest value in that set, which is 1. And don't worry, I will cover matrices and vectors in more detail. The next function that I mentioned is factorial.

So, for example, factorial of 5 would calculate 5 times 4 times 3 times 2 times 1, and that value is 120. The last two functions deal with prime numbers. So if I type in primes(23) and press enter. I get a list of all the prime numbers up to and including 23 and if I type list_primes and type in 23, then I will get a list of the first 23 prime numbers, which you see here.

Those are the built-in functions that I have found to be the most useful. You'll come back to them again and again, when you build your Octave scripts.

###### Released

1/23/2014- Downloading and installing Octave
- Using built-in commands
- Manipulating strings
- Defining vectors and matrices
- Defining functions
- Creating executable scripts
- Debugging your Octave code

## Share this video

## Embed this video

Video: Calculating values using built-in functions and variables