0
votes

I need to minimize the function func(x) with randomly initialised guessed data "x"

np.random.seed(1234)
m = 500    #500
n = 100    #100
x = np.asmatrix(np.random.randint(500,1000,size=(n,1)))

def func(x):
    A = np.asmatrix(np.random.randint(-10,-1, size=(n, m)))
    b = np.asmatrix(np.random.randint(500,10000,size=(m,1)))
    c = np.asmatrix(np.random.randint(1,10,size=(n,1)))
    fx = c.transpose()*x - sum(np.log10((b - A.transpose()* x)))
    return fx
sc.optimize.fmin_cg(func,y)

I am faced with this error "ValueError: shapes (1,100) and (1,100) not aligned: 100 (dim 1) != 1 (dim 0)" Not sure how scipy expects the data. I am new to scipy. It would be great if someone could point to the right direction.

1

1 Answers

1
votes

Something like this should do:

import scipy.optimize
import numpy as np

np.random.seed(1234)
m = 500    #500
n = 100    #100
x0 = np.random.rand(n)

A = np.asmatrix(np.random.randint(1,10, size=(n, m)))
b = np.asmatrix(np.random.randint(500,10000,size=(m,1)))
c = np.asmatrix(np.random.randint(1,10,size=(n,1)))

def func(x, A, b, c):
    fx = np.dot(c.T, x) - np.sum(np.log10((b - np.dot(A.T, x))))
    return fx


res = scipy.optimize.fmin_cg(func, x0, args=(A,b,c), maxiter=5)