Skip to content

Commit 764ebe8

Browse files
committed
Fix SymPy OverflowError in solow.md by using Rational
The SymPy solve() function was failing with 'mpz too large to convert to float' when using Python float values (0.3, 0.5, 2.0) in symbolic expressions with fractional exponents. Using sympy.Rational for exact arithmetic avoids the large intermediate mpz values that caused the overflow in factorint().
1 parent c79d1da commit 764ebe8

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

lectures/solow.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -510,21 +510,26 @@ plt.show()
510510
One can also try to solve this mathematically by differentiating $c^*(s)$ and solve for $\frac{d}{ds}c^*(s)=0$ using [sympy](https://www.sympy.org/en/index.html).
511511

512512
```{code-cell} ipython3
513-
from sympy import solve, Symbol
513+
from sympy import solve, Symbol, Rational
514514
```
515515

516516
```{code-cell} ipython3
517+
# Use Rational for exact symbolic computation
518+
A_sym = Rational(2)
519+
alpha_sym = Rational(3, 10)
520+
delta_sym = Rational(1, 2)
521+
517522
s_symbol = Symbol('s', real=True)
518-
k = ((s_symbol * A) / delta)**(1/(1 - alpha))
519-
c = (1 - s_symbol) * A * k ** alpha
523+
k = ((s_symbol * A_sym) / delta_sym)**(1/(1 - alpha_sym))
524+
c = (1 - s_symbol) * A_sym * k ** alpha_sym
520525
```
521526

522527
Let's differentiate $c$ and solve using [sympy.solve](https://docs.sympy.org/latest/modules/solvers/solvers.html#sympy.solvers.solvers.solve)
523528

524529
```{code-cell} ipython3
525530
# Solve using sympy
526531
s_star = solve(c.diff())[0]
527-
print(f"s_star = {s_star}")
532+
print(f"s_star = {float(s_star)}")
528533
```
529534

530535
Incidentally, the rate of savings which maximizes steady state level of per capita consumption is called the [Golden Rule savings rate](https://en.wikipedia.org/wiki/Golden_Rule_savings_rate).

0 commit comments

Comments
 (0)