In this video, we are going to see how to optimize the nonlinear function in Python
Last time we saw how to optimize the linear programming function. This time we are going to see how to optimize the nonlinear function
So we will import the matplotlib and the numpy library and the function that I am using is
x square plus 10 into sin x. And we will just take a few data points from minus 10 to 10, 50 data points and we will
see how this function looks like. So when you plot it, this is the kind of function you get
Now we want to find the minima of this function. So we want to get to this particular point
So in the scipy, let us import scipy.optimize as opt. And to find the minima of the function, first we will use the bsgs method and it is the
patron saint for the people who have created this method. What is the starting point taken as is, at which point you want to start
So we start at x is equal to 0. We start at this point and then we try to find the minimum of this function
So your initial guess is you are passing a list of values which is 0 and you want to
say the maximum number of iterations. All right. And it says optimization terminated successfully
We can look at the value of x and the value of the function and we will also plot this
function and we will see where it has find the optimum value
So it has found that x optimum is at minus 1.3 and the function value is minus 7.9 and
all right. So there is some deprecation going which I will have to look into where it is coming from
For now we will ignore it. So this is your starting point at x is equal to 0, your function value is 0 and at this
point is where it found the minima. And this is the correct minima given on the type of function that we have
But that is when we started at x equal to 0. Suppose we start at x equal to 6, okay, somewhere here, x equal to 6, then does it find the
local minima or does it find the global minima, okay. So we will start at x equal to 6 and we will again find again plot this
So you see here you get, you started at x equal to 6, now it is stuck in the local minima
So your starting point really plays an important role in the BX in this particular method when
you optimize using the BX-PS method. Now there is another method which we can use that is based on the stochastic this method
the optimization method based in Hopkins. It is based on the stochastic kind of a method
Now here the important parameter it takes is the temperature parameter, okay
So my x0 is still 6, I have not re-utilized this value, my x0 is still 6 and at this value
we want to find out if this particular method finds the global minima
So let us plot it in pink. It does find the global minima at this one
Now what needs to be further examined is if suppose you had x is equal to let us say this
minus 4, would it be able to find out the global minima
There are many such optimization methods in the optimized library of PsiPhi and you can
look into each of these methods and based on the feasibility of your problem you can
find out which method is best used. At least in this problem, Baxman-Hopkins is giving us a very good result, all right
So let us say x is equal to minus 8.5. I think there are about 12 to 15 kind of methods which you will find in the optimized library
and each of them is slightly different from the other. So this particular method is based on the stochastic way of finding out the minima, okay
I hope this helps you. Thank you for watching