Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sympy: How to substitute function with function?

Tags:

python

sympy

Say I have a function like this:

In [44]: eta(p)
Out[44]: 
                                ⎛                  Λ⋅(-F(p) + 1)    ⎞  -Λ
                1               ⎝-Λ⋅(-F(p) + 1) + ℯ              - 1⎠⋅ℯ  
1 - ───────────────────────── + ─────────────────────────────────────────
      ⎛      Λ    ⎞  -2⋅Λ                 ⎛     -Λ⎞                      
    - ⎝-Λ + ℯ  - 1⎠⋅ℯ     + 1             ⎝1 - ℯ  ⎠⋅(-F(p) + 1)  

I would like to plot the function under some simplifying assumptions. I know that I can use subs() to replace the variables like Lambda with real numbers. That leaves me with F(p).

How can I replace the sympbolic F(p) with an actual numerical function?

like image 517
FooBar Avatar asked Sep 27 '26 02:09

FooBar


1 Answers

You can use lambdify() and make it use numpy.

Here is a simplified example:

import sympy as sy
import numpy as np
import matplotlib.pyplot as plt


t, omega = sy.symbols('t omega')

func = sy.sin(omega * t)

func_sub = func.subs({'omega': 2 * sy.pi})

numpy_func = sy.lambdify(t, func_sub, modules='numpy')

px = np.linspace(0, 1, 1000)

plt.plot(px, numpy_func(px))
plt.show()
like image 80
MaxNoe Avatar answered Sep 28 '26 15:09

MaxNoe