market.py

from scipy import optimize
from numpy import array, log

def objective(x):
    return -profit(*x)

def profit(p1, p2):
    x1 = demand1(p1, p2)
    x2 = demand2(p1, p2)
    income = x1*p1 + x2*p2
    cost = costf(x1, x2)
    return income - cost

def demand1(p1, p2):
    return 20*(20 - p1)

def demand2(p1, p2):
    return 40*(40 - p2)

def costf(x1, x2):
    c1 = 10*(x1**0.5)
    c2 = 20*(x2**0.5) + 2*x2
    cbase = 1000 + 5*(x1 + x2)
    return c1 + c2 + cbase

price = (optimize.fmin(objective, [1, 1]))

print 'prices:', price
print 'amounts:', demand1(*price), demand2(*price)