Sujet : Re: Beazley's Problem
De : no.email (at) *nospam* nospam.invalid (Paul Rubin)
Groupes : comp.lang.pythonDate : 21. Sep 2024, 14:45:59
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <87tte941ko.fsf@nightsong.com>
References : 1
User-Agent : Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
ram@zedat.fu-berlin.de (Stefan Ram) writes:
Alright, so here's how I approached it: We know that when the
price x is 5 bucks, the number of people n is 120 (^1).
That assumption doesn't seem so good, but accepting it, your answer
looks right. Here is a pure numerical solution. Since the profit
function is quadratic, the Newton iteration converges immediately.
================================================================
def cost(n): return 180+.04*n # cost to show to n viewers
def revenue(price,n): return price*n # amount collected from them
def people(price): return 120.+(price-5)*(-15./.1) # number who will attend
def profit(price):
n = people(price)
return revenue(price,n) - cost(n)
def ddx(f,x,h=0.001): return (f(x+h)-f(x-h))/(2*h) # numerical derivative
def newton(f,x0): return x0 - f(x0)/ddx(f,x0) # Newton-Raphson iteration
def dprofit(price): return ddx(profit, price) # derivative of profit
x = 5.
for i in range(3):
print(f'{i} {x:.4f} {profit(x):.1f} {dprofit(x):.1f}')
x = newton(dprofit,x)