I use the following example to try to solve the inequality containing tanh, but it produces the error
TypeError: Equation should be a polynomial with Rational coefficients
Anyone knows how to solve it ?
from sympy import Symbol, S
from sympy.solvers.solveset import solveset
x=Symbol('x')
v=0.05*x-8.5
solveset((1-(2*exp(-v))/(exp(v)+exp(-v)))>0, x, S.Reals)
You need to use exp from SymPy:
from sympy import Symbol, S
from sympy.functions import exp
from sympy.solvers.solveset import solveset
x = Symbol('x')
v = 0.05*x-8.5
solveset((1-(2*exp(-v))/(exp(v)+exp(-v)))>0, x, S.Reals)
which yields
Interval.open(170.000000000000, oo)
Having updated the sympy version to 1.9, the code finally works well for me.
Btw, in my case, in order to update the sympy, I should also update python to a newer version (currently 3.9.7) from my older version (3.7.1) since each python version seems to correspond to (a) certain sympy version(s).
Related
I like SymPy, but I have been running into issues with it when I solve partial differential equations. I've constructed a toy example:
from sympy import Function, Eq
from sympy.solvers import pdsolve
from sympy.abc import x, y, t
f = Function('f')
u = f(x,y,t)
ux = u.diff(x)
uy = u.diff(y)
eq = Eq(1 + (2 * (ux/u)) + (3 * (uy/u)), 0)
print(pdsolve(eq))
Which raises an error that tells me there does not exist an implementation for two variables.
NotImplementedError: Right now only partial differential equations of two variables are supported
Often systems of PDEs are only solvable with numerical methods, which packages like py-pde are helpful for approximating, but there are techniques that can be applied to various families of PDE's.
Is there a Python package that allows for systems solving systems of partial differential equations with multiple variables symbolically?
Multiplying a matrix with the imaginary unit raises an exception, i.e.,
import sympy as sy
M = sy.MatrixSymbol('M', 2, 2)
print(sy.I * M)
raises the exception
TypeError: Invalid comparison of non-real I
My current workaround is to replace sy.I with an i = sy.Symbol('i').
Is there a better way to use the MatrxixSymbol together with imaginary expressions?
Thanks.
PS: Conda Python 3.7.6 and Sympy version 1.5.1 is used.
This is a bug in SymPy. I've opened github.com/sympy/sympy/issues/18743 for it. The bug only occurs when printing the expression, so you can work around it by avoiding printing the expression.
I am trying to solve an equation for r when given values for x and y. to do this I am using the solve ability of sympy. the code that I have is
import numpy as np
import matplotlib.pyplot as plt
from sympy import solve
from sympy import Symbol
from sympy import acos,sin
x=2
y=-2
r=Symbol("r",real=True)
solve(r(acos(1.0-(y/r)))-sin(acos(1.0-(y/r)))-x)
when I run the code it gives me the error
'Symbol' object is not callable
line 10, in <module>
solve(r(acos(1.0-(y/r)))-sin(acos(1.0-(y/r)))-x)
the reason I import numpy and matplotlib is that I will use them later in my code. Thanks for any help.
The error directs you toward what to look for: a Symbol that is being called. In Python syntax this is a Symbol followed by pair of parentheses with one or more arguments between them. You probably intended to multiply by r in the first argument of the expression:
>>> solve(r(acos(1.0-(y/r)))...
^__make that r*acos(1.0-(y/r))...
An editor that highlights matching parentheses (like the online editor of Python code at repl.it) can be helpful in these circumstances. Parentheses are either grouping or, when following a Python name, acting as the delimiters for the arguments being passed to a function.
I am using Python to solve an equation. I added the 'Bessel function' in scipy.special, It was working. Now I want to find a variable using Bessel function. For example, I added the order(1) and value(0.44005058574) in Python, but it is not working. (in order to find the variable, I also used solver)
How I can solve the problem?
import numpy as np
import scipy.special as sc
import math
from sympy import Symbol
from sympy.solvers import solve
x=Symbol('x')
y=sc.jn(1,x)-0.44005058574
print(solve(x))
As the output is hinting, the function scipy.special.jn does not know how to handle the object x from simpy. Instead, you should use a numerical approach
>>> from scipy import optimize
>>> f = lambda x: sc.jn(1, x) - 0.44005058574
>>> root = optimize.newton(f, 1.0)
>>> print(root)
0.9999999999848267
import numpy as np
import sympy as sym
from numpy import sin
from sympy import symbols, diff
func = lambda x: sin(x)
x = symbols('x')
print diff(func(x),x)
This works if I replace my function with a polynomial, or if I place the trig function directly into the diff operator. But in this format I get AttributeError: sin.
Basically I think python can't recognize func as just being a trig function which it knows how to symbolically integrate. I could just have sympy import sin and then things would work, but then I'm stuck with func referencing sin in the sympy namespace and there are future things I want to do with func which require that it be defined using sin in the numpy namespace.
You should build your expression up symbolically using SymPy functions, and then use lambdify to convert them into things that can be evaluated with NumPy.
This simply isn't how you use sympy. You can't use normal functions in conjunction with numpy--you need to build symbolic expressions using the stuff it provides.
To write code to get the derivative of sin(x), you would do
import sympy as sym
from sympy import sin, symbols, diff
x = symbols('x')
print diff(sin(x), x)
If you have some other particular case you're having trouble with, you'll have to show it. You can't mix sympy with non-sympy stuff in this way, so there isn't some general feedback that can be provided.