2017-10-03 5 views
1

J'ai une certaine fonction, par exemple sin (b * x), avec sympy je reçois des expressions dérivées et antidérivatives, mais j'ai besoin de tracer ces 3 fonctions dans matplotlib. Mon problème est que je ne peux pas convertir correctement les fonctions en numpy afin de tracer dans matplotlib. J'ai suivi la documentation sur la page sympy avec la fonction lambify mais cela ne fonctionne pas. http://docs.sympy.org/latest/modules/utilities/lambdify.htmlComment convertir la fonction sympy en plot avec matplotlib?

J'ai ce 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() 

Il échoue dans ax.plot ValueError: séquence trop grand; ne peut pas être supérieur à 32

+0

Votre 'evalfunc' définition et les deux lignes suivantes soulèvent des erreurs pour moi sur sympy 1.0, 2.0.0 mpl, numpy 1.12.1. C'est parce que vous définissez 'b' à un entier dans la ligne précédente. Quelle version de ceux-ci courez-vous? – saintsfan342000

+0

J'utilise la canopée. J'ai Numpy 1.11.3-3 mpl 2.0.0-3 sympie 1.0-2. J'ai essayé d'utiliser b comme b = Symbol ('b') et j'ai eu AttributeError: l'objet 'Mul' n'a pas d'attribut 'sin' dans ax.plot – Reyjhonny

Répondre

0

Votre code n'est pas tout à fait un exemple de travail minimal, mais il ne nécessite que des modifications minimes pour fonctionner.

Vous devez déclarer votre b comme le symbole réel avant la dérivation. Vous l'avez défini comme b=5 avant l'évaluation numérique.

Voir:

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()