Sigmoid Function in MATLAB

Tim Buchalka's Learn Programming Academy
A free video tutorial from Tim Buchalka's Learn Programming Academy
Professional Programmers and Teachers - 1.24M students
4.5 instructor rating • 53 courses • 1,253,752 students

Lecture description

Implement a 3-parameter sigmoid function

Learn more from the full course

Master MATLAB through Guided Problem Solving

Become an expert in MATLAB Programming and Scientific Computing. Advance your career in Engineering Physics Biology etc

37:55:43 of on-demand video • Updated July 2021

  • Intermediate to advanced MATLAB programming proficiency. This is *the only course* that develops intermediate and advanced programming skills.
  • Obtain real-world application experience that that researchers and industry professionals use MATLAB for, including signal processing, matrix decompositions, spectral analysis, linear and nonlinear model-fitting, and statistics.
  • Gain skills to solve challenging problems in MATLAB, as opposed to memorizing syntax rules.
  • Data science projects and problem-solving in MATLAB
English [Auto] The goal of this video is to generate and plot a sigmoid function in Matlab based on translating the mathematical formula for a sigmoid function, which I'll show you on the next slide. There are several parameters of a sigmoid function, and you can try playing around with those different parameters and see how that affects the plot. The bonus exercise is to plot dotted lines to indicate the most important point of a sigmoid function, which is sometimes called the inflection point, or it's sometimes called the equivalence point. Here you see the general form of a sigmoid function. It's also called a logistic function. It looks a little bit like an S. It has an asymptote to the left and to the right and a smooth function in between those two. This is the formula that defines a sigmoid function, the A and the B and C R parameters. So the main so the core idea of this function is that F of X is equal to one over one plus E to the minus X, and then there's the parameter A which encodes the height. So in this graph, A is set to one point for obviously the default is one. And then there is B, which is in this case that the two that's sometimes called the temperature or the heat parameter of the sigmoid function, that that encodes how fast this slope rises. So when B gets larger, then this function looks more like a square wave. And when B gets smaller, then this function as a more gentle slope increase, C corresponds to the center point. So you can see that the default would be C equals zero. And here C is minus one. So this center point here, this is the inflection point or sometimes called the equivalence point. So this point occurs where C is set to the bonus exercise is to draw this magenta line and this red line. And you can see these two lines will show the equivalence point of the inflection point. This is important because this is where the second derivative is zero. Good. Let's try to implement this in Matlab. So here we already have a few parameters set for us. This makes it a bit easier. We can see that X is going to vary from minus five to plus five in four hundred steps. And now here is where we want to write the main sigmoid function. So you might need to pause the video and go back to where the formula which showed if you need a refresher, it was basically a divided by some denominator and that denominator was one plus the two the something. So I often like to write out these lines of code like this. So you first get the general idea and then you keep filling in more and more details. So it was E to the minus B times X, except that X needs to be centered. So it's it was actually X minus C. You can see that there's quite a lot of parentheses going on in this line of code. That's why I like to write this piecewise first, get the general broad outline, make sure I have all of the parentheses in the right place, and then I go back and fill it in piece by piece. And then I can be more confident that I haven't made any simple mistakes with putting parentheses in the wrong place. All right. So let's see. I think we can already plot this, uh, this so there's one line that's already going to be drawn in here. Oh, no, this needs to be done. So here you go. This is already. A good start, we have our sigmoid function, we have a dotted line at X equals zero, so that successfully solves the basic problem. And now for the bonus exercise. First, I want to say a little bit about how I got this line to be to go from the bottom of the plot to the top of the plot. Now, you could say that this goes from you could write explicitly that this is a line that goes from the X part, goes from zero to zero. And the Y part, I could have specified this to go from zero to one point for just based on visual inspection. But if I change the A parameter, then that's no longer going to be a good way to solve this. So another solution would be just to write a in here that would work, but it doesn't work exactly because in fact, Matlab stretches this access limit up here a little bit. So that's why I wrote get get Kurn Access Vilem. So here's this get function. I'll talk more about this in the section on plotting, but basically it's extracting information out of the current axis. This is get current access. And what information do we want from the current axis. We want the limit of the Y axis. So you can see when I run this function, this gives me two numbers, zero and two point five corresponding to the lower limit and the upper limit. So by using this code, I can dynamically specify that the line should spend the entire y axis limit without having to know a priori what that limit actually is. And that's why this line of code works to draw this dotted black line. So the next one we need is another vertical line at the inflection point here. And remember that the inflection point here is what we specify as C, so that's the x axis shift. And let's see, I'll start with that. So I'm going to change this comment, actually make it a little bit more specific. So we want a vertical line at X equals C, so now I'm basically going to follow the same procedure that I did here. So plot except this is not going to be plotted at the x axis location of zero. It's going to be plotted at the x axis location of minus one, which goes to here. And it's actually just C so this is going to be C, C, and we still want it to go the entire length of the Y axis. So again, I'm going to get current axis y then and let's make this one a red dash line. So I run this code again. This looks correct. It useful to try changing around these different parameters, the C parameter, just to make sure that this line really is following the center, the inflection point of this curve plus two. And that looks good. So the next line that we wanted was a horizontal line at so where should this horizontal line be? This should be plotted at Y equals. If this is half of the height of the curve, then it ends up being a divided by two. So that's what we'll define this point over here. So how should we do this now? I don't want to specify the X coordinates to be a single point like this. Instead, it has to be two points and this is going to be from the beginning of the X limit to the end of the X limit. So I'm going to follow a similar procedure that I used here, except instead of getting the information from the Y limit, I want the X limit. And what should we do here? So this has to be a divided by two and a divided by two. And this was a magenta dashed line. Good. So that works. And, you know, I actually prefer doing something like this. I like to do one one times a divided by two, because then I only need to write a over two once. Of course this is equivalent. This is just a different way of conceptualizing it because I think of this line as being a line of unit one and then it's scaled by some parameter. But whatever you find more intuitive is fine. So again, we should try changing these different values a little bit just to make sure that this is really following the line. And it is. Now, I'll show you the steepness parameter. So this is said to be that. And let's go back. That's one point for this one. So here B was two. This is what the function looks like. Let's that B to be, let's say five. Now you can see this is increasing much more steeply. And as this the parameter is beta parameter, which is sometimes called the heat parameter, the temperature parameter, when it gets really high, it ends up basically being a perfect step function like this. And when the beta parameter is smaller. OK, now this is really small. Let's make it a little bit bigger than that. Let's try point five. Well, you can see if we would specify X to be longer, let's say minus 15 to plus 15. Now, you would see it again. So this is going this is increasing much more gradually anyway. This solves the exercise in this video. I hope you found this instructive.