【发布时间】:2022-12-02 10:50:28
【问题描述】:
For the time, t, from 0 to 30, z0 is a constant value of 6. For the time,t, from 30 to 100, z0 takes on the form of a time variable where z0 = 6exp(-0.5*(t-15)). I tried to implement the 'if' condition in my function but it does not seem to work. Is there anything I am doing wrong? Any help will be appreciated.
z0 = 6
z0_new = z0*np.exp(-0.5*(t-15))
def f(t,y):
return z0-y[0],3/y[0]-y[1]
fsol = solve_ivp(f,(0,100),[3,350])
plt.plot(fsol.t,fsol.y[0])
z0 = 6
def f(t,y):
if t>=30:
z0 = 6*np.exp(-0.5*(t-15))
return z0-y[0],3/y[0]-y[1]
fsol = solve_ivp(f,(0,100),[3,350])
plt.plot(fsol.t,fsol.y[0])
【问题讨论】:
-
I recommend finding a way to solve this problem without a global variable. You should implement the function
f()directly from the piecewise definition of your function. For example, nothing in yourf()function says anything about the interval from 0 to 30.
标签: python if-statement math ode