Click here to subscribe for more videos like this!

**QUESTION: What are the popular builtin functions/constants in MATLAB? How do you define your own functions?**

So in this tutorial we’re going to talk about matlab functions and constants, so built-in functions and constants to be specific, and then how you can define your own functions later on. So by functions what I mean is mathematical functions like sine, cosine, tangent, exponential log, and square root and so those are pretty much what you’d expect if you’ve ever coded in any other language before. So I can do sine of 0 is going to be 0, I can do cosine of 0 gives me 1, tangent of 0 of course would be also 0. So let’s look at these trigonometric functions for a little bit. So one thing is that because matlab works on matrices I can actually pass in more than just one number into sine. So suppose I use the matrix grid before 1,2,3,4, if I pass that to sine what that does is it calculates the sine of every element of A and returns a matrix the same size as A. So I can do all that separately of course just to show that it is indeed doing that individually for every element and so all matlab functions generally work like that. Some other functions you might want to use is the exponential function that’s exp, there’s the log which is the opposite of the exponential. If I do log of exp of one I should get back one. I can do square root is sqrt ok, so those are some very simple elementary functions in matlab. There are also some built-in constants into matlab that you should know about. So, for example, pi that’s just pi. So if you do something like have a variable called pi and you assign it to something else, now I cannot use the original variable pi anymore. So what I would do if I want pi back is I would go into my workspace and delete pi alright, and so now if I type pi again it’s now back to original pi. Generally, I would avoid overriding built-in constants. So, one weird thing about MATLAB is it doesn’t have a variable e, so if you want the variable e you actually have to do exp of one which is e to the power of 1 which is e. Another important constant is i. So if I do the square root of minus one I get 0 plus 1 times i. I can also just type in I and it would give me the same answer. So you can also do things like i*i which should give you -1, or you can do i^2. So you do exponents using the hat symbol so that also gives me -1. So there are also some important functions for initializing matrices that you should know about. So the first one is the I function. So you’ve seen I in the sense that if I have a matrix A then A*I should equal A, and so I is the identity matrix and it’s a square matrix with ones on the diagonal and zeros everywhere else. So suppose I want to create a 3 by 3 identity matrix. I could do something like this which would be a little bit inefficient right, or I could do eye(3) which gives me the same thing. Another thing you might want to do is create a matrix of all zeros, so that’s just the function zeros. So that gives me a three-by-three matrix of all zeros. If I want to specify different dimensions, so I don’t want it to be a square, what I can do is I can pass in multiple dimensions. There’s also a function called ones which does a similar thing it creates a matrix of all ones. You can pass in the size the same way. So the next thing I want to teach you guys to do is how to define your own functions. So generally you want to for every function you create you wanna put it into a new file, so I’m going to go new function. So it gives me some boilerplate code that I could use and this is good because it will help me explain the syntax of a function to you. So when we create a function it starts with the word function. If your function returns things you can specify the output arguments over here. If there are more than one output argument you can specify it as an array. If there aren’t any output arguments you can get rid of this part completely, and if there’s only one output argument you can get rid of the brackets. So let’s suppose I want to define the hyperbolic sine function which matlab probably already has but we’re going to do it anyway. So it’s going to take one input argument called X and take an output of the Y. So matlab has some useful syntax highlighting it will give us a warning you know stating input argument X is unused since we haven’t written any code yet, and then it’s returning a value Y but we haven’t set Y to anything yet. So let’s save what we have so far, you can do command s or go to file save. So we’ll save it. So one thing to notice you have to save the function name has to be the same as the filename, and so like I mentioned before matlab already has its own sine H so we don’t want to overwrite matlab sine H. So we’re going to call our sine H something else, I’m going to call it my sine H. I’m going to rename this file to my sine H. Alright, so now I’m not getting that warning anymore. Ok, so we’re going to define our own hyperbolic sine which is defined as the exponential of the input, minus the exponential of the negative of the input, and then taking one half of that, so let’s do that. So inside the function I have to set Y remember because I was getting a warning that Y was not set. Remember, I want to put a semicolon here so that I don’t see the output of this expression. I’m going to get rid of this comment because this function is pretty trivial but in general if you have a complicated function you should probably comment it so you remember what you were doing later on or whoever else is reading your code will understand what you’re doing, and so you can write comments using the % sign to comment code. So I’m going to save that, I’m going to use this function so my sine H let’s say one, zero so we can see from this image that hyperbolic sine crosses zero and then it increases as x increases, so that’s what you should be seeing. Remember that matlab works with matrices though so if I wanted to I can say X goes from 0 to 10 and I want to calculate the hyperbolic sine for all of those values I can just pass in X and I’ll get back hyperbolic sine for all of those values. So that’s how you define your own function, probably yours in the future will be more complicated than that but hopefully that was a useful simple example. So we’re going to do one more function example here it will illustrate some new concepts. So I’m going to instead of going clicking the new button and choosing function I’m actually going to right-click on the workspace and I can see an option to create a new file and I can create a new function that way. So I’m going to call this new function is even and so you can see matlab has already replaced so before when we created a function from a new file it called it untitled, but now that I’ve given it a name to start matlab already knows to change the function name to that name. Ok, and I’m only going to have one output argument again so I can get rid of these brackets. The input argument is going to be X, and I’m going to add a comment returns 1 if X is even. Ok, so if you’ve done programming before you’ve probably heard of the modulo operator although I haven’t mentioned it in this tutorial series yet it does exist in matlab, so in matlab because the % sign is used for comments we do not use the % for modulo, if you’ve done that in c or java before. So instead we use modulo by calling a function called mod. So, basically it returns us the remainder. Ok, so this should be very simple. We want to return 1 if X is even so we know we want to mod 2, so y = mod(X, 2). Ok, but we have a problem here because when you take the modulo of an even number with two you will get 0, but if you take an odd number modulo 2 you get one so that’s kind of the opposite of what we want, but what we can do is we can use the utility operator which does a logical negation. So, until the 0 gives us one, until the one gives us 0, so what we want is to put the tilde in front of the mod, ok. And so now if I call is_even(1,2,3,4,5) we should get ones in the spots where the input is even which we do. So for 2 and 4 we get 1, for 1, 3 and 5 we get 0.