Python Forum

Full Version: Model of supply and demand
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, can you help me figure out what is the problem in this code?
In a model of supply and demand,

the supply is:

Qs = P**a + P + 1

And the demand is:

Qd = 1/(0.5*P**2 + P)

I want to draw a sample of 1000 values for a assuming that a is a random variable which is uniformly distributed within interval [0.2,2.0]. Then I want to solve the equilibrium price for each by numerically finding the root of the excess demand function.

This is my code:
import numpy as np

a= np.linspace(0.2,2.0, num=1000)
z = 0.5  
    
    excess_demand = lambda W: 1/(z*P**2 +P)  - (P**(a) +P + 1)
    
    W, res = optimize.brentq(excess_demand, 1e-8, 1000.0, full_output=True)
    assert res.converged == True
    print(res)
    print(W) 
It appears to me an error? Can you tell me why?

This is the output:
Output:
excess_demand = lambda W: 1/(z*P**2 +P) - (P**(b) +P + 1) ^ IndentationError: unexpected indent
Line 6 is indented. No need for the indentation, so it says unexpected indent
And so what should I do?
(Nov-17-2021, 06:54 PM)maeva Wrote: [ -> ]And so what should I do?
Read this. Especially the part about indentation.
(Nov-18-2021, 08:50 AM)ibreeden Wrote: [ -> ]
(Nov-17-2021, 06:54 PM)maeva Wrote: [ -> ]And so what should I do?
Read this. Especially the part about indentation.

Thank you!
I wrote this code:
from scipy import optimize
import numpy as np

def main():
    a= np.linspace(0.2,2.0, num=1000)
    z = 0.5  
    
     
    excess_demand = lambda W: 1/(z*P**2 +P)  - (P**(a) +P + 1)
     
    W, res = optimize.brentq(excess_demand, 1e-8, 1000.0, full_output=True)
    assert res.converged == True
    print(res)
    print(W)
Now the problem is that P is undefined name (pyflakes E). I don't know how to solve it. Do you have any suggestions?