Home > Net >  Plotting ODE with different arguments in matplotlib
Plotting ODE with different arguments in matplotlib

Time:04-11

I would like to plot this differential equation dAdt = r-c*A, making different plots for different values for c

#where 
c = cs(BW/70)^K 

This is how I calculated my c array

Height = [58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76]
K = 0.75
cs = 0.35
BW = [103,106.5,110,113.5,117.5,121,125,129,133,137,141.5,145,149.5,154,158.5,163,167,172,176.5] 
BW_1 = []

for i in range(0, len(BW)):
    BW_1.append(cs*(BW[i]/70)**K)
print(BW_1)

code 2- The code I have to solve my ODE with one c value

#Function that returns dA/dt
h = 7
def model(A,t):
    c=0.35
    r=10 if t < h else 0
    dAdt = r-c*A
    return dAdt

#initial condition 
A0=10

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

#solve ODE
A= odeint(model,A0,t)

#Plot 
plt.plot(t,A)
plt.xlabel('time(h)')
plt.ylabel('Amount of drug in the body, A(t) (mg)')
plt.show()

But I would like to plot multiple plots for my array of c values, how would I go about this?

CodePudding user response:

You can simply pass a list of c's as a parameter to your model function:

h = 7
def model(A,t, c_values):
    r=10 if t < h else 0
    dAdt_values = [r-c*A for c in c_values]
    return dAdt

Then if you want to make different plots just select the correct index of dAdt which will return the array of data you want.

CodePudding user response:

Just as you used a loop to compute BW_1, you can also loop to produce different plots.

def model(A,t,c,h):
    r=10 if t < h else 0
    dAdt = r-c*A
    return dAdt

#time points 
t=np.linspace(0,20)
#initial condition 
A0=10

for bw in BW:
    c= cs*(bw/70)**K
    
    #solve ODE
    A= odeint(model,A0,t, args=(c,h))
    
    #Plot 
    plt.plot(t,A, lw=1)

plt.xlabel('time(h)')
plt.ylabel('Amount of drug in the body, A(t) (mg)')
plt.show()

family of plots

  • Related