0

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!

MatFer
  • 3
  • 4
  • Try building insight for the problem by plotting `f` as a function of a range of `ei` values. That may help you determine how difficult the optimization challenge will be, and will also give you some hints about its optimal value. – AbbeGijly Apr 30 '20 at 17:10
  • after that check out the `scipy` `optimize` module, which provides a number of algorithms to find the best `ei`. – AbbeGijly Apr 30 '20 at 17:12
  • also watch out for the `^` operator. In matlab it means "raise to the power" but in python it means "exclusive or". use `**` or `numpy.power()` instead – AbbeGijly Apr 30 '20 at 17:17
  • @AbbeGijly yes, i konw about the ^ symbol. I replaced it with ** in the question in order to avoid confiusion for other readers. – MatFer May 01 '20 at 08:00

1 Answers1

0

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()

minimized fn

Paddy Harrison
  • 1,808
  • 1
  • 8
  • 21
  • Ok thanks, seems good. I'll try some set of data and compare them to the optimization done previously by excel solver. Do you suggest the Nelder-Mead method of optimization in particular or are there any other methods that you can suggest? Thanks again. – MatFer May 01 '20 at 07:55
  • Problem: c is a constant number, the max of ep array, and it has to remain that value. If i try to delete it from the arg and res i have these errors related to the line of the sw calculation: 1)RuntimeWarning: overflow encountered in power - 2)RuntimeWarning: invalid value encountered in multiply – MatFer May 04 '20 at 08:03
  • I see. You can add constants to the minimisation function as extra arguments, and pass them through `args` of the `minimize` function. I have edited the above to acommodate for this. The difference is that the first argument of your minimisation function are the parameters that are minimised, so by adding `c` as a separate argument we can keep it constant. Just a note, the minimisation of `ei=143.0` in this case, but the overall minimisation isn't as good as when minimising with `c` as well. The total sum of differences is smaller in the minimised case. – Paddy Harrison May 04 '20 at 10:04
  • My mistake, i found the cause of the error: the formula was wrong. It wasn't correct to put sp (array) in the sw calculation, I had to replace it with the max value of sp. Doing so it works perfectly without warnings. Thanks again. – MatFer May 11 '20 at 09:45