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.1M students
4.5 instructor rating • 52 courses • 1,160,552 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 March 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 line. There are several parameters of a sigmoid function and you can try playing around 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 --. It has a asymptote to the left and to the right and a smooth function between those two. This is the formula that defines the sigmoid function. The A and B and C are parameters so the main. So the core idea of this function is that f of x is equal to 1 over 1 plus to the minus X.. And then there's the parameter a which encodes the height. So in this graph and gives that to 1.4 obviously the default is 1. 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 1 so the 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 to 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 and 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 show. If you need a refresher it was basically a divided by some denominator and that denominator was one plus he do 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 a to a minus B. Times x except that X needs to be centered. So it's it was actually X minus. 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. No this needs to be done. So here we 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 there 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 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 access limit up here a little bit. So that's why I wrote get get current access wind them. So here's the get function I'll talk more about this in the section on plotting. But basically it's extracting information out of the current Access's get current access and what information do we want from the current access. We want the limit of the y axis so you can see when I run this function. This gives me two numbers 0 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 spend the entire y axis limit without having to know apriori 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 when a change is coming. Actually I make it a little bit more specific so if you 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 0. It's going to be plotted at the x axis location of minus 1 which goes to here. And it's actually just seen. 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 whilom and let's make this one a red dashed line. So I run this code again. This looks correct. It's useful to try changing around these different parameters to see parameter. Just to make sure that this line really is following the center of the inflection point of this curve plus 2. And that looks good. So the next line that we wanted was a horizontal line. 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 will 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 Why limit i 1 the X limit. And what should we do here. So this has to be divided by two and divided by two. And this was a magenta dash line. So that works. And you know I actually prefer doing something like this and I to do 1 1 times a divided by two because then I only need to write OK over to 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 1 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 all set to the math. And let's go back. When we find this one. So here B was 2. This is what the function looks like. Let's say it beats a B let's say five I can see this is increasing much more steeply and as does the parameter this beta Promina which is sometimes called the heat primary 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 to point five. Well you can see if 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 and this is increasing much more gradually. Anyway this solves the exercise. In this video I hope you find this instructive.