0

I have two simple arrays in Python and I would like to minimize the sumproduct of these arrays with respect to given target value by changing the values in the first array. Here is an example:

import numpy as np
from scipy.optimize import fmin

def func2(params):
    a, b, c = params
    arr1 = [a, b, c]
    arr2 = [150, 200, 230]
    res = sum(np.multiply(arr1, arr2))
    tar = 2
    error = res - tar
    return error

initial_guess = [0.0025, 0.0030, 0.0035]
finarr = fmin(func2, initial_guess) 
print(finarr)

The code above runs but I receive wrong results because the numbers in first array should be ~ 0.0027, 0.0033 and 0.0040. I would be grateful if someone can help me. Thank you.

1
  • Why should the solution be [0.0027, 0.0033, 0.0040] and not [0, 0.01, 0]? Commented Sep 15, 2021 at 15:04

1 Answer 1

1

You need to return the absolute value of the error in func2.

error = abs(res - tar)
Sign up to request clarification or add additional context in comments.

2 Comments

You're welcome. notice that output depends on your initial guess. for instance, if you negate all elements in the initial guess, you would get different results as the error is independent of the sign of the result in comparison with the target value.
For me, that is an important additional information. Many thanks for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.