Absolute Value

16 views
Skip to first unread message

Steve F

unread,
Oct 5, 2019, 2:52:15 AM10/5/19
to sympy
Hello,

    I am new to Python and Sympy and have looked around the web for a good bit and found a solution to this problem

Solve: Abs(2*x -4) =14.   

    I could do it this way:

#Solving a single Linear Equation for X.
from sympy.solvers import solve
from sympy import Symbol
x = Symbol('x')
solve(2*x - 4 -14,x)

and

#Solving a single Linear Equation for X.
from sympy.solvers import solve
from sympy import Symbol
x = Symbol('x')
solve(2*x -4  + 14,x)

But I wonder if there is a better way.

Also, I would like to plot this in Jupyter Lab/Python.

Thank you,
Steve.

Aaron Meurer

unread,
Oct 5, 2019, 2:55:59 AM10/5/19
to sympy
It works but for solve you need to set x as real using

>>> x = symbols('x', real=True)
>>> solve(abs(2*x - 4) - 14, x)
[-5, 9]

alternately you can use solveset

>>> solveset(abs(2*x - 4) - 14, x, S.Reals)
{-5, 9}

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/ad9480bc-f268-455e-b056-e5abcc73c173%40googlegroups.com.

Chris Smith

unread,
Oct 5, 2019, 7:44:50 AM10/5/19
to sympy
As noted, this will work if you make `x` real. You can also state the equation as an Equality and solve it:

>>> var('x', real=True)
x
>>> solve(Eq(abs(2*x -4), 14))
[-5, 9]

/c
Reply all
Reply to author
Forward
0 new messages