Home > Blockchain >  Plotting a System of Two Differential Eqns Python
Plotting a System of Two Differential Eqns Python

Time:07-27

I am having some trouble with a model I want to analyze. I am trying to plot two differential equations however I am very new to doing this and am not getting it to work. Any help is appreciated

#Polyaneuploid cell development during cancer
#two eqns
#Fixed Points:
#13.37526 
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt

def modelC(C,t):
  
  λc = 0.0601 
  K = 2000 
  α = 1 * (10**-4) 
  ν = 1 * (10**-6) 
  λp = 0.1 
  γ = 2

def modelP(P,t):
  
  λc = 0.0601 
  K = 2000 
  α = 1 * (10**-4) 
  ν = 1 * (10**-6) 
  λp = 0.1 
  γ = 2
  

#returning odes
  dPdt = ((λp))*P(1-(C (γ*P))/K)  (α*C) 
  dCdt = ((λc)*C)(1-(C (γ*P))/K)-(α*C)   (ν*P) 
  return dPdt, dCdt

#initial conditions

C0= 256 
P0 = 0 

#time points
t = np.linspace(0,30)

#solve odes
P = odeint(modelP,t,P0, args = (C0,))
C = odeint(modelC,t,C0, args= (P0,))

#P = odeint(modelP, P0 , t)
#P = P[:, 2]

#C = odeint(modelC, C0 , t)
#C = C[:, 2]


#plot results
plt.plot(t,np.log10(C0))
plt.plot(t,np.log10(P0))
plt.xlabel('time in days')
plt.ylabel('x(t)')
plt.show()

This is just what I have so far, and currently I am getting this error: ValueError: diff requires input that is at least one dimensional Any tips on how to get the graphs to show?

CodePudding user response:

You need to put your initial conditions in a list like so:

initial_conditions = [C0, P0]
P = odeint(modelP,t,initial_conditions)

you still have some error in your P function where try to access C which is not defined in the local scope of your function neither passed as an argument.

UPDATED

def modelP(P,t,C):
  
  λc = 0.0601 
  K = 2000 
  α = 1 * (10**-4) 
  ν = 1 * (10**-6) 
  λp = 0.1 
  γ = 2
  

#returning odes
  dPdt = ((λp))*P(1-(C (γ*P))/K)  (α*C) 
  dCdt = ((λc)*C)(1-(C (γ*P))/K)-(α*C)   (ν*P) 
  return dPdt, dCdt

#initial conditions

C0= 256 
P0 = 0 
Pconds = [P0]
#time points
t = np.linspace(0,30)

#solve odes
P = odeint(modelP,t, Pconds, args=(C0,))

CodePudding user response:

The solver deals with flat arrays with no inherent meaning in the components. You need to add that meaning, unpack the input vector into the state object, at the start of the model function, and remove that meaning, reduce the state to a flat array or list, at the end of the model function.

Here this is simple, the state consists of 2 scalars. Thus a structure for the model function is

def model(X,t):
    P, C = X
    ....
    return dPdt, dCdt

Then integrate as

X = odeint(model,(P0,C0),t)
P,C = X.T

plt.plot(t,P)
  • Related