0

I want to solve the following expression using sympy. I have defined all the symbols and functions. c,x,R are functions and f is the equation I want to solve for p. I will have two possible p which I want to plot like the following. p is also between 0 and 1 and I didn't put that and don't know how to define that. x is the derivative of c function wrt k and I have put the inverse of it in the f.

enter image description here

Code:

import sympy as sym
sym.init_printing()

p,q,a,c0,c1,z,k = sym.symbols('p,q,a,c0,c1,z,k')

c = sym.Eq(c0+0.5*c1*(1/k**2))

x = sym.diff(c,k)

R = sym.Eq(z*(a+x**(-1)))

f = sym.Eq((1-p+p*q)*(a+x**(-1))-c-(1-p)*R)

sym.solve([f],(p))
Parzival
  • 1,401
  • 1
  • 7
  • 18
Ali
  • 1
  • 1
  • This should answer your question. https://stackoverflow.com/questions/19553652/sympy-limit-symbol-variable-to-interval – Tim Roberts Apr 27 '21 at 23:09
  • You are mixing equations and expressions e.g. R is an equation but you use it as an expression in `(1-p)*R`. I don't know what you think that means but probably you just don't need to use `sym.Eq` at all. – Oscar Benjamin Apr 27 '21 at 23:37
  • thanks. how can I put a function in an equation? – Ali Apr 28 '21 at 14:49

1 Answers1

0

Perhaps seeing a concrete working example will help you clear up your problem statement. The expression you are solving is linear in p so there will be only a single solution. Let e1 and e2 be two functions of x (instead of p). If you want to know where they cross you need to know when e1 == e2 or, equivalently, when e1 - e2 = 0:

>>> from sympy import solve, solveset, Interval
>>> from sympy.abc import x
>>> e1 = 1/x
>>> e2 = (5*x + 9)/(25*x - 5)
>>> solve(e1 - e2)
[8/5 - sqrt(39)/5, sqrt(39)/5 + 8/5]  <--- two solutions but one is larger than 1
>>> solveset(e1-e2, x, Interval(0,1))
FiniteSet(8/5 - sqrt(39)/5)  <--- single solution in range of interest
smichr
  • 10,440
  • 2
  • 17
  • 24
  • many thanks. it works. How can I plot this using sympy? you can see the figure I am looking for in the question. – Ali Apr 28 '21 at 22:19
  • `help(plot)` indicates that to plot two functions you do something like `plot(x**2, 1/x, (x, 0.1, 1))`. – smichr Apr 29 '21 at 01:32