1

I have certain function, for example sin(b*x), with sympy I get derivative and antiderivative expressions, but I need to plot these 3 functions in matplotlib. My problem is I can't convert correctly functions to numpy in order to plot in matplotlib. I have followed the documentation in sympy page with lambify function but it doesn't work. http://docs.sympy.org/latest/modules/utilities/lambdify.html

I have this code:

from sympy import Symbol, diff, integrate, sin, cos, Function
from sympy.utilities.lambdify import lambdify, implemented_function
from sympy.abc import x

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons

def signal(b,x):
    return sin(b*x)

def derivative(b,x):
    yprime = diff(signal(b,x), x)   
    return yprime

def antiderivative(b,x):
    anti = integrate(signal(b,x), x)
    return anti

b = 5

evalfunc = lambdify((b,x), signal(b,x), modules=['numpy'])
evalderiv = lambdify((b,x), derivative(b,x), modules=['numpy'])
evalantideriv = lambdify((b,x), antiderivative(b,x), modules=['numpy'])

axis_color = 'lightgoldenrodyellow'
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
fig.subplots_adjust(left=0.25, bottom=0.25)
t = np.arange(-10, 10, 0.001)

[line] = ax.plot(t, evalfunc(b,t), linewidth=2, color='red')
[line2] = ax.plot(t, evalderiv(b,t), linewidth=2, color='blue')
[line3] = ax.plot(t, evalantideriv(b,t), linewidth=2, color='blue')
ax.set_xlim([-10, 10])
ax.set_ylim([-5, 5])

ax.grid()
plt.show()

It fails in ax.plot ValueError: sequence too large; cannot be greater than 32

2
  • Your evalfunc definition and the next two lines raise errors for me on sympy 1.0, mpl 2.0.0, numpy 1.12.1. It's because you set b to an integer in the preceding line. What version of these are you running? Commented Oct 3, 2017 at 18:08
  • I'm using canopy. I have Numpy 1.11.3-3 mpl 2.0.0-3 sympy 1.0-2. I tried using b like b = Symbol('b') and I got AttributeError: 'Mul' object has no attribute 'sin' in ax.plot Commented Oct 3, 2017 at 18:11

1 Answer 1

2

Your code is not quite a minimal working example, but it requires only minimal changes to work.

You need to declare your b as real symbol before the derivation. You set it as b=5 before the numerical evaluation.

See:

from sympy import Symbol, diff, integrate, sin, cos, Function
from sympy.utilities.lambdify import lambdify, implemented_function
from sympy.abc import x

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons

def signal(b,x):
    return sin(b*x)

def derivative(b,x):
    yprime = diff(signal(b,x), x)   
    return yprime

def antiderivative(b,x):
    anti = integrate(signal(b,x), x)
    return anti

b = Symbol('b', real=True)

evalfunc = lambdify((b,x), signal(b,x), modules=['numpy'])
evalderiv = lambdify((b,x), derivative(b,x), modules=['numpy'])
evalantideriv = lambdify((b,x), antiderivative(b,x), modules=['numpy'])

axis_color = 'lightgoldenrodyellow'
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
fig.subplots_adjust(left=0.25, bottom=0.25)
t = np.arange(-10, 10, 0.001)

b = 5

[line] = ax.plot(t, evalfunc(b,t), linewidth=2, color='red')
[line2] = ax.plot(t, evalderiv(b,t), linewidth=2, color='blue')
[line3] = ax.plot(t, evalantideriv(b,t), linewidth=2, color='blue')
ax.set_xlim([-10, 10])
ax.set_ylim([-5, 5])

ax.grid()
plt.show()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.