Q:

Numpy: Find the value needed in one equation which minimizes the error

the title is not clear, i hope to explain better here:
i have the two following arrays, ep and sp with the same dimension: 
ep = [0.00000000e+00, 4.29973987e-05, 1.77977219e-04, 3.08940223e-04, 4.44883670e-04, 5.84806153e-04, 7.28705999e-04, 8.77580573e-04, 1.03342551e-03, 1.19623754e-03, 1.36301748e-03, 1.53675860e-03, 1.72145026e-03. 1.91608833e-03]

sp = [336.17311024, 366.02001118, 427.4927458,  471.53403676, 503.53359236, 527.23879184, 544.98822976, 558.34153011, 568.29913137, 575.9109472, 581.00400657, 584.97104685, 587.14272582, 587.92832846]

I need to obtain an array sw as per the following formula:
sw = (np.amax(sp)/(ei**(ei+c))) * ((ep+ei)**(ei+c))

where c is the max value of ep array and ei has to be the value that minimize the sum of the following other equation (after the iteration for each value of sp and sw):
f = (sp - sw)**2

Any idea?
Thanks!

A:

How about something like this? You have already described your error function so can use scipy.optimize.minimize to minimize it:
from scipy.optimize import minimize
import numpy as np
from matplotlib import pyplot as plt

ep = np.array([0.0000000e+00, 4.29973987e-05, 1.77977219e-04, 3.08940223e-04, 4.44883670e-04, 5.84806153e-04, 7.28705999e-04, 8.77580573e-04, 1.03342551e-03, 1.19623754e-03, 1.36301748e-03, 1.53675860e-03, 1.72145026e-03, 1.91608833e-03])

sp = np.array([336.17311024, 366.02001118, 427.4927458, 471.53403676, 503.53359236, 527.23879184, 544.98822976, 558.34153011, 568.29913137, 575.9109472, 581.00400657, 584.97104685, 587.14272582, 587.92832846])

def err(ei, c):
    sw = (sp/ei**(ei+c))*((ep+ei)**(ei+c))

    return np.sum((sp-sw)**2)

# do minimization
c = max(ep)
guess = [1.2]
res = minimize(err, guess, args=(c,), method='Nelder-Mead')
# get miniization result
ei, = res.x

# plot results
fig, ax = plt.subplots(ncols=2)
ax[0].plot(sp)
ax[0].plot((sp/ei**(ei+c))*((ep+ei)**(ei+c)))
ax[0].set_title('Function evaluation')

ax[1].plot((sp/ei**(ei+c))*((ep+ei)**(ei+c)) - sp, label='Minimized')
ei, = guess
ax[1].plot((sp/ei**(ei+c))*((ep+ei)**(ei+c)) - sp, label='Initial Guess')
ax[1].set_title('Difference')
ax[1].legend()

