TransWikia.com

2nd order ODEs in Python - defining the differential

Stack Overflow Asked by a0142204 on November 16, 2021

I have the following differential equation:

y"(t)+b*y'(t)+a*y(t)=c(p(t)-p0)

with initial values y(0), y'(0). y(t) is to be found.

There are several ways to solve it, but my problem was with defining the following function based on it:

def Function(y,t):
    b=2
    a=3
    c=1
    pT=4
    p0=1
    dydy=-b*dy-a*y-c*(pT-p0)
    return dydy

dy is obviously not defined – how is it possible to define it?

2 Answers

Your state vector has two components, so the function you have to pass to odeint should be

def Function(y,t):
    ...
    return [ y[1], c*(pT-p0) - b*y[1]-a*y[0] ]

that is, also return the tuple of the derivatives of the two state components.

Answered by Lutz Lehmann on November 16, 2021

If you have y'(0) you can use that as the value for dy. Also, your notation is odd. y'(t) is not dy. y'(t) is dy/dt. Below, I have used yprime instead of dy.

Your function should probably be

def findYDoublePrime(y, yprime):
    b=2
    a=3
    c=1
    pT=4
    p0=1
    return -b*yprime-a*y-c*(pT-p0)

Answered by Allen Han on November 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP