0

I am trying to minimize the "function()" with respect to two parameters. I have done so by creating mesh arrays and used them in the above "function()" to return similar meshed array values. However, upon using "fmin()" to find the minimum, the output says that the operators could not be broadcasted.

The code is pasted below:

import numpy as np

from scipy.optimize import fmin 

import matplotlib.pyplot as plt

i=0

x_values = np.arange(-10,10,2)

y_values = np.arange(-10,10,2)

x_mesh = np.empty((0,len(x_values)))

y_mesh = np.empty((0,len(y_values)))

for i in range(len(x_values)):
    y_mesh = np.vstack((y_mesh, y_values))
    
i=0

for i in range(len(y_values)):
    x_mesh = np.vstack((x_mesh, x_values))
    
y_mesh = np.transpose(y_mesh)
    

def function(x_mesh, y_mesh):
    
    
    return (2*x_mesh**2 + y_mesh**2)
    
''' Want to minimize function '''

x_start = np.zeros((len(x_values), len(y_values)))
y_start = x_start 

y = fmin(lamda x_mesh: function(x_mesh, y_mesh), (x_start, y_start), full_output = True, disp = 0) 

The output shown was:

  File "C:/Users/User/Documents/Year2/Programming/elrter.py", line 42, in function
    return (2*x_mesh**2 + y_mesh**2)

ValueError: operands could not be broadcast together with shapes (200,) (10,10) 
        

But why does this happen? What is the solution?

Karthik
  • 113
  • 1
  • 8
  • `fmin` expects to be passed the function, so `fmin(function, ...)` not `fmin(function(..), ...)`, the way you are calling it now you call it and pass the result numpy array to `fmin`. – Tadhg McDonald-Jensen Jul 16 '20 at 16:12
  • @TadhgMcDonald-Jensen I have edited the post again. I have used "lambda" instead now, because it's x_mesh that's being used. – Karthik Jul 16 '20 at 16:37

0 Answers0