My code is really simple:
import numpy
def f(x):
return x**2 +1
f_min=10
for x in numpy.arange(-1,10,0.1):
if f(x)<f_min :
f_min=f(x)
x_min=x
else:
print (x_min)
It gives me the correct result (x-->0) but not only once but alot of times. Why is that and how can I keep it from doing so?
Because you told it to. :-)
Your if statement reads:
Any time you don't find a new minimum, you print. Since this function has its minimum early in your range, you get a lot of output.
If you want the global minimum printed only once, then do it only once: move it outside of the loop.