0

fallowing this question, I've solved the first two equations obtained dydt as an array and wrote this code for the third one:

#some instants
g=1e-25
k=4.14*(10**-5)
#steps
steps=np.logspace(31.247237,33.35443,490000)

ini_values=[1/np.sqrt(2*k),0.7071067811865476]

def func(var,steps,dydt):
    u=var[0]
    dudt=var[1]
    return [dudt,-((k**2)+(g*abs(dydt)/k))*u]
#make t and u have same dimension
u=np.empty_like(steps)
dudt=np.empty_like(steps)
#set initial values
u[0]=ini_values[0]
dudt[0]=ini_values[1]

this part solves the equation step-by-step; for example in step i we use answers of step i-1 as new initial values.

for i in range(1,len(steps)):
    #new steps
    new_steps=[steps[i-1],steps[i]]
    #solving equation
    sol=odeint(func,ini_values,new_steps,args=(dydt[i],))
    #saving data
    u[i] = sol[1][0]
    dudt[i] = sol[1][1]
    # next initial condition
    ini_values = sol[1]

but it takes too long time to get answer which is not efficient for me because I need to repeat this code like thousand times more, also I don't know even if that gives correct answer or not. Is this code correct? and if it is, is there any way to rewrite it to get answers faster?

danial
  • 21
  • 3
  • replace `-((k**2)+(g*abs(dydt)/k))*u` with `c*u`, and pass a precomputed `c` instead of `dydt` may save some evaluation time. – hpaulj Apr 18 '23 at 22:13
  • Since `ini_values` for one `odeint` call depend on the `sol` from the previous call, you can't perform multiple `odeint` in "parallel" or with a larger `var` array. Changing the `dydt` with each `odeint` call is, in a sense, the least of your problems. But that's just from a brief read of your question. – hpaulj Apr 18 '23 at 22:18
  • Can you use just one `odeint` call, and select the `dydt` value as a function of the `steps` value? – hpaulj Apr 19 '23 at 15:10
  • @hpaulj `dydt` and `steps` have same `len`, but I can't see any way to do that. – – danial Apr 20 '23 at 09:45
  • Careful, the global `steps` is not the same as the `steps` that `func` gets. – hpaulj Apr 20 '23 at 14:36
  • @hpaulj I have no idea what are you talking about "global steps" and "func steps", I've found [https://stackoverflow.com/questions/54808306/system-of-differential-equations-with-time-dependent-constants-in-arrays-using] which you have answered and thought it might solve my problem, so I gave it a shot, but It doesn't give answer unless atol<=1e-1 and also all answers are the same for `dudt` and `u` and even when I change `dydt` with another similar array. may I replace this code with the new one so you can see it? – danial Apr 21 '23 at 16:13
  • What I meant was that you should use a different variable name for `steps=np.logspace(31.247237,33.35443,490000)`, and `def func(var,steps,dydt):`. Use `step` or `aStep` or even `t`. `func` gets a single value provided by `odeint`; it may be in the `new_steps` range. – hpaulj Apr 21 '23 at 16:28
  • @hpaulj in the code which I ran , all variables have different names. so I don't think that be a problem. how about [https://stackoverflow.com/questions/54808306/system-of-differential-equations-with-time-dependent-constants-in-arrays-using]? I used `solve_ivp`. – danial Apr 21 '23 at 16:58

0 Answers0