How to create Functions in Matlab

Hossein Tootoonchy
A free video tutorial from Hossein Tootoonchy
Invest in yourself, Join 35000 students in the community.
4.3 instructor rating • 4 courses • 31,209 students

Learn more from the full course

Become a Good Matlab Programmer in 30 days

This is the last time, you wish you could be a Matlab Programmer

13:39:38 of on-demand video • Updated March 2019

  • Matlab Programming
  • Working with M-Files, Simulation Files, Functions and Scripts
  • You are fully able to solve any engineering and technical project offered at University or College
English [Auto] Everyone will come to Mallah programming course I'm with center 20 and today we're going to start a new chapter on defining functions. We want to learn how to define functions in my lap and this video is going to be one of the most important ones in this series because from this day on we always use this function in our programming and it is one of those fundamental functions in programming. OK. So let's get started. You know when we define X equals three and then we type sign of X Matlab already knows what sign means. So when we pass an argument into that it already has and has the algorithm I just calculates it the same way this q r t. When we do it negative 5 for example. So Matlab returns a result for absolute value of negative 12. Matt already knows how to behave when an argument is passed to it. But today we are going to tell matlab to consider customize functions the way that we do that is through a function called inside. So what we can do is to name it like a like function or let's do my name Hosain equals. You can put any VAP any variable or name instead of my name here. I need you to put yours. So this function is going to be in line. So when you put inline open parentheses Matlab asks to insert the information. We want to put. So the first thing is going to be what's going to be the expression. What do you want me to do when you pass the arguments to me. So let's do something. We put a single quotation and that's our function to calculate absolute value of X because a single quotation and then we need to tell matlab What is the independent variable the independent variable is x. So it is something that we pass through mouth that to calculate. So what it does is what every value we put we insert into in line as x. Matlab is going to return the absolute value of that and what is the name of the function as Hosain mining. So let's do that. If I hit Enter now Hussein is defined as in one function or saying X equals ABS of x. So let's try to. If I say say Enough 3 then there is of this street because absolute value of three is free. How about Hosain of negative for we expect the answer to be 4. Because what it does it returns the absolute value of the input. The answer would be for let's create another one. Let's go with new. I call it new equals in line. What is the function that we gonna define. Let's do something like exponential of x negative ops. Absolute value of x plus security of x squared. That's enough. So this is going to be the function. And what is the independent variable here. It's going to be x. Ok so now I defined a new function called knew of x. Let's pass variables here knew of X of what this is going to be the answer. How about new of 0. Forget How about negative to you see by defining function. It's really easy for us to calculate different values very fast instead of just writing the whole function over and over again. We can define it as a new variable and just pass it to the function and also when you want to get rid of the function like New What. All we need to do as you see here it's now a recorded as as a function into workspace. All we need to do is to call clear and the name. If I do that just it removes look at the Hosain here. I just want to remove myself. Doesn't make sense but anyway so queer was saying and you know it's all gone and if I type a scene of free matter just doesn't know what I'm talking about so let's clear. So far we've learned how to deal with functions with only one independent variable. But how about if you have two or more in this section I'm going to show you how we can those as well. OK. So let's create another one called parabolic power of. And what it does I'm going to define two independent variables x and y. And when I pass this the function is going to calculate the sign of the first variable. Plastic cosign of the second very well and add them together. So let's do that. We type in line and it asks what kind of information we need to do. So we put two single quotation and comes between. Put two single quotations and in between the right sign of X plus cosign of y comma. And what are the arguments x is one of them and Y is the second. And if we do that we define the parabolic function. So let's go with parabolic of 1 and 0. The answer is going to be that. How about parabolic of 2 and 6. The answer is going to be that. And I think you get my point that we can define a lot of functions with different variables and we just the only thing we need to do is to pass the input to that function that's going to calculate the output very easily and conveniently.