Python Forum
plot differential equation sympy
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
plot differential equation sympy
#1
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python sympy problem for symbolic equation definition joellapointe_engineering 0 195 Mar-24-2024, 11:09 PM
Last Post: joellapointe_engineering
  Sympy nonlinsolve does not return explicit solution axelle 1 2,232 Jan-20-2021, 11:38 AM
Last Post: Serafim
  construction of Neural Network for solving Differential equations arshad 0 1,615 Jun-04-2020, 09:20 AM
Last Post: arshad
  Attribute error - Sympy VictorG8 1 4,983 Apr-09-2020, 06:03 PM
Last Post: snippsat
  How to use list of symbols for sympy calculation in python? tanmaybhise 1 2,795 Mar-01-2020, 10:36 PM
Last Post: Gribouillis
  Next zero of a function on sympy VicenteM 0 2,075 Aug-21-2019, 04:58 PM
Last Post: VicenteM
  Need Help solving second order differential equations SkewedZone 2 3,104 Jun-25-2019, 12:14 PM
Last Post: SkewedZone
  sympy psosmol 9 5,012 Mar-11-2019, 11:09 PM
Last Post: scidam
  Fitting experimental data with differential equations : Optimization madoko 6 6,842 Jan-17-2019, 11:30 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020