Udemy

Sigmoid Function in MATLAB

A free video tutorial from Tim Buchalka's Learn Programming Academy
Professional Programmers and Teachers - 2.28M students
Rating: 4.5 out of 5Instructor rating
60 courses
2,312,133 students
Sigmoid Function

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 June 2026

Intermediate to advanced MATLAB programming proficiency. This is *the only course* that develops intermediate and advanced programming skills.
Experience with real-world applications including signal processing, matrix decompositions, spectral analysis, linear and nonlinear model-fitting, and statistic
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 the C are 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 1.4, obviously the default is one, and then there is B, which is in this case set to 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 has 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 or 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 5 in 400 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 was showed. If you need a refresher, it was basically a divided by some denominator and that denominator was one plus e to 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. 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's 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 0 to 0, and the Y part I could have specified this to go from 0 to 1.4 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 axis limit up here a little bit. So that's why I wrote get get current axis y lim 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. So this is get current axis. 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 2.5 corresponding to the lower limit and the upper limit. So by using this code, I can dynamically specify that the line should span 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, I'll 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 do get current axis y LIM And let's make this one a red dashed line. So I'll run this code again. This looks correct. It's 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. Here's 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 I'll set this to be that. And let's go back to this 1.4 minus one. So here B was two. This is what the function looks like. Let's set B to be let's say five. Now you can see this is increasing much more steeply. And as this B parameter, this beta parameter, which is sometimes called the heat parameter or 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, okay, now this is really small. Let's make it a little bit bigger than that. Let's try 0.5. Well, you can see if we would specify X to be longer, let's say -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.