Minimizing a function in python

  • Context: Python 
  • Thread starter Thread starter ver_mathstats
  • Start date Start date
  • Tags Tags
    Function Python
Click For Summary
SUMMARY

The discussion focuses on minimizing the function f(x) = x5 - 12x3 + 7x2 + 2x + 7 using Python's SciPy library. The user successfully finds a minimum value using the optimize.fmin function but seeks clarification on incorporating an interval constraint, specifically [0, ∞). It is recommended to adjust the interval to [-2, 1000] with finer increments for improved accuracy in estimating the minimum.

PREREQUISITES
  • Understanding of Python programming
  • Familiarity with the SciPy library, specifically optimize.fmin
  • Basic knowledge of mathematical functions and their properties
  • Experience with data visualization using Matplotlib
NEXT STEPS
  • Learn how to implement interval constraints in optimization using SciPy's optimize.minimize
  • Explore the use of NumPy for creating finer intervals with np.linspace
  • Study the behavior of polynomial functions to understand their minima and maxima
  • Investigate alternative optimization methods in SciPy, such as optimize.brent or optimize.minimize_scalar
USEFUL FOR

Students and professionals in mathematics, data science, or software development who are interested in function optimization using Python.

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   Reactions: 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.
 

Similar threads

  • · Replies 16 ·
Replies
16
Views
2K
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
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
1K
  • · Replies 5 ·
Replies
5
Views
3K