Minimizing a function in python

  • Context: Python 
  • Thread starter Thread starter ver_mathstats
  • Start date Start date
  • Tags Tags
    Function Python
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 3K views
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.
 
Physics 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.