2

My question is if there's a way to take some values in a function that are not integrated in odeint.

Exemple: if I have a derivative dy(x)/dt = A*x+ln(x) and before to get this equation I computed A throught of a intermediate equation like A = B*D . I would like to take the A's value during the process.

More detailed (only exemple):

def func(y,t)
    K = y[0]
    B = 3
    A = cos(t**2) + B
    dy/dt = A*t+ln(t)
    return [dy/dt]

Can I take A's values of function?

The answer for Josh Karpel

The code is like that:

def Reaction(state,t):

  # Integrate Results

  p = state[0]

  T = state[1]

  # function determine enthalpy of system

  f1(T,p) = enthalpy

  # function determine specific volume of system

  f2(T,p) = specific volume

  # function determine heat release by reactions

  f3(T,p,t) = heat release by reactions

# Derivatives

 dp/dt = f(T,p,enthalpy,specific volume,heat release by reactions)

 dT/dt = f(T,p,enthalpy,specific volume,heat release by reactions)

The real code is bigger than that. But, I would like to know if there is a way to store the values of f1 (enthalpy), f2 (specific volume), f3 (heat release) as a vector or tuple during the process of solution of odeint with the same size of p and T.

Marr
  • 555
  • 1
  • 6
  • 11
  • 1
    If I understand your question correctly, there are some solutions in these two questions https://stackoverflow.com/questions/16904202/numpy-odeint-output-extra-variables https://stackoverflow.com/questions/46812671/extract-values-from-function-used-by-odeint-scipy-python – j.c. May 31 '18 at 04:17
  • Thanks for pointer. More specifically, I think this answer provides an easy (if not necessarily neatest) solution of getting non-state variables out of `odeint`: https://stackoverflow.com/a/46815621/4988601 – kabdulla May 14 '21 at 11:19

1 Answers1

0

It's not entirely clear what you want, but it sounds like you need to pass another value to the function you're integrating over. There are two options I can think of:

  1. scipy.integrate.odeint takes an args argument which contains extra arguments to be passed to the integrand function, which could then have signature y(t, A).
  2. You could use functools.partial to construct a new function which has the argument A for the integrand function y(t, A) already set.
Josh Karpel
  • 2,110
  • 2
  • 10
  • 21
  • You is talking about input arguments. I was talking regarding if there's a way get off integral function with any vectors or values besides integrated values. – Mycroft Holmes Aug 21 '17 at 13:09
  • To clarify: you want the result of the integral to be a function Y(A)? [SymPy](http://www.sympy.org/) can perform symbolic integrals/derivatives. Alternatively, it seems like you know the values of A - is there any reason you can't simply compute the integral for each value of A you're interested in? – Josh Karpel Aug 21 '17 at 15:29
  • Thanks by attention Josh. I will clarify more the problem. I am building a code to calculate thermodynamic behouivor of a reactor. I am working with two differential equations: one to compute pressure and another temperature. However, there some thermodynamic properties that not are solved but I would like return the value to plot in a graph. – Mycroft Holmes Aug 23 '17 at 19:06
  • I saw that exist the function 'append' that I can use and I have already tested. But as we know odeint do adaptatives steps during solution yields more steps than the derivatives's result. I would like to Know if there's a way to use 'append' in odeint and this function return a vector with the same size of solution of derivatives. – Mycroft Holmes Aug 23 '17 at 19:18
  • Sorry, it's still not clear to me what you want the desired output to be. Could you add an example of the desired inputs and outputs to the question? – Josh Karpel Aug 23 '17 at 21:22
  • I posted your question as answer above. – Mycroft Holmes Aug 24 '17 at 10:31
  • I deleted the second question. – Mycroft Holmes Aug 25 '17 at 14:46
  • If you're using odeint to solve for T and p as functions of t, why can't you just plug T, p, and t back into f1, f2, and f3 to get their values at those points? – Josh Karpel Aug 25 '17 at 17:18
  • Because those points not are derivatives. I work with a open source called cantera (a library of thermodynamic properties ) that return the values of f1, f2 and f3. T and p in this case, are arguments that enter inside of Cantera's functions – Mycroft Holmes Aug 26 '17 at 20:59