mardi 29 décembre 2020

How can i update an differential equation variable during the odeint operation using 'if" condition in python?

I have these differential equations:

a=5*2.17*(10**-6)*n1*nx 
b=4*2.17*(10**-6)*n1*ni
c=n1/(0.154*(10**-7))
h=((8*(10**-7))/(3.14*t))**0.5
dn1dt=((1.7565*(10**18))-(n1/10**-6)-a-(19*b)-(c*h))
dnxdt=b
dxdt=(4*2.17*(10**-6)*n1)

initially ni=0 but i want to update it at each step according to the x that is calculated during odeint. here is my code:

function that returns dz/dt

enter code here
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import math
def model(z,t):
    n1=z[0]
    nx=z[1]
    x=z[2]
    ni=0
    a=5*2.17*(10**-6)*n1*nx
    b=4*2.17*(10**-6)*n1*ni
    c=n1/(0.154*(10**-7))
    h=((8*(10**-7))/(3.14*t))**0.5
    dn1dt=((1.7565*(10**18))-(n1/10**-6)-a-(19*b)-(c*h))
    dnxdt=b
    dxdt=(4*2.17*(10**-6)*n1)
    if x>18:
        ni=0.5*n1*(x-18)
    elif x<=18:
        ni=0
    dzdt = [dn1dt,dnxdt,dxdt]

    return dzdt

# initial condition
z0 = [0,0,0]

# time points
t = np.linspace(0.001,5)

# solve ODE
z = odeint(model,z0,t)

# plot results
plt.plot(t,z[:,0],'b-')
plt.plot(t,z[:,1],'r--')
plt.ylabel('response')
plt.xlabel('time')
plt.show()

someone please help!!

Aucun commentaire:

Enregistrer un commentaire