Minimizing a function in python

In summary, the conversation discusses minimizing the function f(x)=x5-12x3+7x2+2x+7 on the interval [0, infinity). The speaker mentions finding the minimum value and comparing it to a calculator, but is unsure about how to incorporate the interval into their code. They receive suggestions to use a smaller interval and finer increments to get a better estimate of the minimum value. The conversation ends with the assurance that the speaker is on the right track.
  • #1
ver_mathstats
260
21
The function is f(x)=x5-12x3+7x2+2x+7.

I found the minimum of the function and compared the value to a calculator and it seemed okay. But I am confused as to how to incorporate the interval into my code. Has my code already sufficiently answered the question?

Code:
from scipy import optimize
import numpy as np
import matplotlib.pyplot as plt 

def f(x):
    return (x**5)-12*(x**3)+7*(x**2)+2*x+7

xdom=np.linspace(-2,2,1000)
plt.plot(xdom,f(xdom))

minimum=optimize.fmin(f,1)
print('The minimum:',minimum)

Thank you.
 
Technology news on Phys.org
  • #2
ver_mathstats said:
Homework Statement:: Minimize the function on the interval [0,infinity).
Relevant Equations:: Python

The function is f(x)=x5-12x3+7x2+2x+7.

I found the minimum of the function and compared the value to a calculator and it seemed okay. But I am confused as to how to incorporate the interval into my code. Has my code already sufficiently answered the question?

Code:
from scipy import optimize
import numpy as np
import matplotlib.pyplot as plt

def f(x):
    return (x**5)-12*(x**3)+7*(x**2)+2*x+7

xdom=np.linspace(-2,2,1000)
plt.plot(xdom,f(xdom))

minimum=optimize.fmin(f,1)
print('The minimum:',minimum)

Thank you.
I would get creative. Your function, ##f(x) = x^5 - 12x^3 + 7x^2 + 2x + 7## is dominated by the 5th degree term. Just in round number, ##6^5## is more positive than ##-12*6^3##, and the other terms are pretty much insignificant. Obviously, you can't have an interval that stretches from 0 to infinity, so the function should be positive and increasing steadily if ##x \ge 10##, just to pick a number.

I see that your interval is [-2, 1000] in increments of 2. With a smaller interval and finer increments, you should be able to get a better estimate of the minimum value. Otherwise, it looks like you're on the right track.
 
  • Like
Likes ver_mathstats
  • #3
Mark44 said:
I would get creative. Your function, ##f(x) = x^5 - 12x^3 + 7x^2 + 2x + 7## is dominated by the 5th degree term. Just in round number, ##6^5## is more positive than ##-12*6^3##, and the other terms are pretty much insignificant. Obviously, you can't have an interval that stretches from 0 to infinity, so the function should be positive and increasing steadily if ##x \ge 10##, just to pick a number.

I see that your interval is [-2, 1000] in increments of 2. With a smaller interval and finer increments, you should be able to get a better estimate of the minimum value. Otherwise, it looks like you're on the right track.
Oh okay, that all makes sense. Thank you for the response.
 

Related to Minimizing a function in python

1. How do I minimize a function in python?

To minimize a function in python, you can use the scipy.optimize.minimize function. This function takes in the function to be minimized, the initial guess for the minimum, and any additional constraints or arguments.

2. What is the difference between scipy.optimize.minimize and scipy.optimize.minimize_scalar?

scipy.optimize.minimize is used for minimizing multivariate functions, while scipy.optimize.minimize_scalar is used for minimizing a function with a single variable.

3. Can I use any function as the input for scipy.optimize.minimize?

Yes, you can use any function as the input for scipy.optimize.minimize, as long as it returns a scalar value. The function can also have multiple input variables.

4. How do I specify constraints for the function being minimized?

You can specify constraints for the function being minimized by passing in a constraints parameter to the scipy.optimize.minimize function. This parameter can be used to define equality or inequality constraints on the input variables.

5. How do I know if the minimization was successful?

The scipy.optimize.minimize function returns a OptimizeResult object, which contains information about the success or failure of the minimization. You can check the success attribute of this object to determine if the minimization was successful or not.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
2
Views
922
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
1
Views
607
Back
Top