MATLAB comes with a huge selection of built-in functions you can use to create calculations. There’s no way to cover all of them, so in this video, get a quick overview of some of the most common functions and find out where to look for the rest.
- [Instructor] MATLAB comes with a huge selection of built-in functions that you can use to create calculations. There is no way to cover them all, so in this movie, I'll give you a quick overview of some of the most common. I have started MATLAB, and I'm working within a blank command window. One frequent operation that you'll perform, is to round numbers, either up or down, based on different criteria that you set. If you always want to round numbers up, then you can use the ceiling function, which is indicated by the keyword C-E-I-L, which is just short for ceiling.
Ceiling rounds up any decimal point to the next whole number. So if you were to type "ceil", followed by a left parentheses, so then "14.01", the smallest two-digit decimal you can have, and then enter, you see that it rounds up to 15. Floor is the opposite. It always rounds down. So if you type "floor", left parentheses, and let's do "14.99", right parentheses, and enter, you see it rounds down to 14. It's almost the same as the integer operation, where it truncates the decimal.
If you want a little bit more control over rounding, then you can use the round function. So just type "round", and then left parentheses. You can just enter a number, and it will round any decimal value of .5 or greater up to the next whole number, anything less than .5 will be rounded down. So if you round 14.5 with no other arguments, and then enter, you see that it goes up to 15. On the other hand, if you round 14.4999, and right parentheses and enter, see that it's rounded down to 14.
You can also round to the left or right of the decimal point. So let's say that I want to round the following number: "round", left parentheses, "14.485", and I want to round that to two places to the right of the decimal point. So in other words, if we're using the rules we had before, I want to end up with 14.49. So I'll type a comma, and then the number two, indicating the number of places to the right of the decimal point. Then a right parentheses, and enter, and I get 14.49, as expected.
You can also round to places to the left of the decimal point, but it's one further to the left than you think it should be. Let me show you what I mean. Let's say that you're rounding the number 175, and then you type a comma, and you type in minus two. So what you would expect, is to round two places to the left of the decimal point, and it would give you 180. However, if you type a right parentheses and enter, instead you get 200. So what's happening is that it's ignoring the single, or units, digit, the ones digit.
So you have 175, if you round it one place to the left, it would just be 175. So that means that the function starts counting from the second position. So if you type "round", then, in parentheses, "175" comma minus one, and add right parentheses and enter, then you get your 180. You can also do modular division, which finds the modulus, or remainder, of a division operation. So if you do "mod", M-O-D, followed by a left parentheses, and then "176" comma, and then the divisor is nine.
Type a right parentheses and enter, and you get a remainder of five. So that means nine goes into 176 with a remainder of five, so that looks like nine times 19, which would be 171. And finally, you might also want to convert between degrees and radians, when you're working with circles, or other angles. To switch between degrees and radians, use the "dg2rad", which is short for degrees to radians, so "dg2rad", followed by a left parentheses, and let's say that we have 270 degrees, in parentheses.
Press enter. Oh, sorry, it should be "deg2rad". So we have "deg2rad", and you can see that MATLAB corrected, or offered a correction. Press enter, and it's 4.7124, in radians. You can do the same thing going from radians to degrees. And that is "rad2deg", and then after a left parentheses, type "3", right parentheses, and enter, and you get 171.8873.
Again, these are just some of the functions that are available to you in MATLAB. I'll cover quite a few more elsewhere in the course.
- Defining variables and contains
- Exploring operators
- Summarizing with built-in functions
- Generating random numbers
- Defining vectors and matrices
- Accepting input in scripts
- Writing and reading data from external files
- Creating custom functions
- Using conditional logic
- Repeating operations with loops
- Working with text strings
- Plotting data and function output
- Formatting, saving, and printing plots
- Using statistical and matrix functions