Python Minimizing a function in python

  • Thread starter Thread starter ver_mathstats
  • Start date Start date
  • Tags Tags
    Function Python
Click For Summary
The discussion centers on minimizing the function f(x) = x^5 - 12x^3 + 7x^2 + 2x + 7, specifically within the interval [0, infinity). The initial code provided uses SciPy's optimize.fmin function to find the minimum, but there is confusion about how to properly incorporate the specified interval into the code. A participant suggests that the function is dominated by its fifth-degree term, indicating that for large values of x, the function will be positive and increasing. They recommend using a smaller interval with finer increments to achieve a more accurate estimate of the minimum value. The original code's interval of [-2, 1000] is noted as inadequate for the task. Overall, the discussion emphasizes the need for adjustments to the code to effectively address the interval constraint while minimizing the function.
ver_mathstats
Messages
258
Reaction score
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
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
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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
916
  • · Replies 5 ·
Replies
5
Views
3K