Python Forum

Full Version: plot differential equation sympy
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,I am new to python.
I am using sympy for symbolic representation of a differetial equation. From what I understand I need to convert the symbolic representation to numerical representation through lambdify function and then plot it.
The code executes ok, until I try to plot the function. Any help is much appreciated. I am using an exponential function to model the decay of the antibiotic concentration after a bolus administration.

The code:
import numpy as np
import matplotlib.pyplot as plt
import sympy as sym
from sympy import *
sym.init_printing()

t = sym.Symbol('t')
k = sym.Symbol('k')

y = sym.Function('y') # y is a general function
f = y(t).diff(t) # differentiate the function
ode = sym.Eq(f, -k * y(t)) # I define the diff equality, y=-kt
resolve = sym.dsolve(ode, y(t))

ode_function_lam = lambdify(t, resolve.rhs, 'numpy')

x_value = np.linspace(1, 24)
y_value = ode_function_lam(x_value)

plt.plot(x_value, y_value)
plt.show()
The errors:
Error:
AttributeError: 'Mul' object has no attribute 'exp' .... TypeError: loop of ufunc does not support argument 0 of type Mul which has no callable exp method