0

Does any equation solver work for a timestep case?

I've been implementing ODEint, Solve_ivp and even sympy to solve a first order diff.eq like this :

dTsdt = Ts* A - B + C # Set up in a function. This is sort the mathematical model.

where A,B,C are vectors that depend on time(e.g. A[1,3,4,5 ...]). tloop=[t[i-1],t[i]]

Sol_Ts = solve_ivp(dTsdt,tloop,[Ts0],args=(A[i],B[i],C[i],))

I just wonder, if this approach is correct to solve the equation at every timestep. As I am replacing the value of those constants at every time and thus asking for result at that specific time which is then stored in a variable.

I'm not sure if these solvers are suitable for the task or if in fact, I should be using a different method like "Finite Difference Method", although, the latter would take more time and is prone to time issues.

The results are so far obtained out of spec. Any advice would be really appreciate !

1 Answers1

0

Yes, that is a valid strategy

for i in range(N):
    Sol_Ts = solve_ivp(dTsdt,t[[i,i+1]],[Ts0],args=(A[i],B[i],C[i],))
    Ts_arr.append(Sol_Ts.y.copy())
    time_arr.append(Sol_Ts.t.copy)
    Ts0 = Sol_Ts.y[:,-1]

Ts_arr = np.concatenate(Ts_arr, axis=1)
time_arr = np.concatenate(time_arr)

You could also integrate over the full interval using the sympy.interpolate.interp1d function generator for interpolation functions of various types, here you would use the "zero-order hold", the piece-wise constant extension of the given function table.

Lutz Lehmann
  • 25,219
  • 2
  • 22
  • 51
  • Alright, then the approach is correct for both solvers (ODEint and Solve_ivp), the point is that I'm not getting the right output as expected. I am not so sure if the first time the solver works into the loop, it has to start from t=1, as t=0 is where the initial condition is replaced in. Also, one of those variables (B) is calculated by a class which is called up inside the loop to generate the result, This result will be used to feed the solver on the next loop. B[0] = given, B = takes Sol_Ts and generates B[1], then it's used for the second loop... – Mauricio Mejia Nov 28 '22 at 15:57
  • For that you would have to be more specific. You can use a simplified example or a mock-up that has nothing to do with your system. – Lutz Lehmann Nov 28 '22 at 19:30
  • I tried to validate the output using the Foward finite Differential method : dTs/dt = Alfa* (A+ TsB - C) ; where Alfa, A,B and C depend on vectors that vary over time. Since I need to estimate the Ts value and store it in a variable to later plot it out, this is how I approach it : Ts(t+dt) - Ts(t) = dt Alfa(A + TsB - C) Ts(t+dt) = dt Alfa(A + Ts*B - C) + Ts(t) The output is not what I expected. I reduced the timestep but it is still showing the same behavior. Any recomendation ? Thanks – Mauricio Mejia Nov 30 '22 at 20:25