Dimitris
Dimitris

Reputation: 579

Solve a trancendental equation with sympy's solveset

I prepare some course notes for my students. They are about the solution of the ODE enter image description here

with N(0)=25 (initial condition to calculate arbitrary constant) and N(1)=25 (to get k). We have done it analytically; I want to present sympy's solution. I use

k = sp.Symbol('k', positive = 'True') 
t = sp.Symbol('t', positive = 'True')
N = sp.Function('N')
N0 = 25
N1 = 100
eqn = sp.Eq(sp.diff(N(t),t,1),k*N(t)*(1000-N(t)))   # differential equation
sol_part = sp.dsolve(eqn, ics={N(0): N0})  # particular solution for N(0)=25
sol_part_k = sol_part.subs(t,1)           # value of N(1)
sp.solveset(sol_part_k.rhs - N1,k)        # determining k

It is here my issue because sympy does not take into account that k is a positive declared symbol.

Any suggestions? Thank you very much.

Upvotes: 0

Views: 45

Answers (1)

Oscar Benjamin
Oscar Benjamin

Reputation: 14500

The solveset function does not directly look at assumptions on the unknown symbol but rather has a separate domain argument. The domain argument is the set from which solutions should be sought. By default the domain is assumed to be the complex numbers but you can specify any other set:

In [6]: solveset(sol_part_k.rhs - N1, k)
Out[6]: 
⎧n⋅ⅈ⋅π   log(13/3) │      ⎫   ⎧ⅈ⋅(2⋅n⋅π + π)   log(39) │      ⎫
⎨───── + ───────── │ n ∊ ℤ⎬ \ ⎨───────────── + ─────── │ n ∊ ℤ⎬
⎩ 500       1000   │      ⎭   ⎩     1000         1000  │      ⎭

In [7]: solveset(sol_part_k.rhs - N1, k, Complexes)
Out[7]: 
⎧n⋅ⅈ⋅π   log(13/3) │      ⎫   ⎧ⅈ⋅(2⋅n⋅π + π)   log(39) │      ⎫
⎨───── + ───────── │ n ∊ ℤ⎬ \ ⎨───────────── + ─────── │ n ∊ ℤ⎬
⎩ 500       1000   │      ⎭   ⎩     1000         1000  │      ⎭

In [8]: solveset(sol_part_k.rhs - N1, k, Reals)
Out[8]: 
⎧-log(3/13) ⎫
⎨───────────⎬
⎩    1000   ⎭

Upvotes: 1

Related Questions