-1

I just want to minimize a simple function, every example i' ve watch didnt get me anywhere.

import math
import numpy as np 
import sympy as sp
from scipy.optimize import minimize
import scipy.optimize as optimize

R=1.5
k_1=2
a=1
n=a
alpha=0.25
beta=0.5
delta=0.9

def f_gob(x, y, z):
    c_1=((1/x-y/x)+R*k_1)/(1+delta*(1+alpha)) 
    c_2=delta*x*(((1/x-y/x)+R*k_1)/(1+delta*(1+alpha))) 
    l=n-(alpha*(delta*x*(((1/x-y/x)+R*k_1)/((1+delta*(1+alpha))))))/(1-y) 
    return -1*(math.log(c_1)+delta*(math.log(c_2)+alpha*math.log(n-l)+beta*math.log(z)))

f_gob(0.9996,0.332,0.7765)

x0 = [0.8,0.2,0.6]

res =  minimize(f_gob, x0)

Thank you very much.

t.niese
  • 39,256
  • 9
  • 74
  • 101

1 Answers1

0

Better is:

def f_gob(a):
    x = a[0]
    y = a[1]
    z = a[2]
    c_1= ((1/x-y/x)+R*k_1)/(1+delta*(1+alpha)) 
    c_2=delta*x*c_1 
    l=n-(alpha*c_2)/(1-y) 
    return -1*(math.log(c_1)+delta*(math.log(c_2)+alpha*math.log(n-l)+beta*math.log(z)))

f_gob([0.9996,0.332,0.7765])

The main issue is that the current levels of the three decision variables x,y,z are passed on as a single array, which I call a. I just unpack the individual members to keep things close to what you had. Passing things on as an array makes sense, especially if you want to allow for large numbers of variables (say hundreds).

For further information see the documentation: the third sentence explains the format of the function to be called. Also check the examples.

Erwin Kalvelagen
  • 15,677
  • 2
  • 14
  • 39