Home > Mobile >  Error while trying to use Sympy for the Law of Supply and Demand in Python
Error while trying to use Sympy for the Law of Supply and Demand in Python

Time:01-22

I am trying to solve an exercise about the Law of Supply and Demand

I have the following equations:

Supply: S(q):(q**2) β 10

Demand: D(q):(q - 20)**2

I want to solve it with β values to be from 0 to 10.

I made the following code

import sympy as sy

def S(q, β):
    return (q**2)   β   10

def D(q):
    return (q - 20)**2

for i in range(11):
    β = i
    q = sy.Symbol('q')
    eq = sy.Eq(S(q,β), D(q))
    q_sol = sy.solve(eq)
    p_sol = S(q_sol[0], β)
    print(q_sol, p_sol)

But I get the following results instead of a float:

[39/4] 1681/16

[389/40] 168921/1600

[97/10] 10609/100

[387/40] 170569/1600

[193/20] 42849/400

[77/8] 6889/64

[48/5] 2704/25

[383/40] 173889/1600

[191/20] 43681/400

[381/40] 175561/1600

[19/2] 441/4

CodePudding user response:

You are seeing the exact Rational numbers that SymPy can work with. To convert them to Float, use the n or evalf method:

>>> from sympy import Rational
>>> Rational(22, 7)
22/7
>>> _.n()
3.14285714285714
  •  Tags:  
  • Related