I tried to solve system of two differential equations using scipy.integrate.odient.
The results are far from my expectations as one can see from the plots I attached below.
I would be grateful for any assistance.
initial introduction to the ODE system.
This is the system of ODE
where alpha and beth functions are functions of the time t, and repsernting function of the input signal.
my plots for the signals in my code are:
The expected plots
my code:
import numpy as np
from scipy.integrate import odeint ,solve_ivp
import matplotlib.pyplot as plt
def normalize_array(A):
# Convert to NumPy array if it's not already
A = np.array(A)
# Calculate min and max
min_val = np.min(A)
max_val = np.max(A)
# Normalize
if max_val == min_val:
# Avoid division by zero if all values are the same
return np.zeros_like(A)
else:
return (A - min_val) / (max_val - min_val)
def solve_system_of_ODE(init_vec,t,alpha_func,beth_func,gamma,delta):
# Define the system of differential equations
def system(init_vec,t,alpha_func,beth_func,gamma,delta):
x,A = init_vec
dydt = [alpha_func(t)*(A-x)-beth_func(t)*x-gamma*x+delta*(1-A),
delta*(1-A)-gamma*x]
return dydt
# Solve the system of differential equations
solution = odeint(system,init_vec,t,args=(alpha_func,beth_func,gamma,delta))
return solution
# Initial conditions
init_vec = [0,0]
# Time points where the solution is evaluated
t = np.linspace(0,50,500)
signals=[lambda t: 1 / (1 + np.exp(-5 * (t - 10))),
lambda t: 0.3*np.sin(t/3)+ 0.25*np.sin(t/2)+np.sin(t/5)+np.sin(t/10),
lambda t: np.sin(t/3)]
n_signals=len(signals)
delta=0.2
gamma=0.4
X_lst,A_lst=[],[]
for i in range(len(signals)):
alpha=signals[i]
beth=signals[i]
solution=solve_system_of_ODE(init_vec,t,alpha,beth,gamma,delta)
X = solution[:,0]
X=normalize_array(X)
A = solution[:,1]
A=normalize_array(A)
X_lst.append(X)
A_lst.append(A)
fig, axes = plt.subplots(2, n_signals, figsize=(10, 5))
for i in range(len(signals)):
y=signals[i](t)
axes[0,i].plot(t,y)
axes[0, i].set_title(f" signal({i+1})")
axes[0, i].grid(True)
axes[0, i].set_xlabel('t')
axes[0, i].set_ylabel('u(t)')
#plot the results
axes[1,i].plot(t, X_lst[i], label='X(t)')
axes[1,i].plot(t, A_lst[i], label='A(t)')
axes[1,i].set_title('Solutions ')
axes[1,i].set_xlabel('t')
axes[1,i].set_ylabel('Solutions X, A')
axes[1, i].grid(True)
axes[1, i].legend()
plt.tight_layout()
plt.show()
I tried to change the initial condition of the ODE system – but it is not help me.
David Ben-Michael is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.