Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a mathematical function in SymPy?

Tags:

python

sympy

I've been trying this now for hours. I think I don't understand a basic concept, that's why I couldn't answer this question to myself so far.

What I'm trying is to implement a simple mathematical function, like this:

f(x) = x**2 + 1

After that I want to derive that function.

I've defined the symbol and function with:

x = sympy.Symbol('x')
f = sympy.Function('f')(x)

Now I'm struggling with defining the equation to this function f(x). Something like f.exp("x**2 + 1") is not working.

I also wonder how I could get a print out to the console of this function after it's finally defined.

like image 406
Robin Avatar asked May 08 '16 12:05

Robin


People also ask

How do you use SymPy to define a function?

sympy. Function is for undefined functions. Like if f = Function('f') then f(x) remains unevaluated in expressions. Then f(Symbol('x')) will give a symbolic x**2 + 1 and f(1) will give 2 .

How do you evaluate a function in a SymPy?

To evaluate a numerical expression into a floating point number, use evalf . SymPy can evaluate floating point expressions to arbitrary precision. By default, 15 digits of precision are used, but you can pass any number as the argument to evalf .

How do you display formula in SymPy?

Although you first declare x as a sympy. Symbol , once you perform the assignment x=y+z , x becomes an alias for y+z . Whenever you use x from that point after, x will be automatically translated by python as y+z . If you insist on this workflow, you could use Eq(S('x'),y+z) to display the equation.

How do you replace formula in SymPy?

One of the most basic operations to be performed on a mathematical expression is substitution. The subs() function in SymPy replaces all occurrences of first parameter with second. This function is useful if we want to evaluate a certain expression.


4 Answers

sympy.Function is for undefined functions. Like if f = Function('f') then f(x) remains unevaluated in expressions.

If you want an actual function (like if you do f(1) it evaluates x**2 + 1 at x=1, you can use a Python function

def f(x):
    return x**2 + 1

Then f(Symbol('x')) will give a symbolic x**2 + 1 and f(1) will give 2.

Or you can assign the expression to a variable

f = x**2 + 1

and use that. If you want to substitute x for a value, use subs, like

f.subs(x, 1)
like image 174
asmeurer Avatar answered Oct 19 '22 05:10

asmeurer


Here's your solution:

>>> import sympy
>>> x = sympy.symbols('x')
>>> f = x**2 + 1
>>> sympy.diff(f, x)
2*x
like image 20
enedil Avatar answered Oct 19 '22 05:10

enedil


Another possibility (isympy command prompt):

>>> type(x)
<class 'sympy.core.symbol.Symbol'>
>>> f = Lambda(x, x**2)
>>> f
     2
x ↦ x 
>>> f(3)
9

Calculating the derivative works like that:

>>> g = Lambda(x, diff(f(x), x))
>>> g
x ↦ 2x
>>> g(3)
6
like image 26
hochl Avatar answered Oct 19 '22 05:10

hochl


Have a look to: Sympy how to define variables for functions, integrals and polynomials

You can define it according to ways:

  • a python function with def as describe above
  • a python expression g=x**2 + 1
like image 29
Nadir SOUALEM Avatar answered Oct 19 '22 07:10

Nadir SOUALEM