0
$\begingroup$

I have an ordinary differential equation:

$\quad$ $\frac{dP}{dx}$ = $f(x,P,Q)$

which I need to integrate from $x$ = $0$ to $X$, where $P$=$P_0$ at $x$=$0$.

For the moment let's assume I am using simple Euler to solve it:

$\quad$ $P_{i+1}$ = $P_i$ + $h*f(x_i,P_i,Q)$

From the solution to this I get the values of $P_i$ at each $x_i$, and in particular $P_n$ at $x$=$X$. All this works fine where $Q$ is a constant.

I now need to deal with the case where $Q$ is variable and in particular I need to find $\frac{\partial P_n}{\partial Q}$. Obviously I can do it numerically by perturbing $Q$ by a small amount $q$, rerunning the Euler solution to get $P_n'$ to give $\frac{\partial P_n}{\partial Q}$ $\approx$ $\frac{P_n'-P_n}{q}$, however this will take two Euler passes.

So I wish to do it analytically using the first Euler solution. $f$ is an analytic function and so I have all the required derivatives $\frac{\partial f}{\partial x}$, $\frac{\partial f}{\partial P}$, $\frac{\partial f}{\partial Q}$

I also need $\frac{\partial P_n}{\partial P_0}$ although I guess the solution methodology is the same as for $\frac{\partial P_n}{\partial Q}$

Any help doing this would be appreciated.

PS. I am actually using Runge Kutta for this, but am choosing Euler in this question for simplification as the solution technique is all that is important here.

  • 0
    I doubt the result of this will be nice. The problem is that the perturbation in $Q$ causes perturbations in the prior values of $P$ which then have to be fed into $\frac{\partial f}{\partial P}$. So consider for example (with $P_0$ fixed) $P_2'=P_1'+hf(x_1,P_1',Q')=P_0+hf(x_0,P_0,Q')+hf(x_0,P_0+hf(x_0,P_0,Q'),Q')$. That third term is very annoying to handle, and it only gets worse as you go to higher $n$.2017-01-29
  • 0
    Not sure I agree. Let's say there is one Euler step. $\quad$ $P_1$=$P_0+h*f(x_0,P_0,Q)$ $\quad$ so $\frac{\partial P_1}{\partial Q}=h*\frac{\partial f}{\partial Q}(x_0,P_0,Q)$ if 2 steps: $\quad$ $P_2$=$P_1+h*f(x_1,P_1,Q)$ So $\quad$ $\frac{\partial P_2}{\partial Q}$=$\frac{\partial P_1}{\partial Q}+h*\frac{\partial f}{\partial Q}(x_1,P_1,Q)$ $\quad$ $=h*\left([\frac{\partial f}{\partial Q}(x_0,P_0,Q)+\frac{\partial f}{\partial Q}(x_1,P_1,Q)\right)$ So it seems to me: $\quad$ $\frac{\partial P_n}{\partial Q}=h*\sum_{i=0}^{n-1}\frac{\partial f}{\partial Q}(x_i,P_i,Q)$2017-01-29
  • 0
    sorry I formatted this on separate lines as in my question but it chose to concatenate it all onto one line, hope it is clear2017-01-29
  • 0
    Your $P_1$ in the perturbed case is not the same as in the unperturbed case. So there is a df/dp term for that.2017-01-29
  • 0
    I am not perturbing anything. I would only do that for numerical derivatives. I need analytical derivatives. Hence I am using regular differentiation rules. No perturbation.2017-01-29
  • 0
    The two notions are equivalent...2017-01-29
  • 0
    Absolutely not. Numerical derivatives of a function are calculated by perturbing the independent variable, and seeing the resulting change in the dependent variable. This requires 2 evaluations of the function. This is not what I want to do, mainly because I have the analytical derivatives of the underlying functions, but also because of accuracy. Read digital. On the contrary, an analytic derivative requires only one evaluation of the function, and some clever maths (thanks Isaac) at the same time to establish an accurate derivative. Read analog.2017-01-29
  • 0
    Perturbative expansion and differentiation are equivalent concepts for smooth functions. That said, LutzL has pointed out that this is actually not that hard to handle, provided you seek to approximate the derivative of the true solution with respect to the parameter. It is more difficult to approximate the derivative of the numerical solution with respect to the parameter, which is what I was trying to say.2017-01-29
  • 0
    To clarify what I mean, $\frac{\partial P_2}{\partial Q}$ is all about comparing $P_0+hf(x_0,P_0,Q')+hf(x_1,P_0+hf(x_0,P_0,Q'),Q')$ with $P_0+hf(x_0,P_0,Q)+hf(x_0,P_0+hf(x_0,P_0,Q),Q)$. This cannot be represented solely through $\frac{\partial f}{\partial Q}$ because in the last term the value of $P$ is not the same in the two evaluations of $f$. Thus $\frac{\partial f}{\partial P}$ must be involved, which makes matters messy, and this messiness just gets uglier as you try to differentiate higher terms (or for that matter, use a more complicated numerical method).2017-01-30
  • 0
    But approximating the derivative of the true solution is not so bad (except that it requires solving a 2D ODE system, which is really about the same difficulty as solving 2 1D ODE systems for the same amount of time).2017-01-30

1 Answers 1

2

Using $$ \frac{d}{dx}\frac{d}{dQ}P(x,Q)=\frac{d}{dQ}f(x,P(x,Q),Q)=\frac{∂f}{∂P}(x,P,Q)·\frac{dP}{dQ}+\frac{∂f}{∂Q}(x,P,Q) $$ you get for $V=\frac{dP}{dQ}(x,Q)$ the augmented ODE system \begin{align} P'&=f(x,P,Q)\\ V'&=f_P·V+f_Q \end{align} which you can solve like any other ODE system. It also works if $P_0$ is a compontent of $Q$.

  • 0
    Thanks, I will pursue this approach for $\frac{\partial P_n}{\partial Q}$ What would the equivalent equations be for $\frac{\partial P_n}{\partial P_0}$ ?2017-01-30
  • 1
    As $f$ does in general not depend directly on $P_0$, you get $W'=f_P·W$ with $W_0=I$ for $W(x)=\frac{∂P(x)}{∂P_0}$.2017-01-30
  • 0
    @dacfer In a sense you're asking the wrong question. This calculation works for any parameter vector $Q$ (replacing $\frac{d}{dQ}$ by $\nabla_Q$, of course). Initial conditions are parameters too. (Also it should be stressed again that this does *not* compute the derivative of the numerical solution $P_n$, so writing $\frac{\partial P_n}{\partial Q}$ for this is not appropriate.)2017-01-30
  • 0
    @Ian. Maybe I am, but I have a requirement to calculate $\frac{\partial P_n}{\partial Q}$ I know I can set $Q=Q_1$, run the n-step ODE solver and get $P_n$ Then I can set $Q=Q_1+\delta$, run it again to get $P_n'$ and estimate the derivative. However I don't want to run the ODE solver twice, particularly as I already have the analytic derivatives for the function at each step. So if I'm asking the wrong question, then feel free to rephrase it, but I know what I need.2017-01-31
  • 0
    @LutzL I have implemented your equations above using V and W, and in 3 test cases the analytic solution for both $\frac{\partial P_n}{\partial Q}$ and $\frac{\partial P_n}{\partial P0}$ give very similar results to the numerical derivatives calculated with multiple passes of the ODE solver. I had to ensure that the $V$ and $W$ ODE's do not contribute to the step length control algorithm which must only use the base ODE ie. for $P$. Thanks for your solution!2017-02-01
  • 0
    @LutzL, thanks for the above methods for calculating $\frac{\partial P_n}{\partial Q}$ and $\frac{\partial P_0}{\partial Q}$. I now need to calculate the other 4 partials: $\frac{\partial P_n}{\partial P_0}$, $\frac{\partial Q}{\partial P_0}$, $\frac{\partial P_0}{\partial P_n}$ and $\frac{\partial Q}{\partial P_n}$. Could you point me to a similar solution for obtaining these? Many thanks. Let me know if I need to start a new post.2017-10-31
  • 0
    $Q$ is a free parameter, there is no dependency on $P_0$ or $P_n$. $\frac{∂P_n}{∂P_0}$ I did in a comment above, it is $W(t_n)$ and the inverse you get with a very carefull application of the chain rule.2017-11-01
  • 0
    Sorry my mistake, I meant to say I need $\frac{\partial P_0}{\partial Q}$, $\frac{\partial Q}{\partial P_0}$, $\frac{\partial P_0}{\partial P_n}$ and $\frac{\partial Q}{\partial P_n}$. I do have $\frac{\partial P_n}{\partial Q}$ and $\frac{\partial P_n}{\partial P_o}$. Regarding $Q$ being a free parameter, it is a variable just like $P_0$ and $P_n$. $P_0$ is the pressure at the start of a pipe, $P_n$ is the pressure at the end, and $Q$ is the flow rate in the pipe. In this system, one can fix any 2 of the latter variables, and the 3rd will be calculated.2017-11-01
  • 0
    So it should be possible to evaluate all 6 of the partial derivatives.2017-11-01
  • 0
    OK I think it's time to start a new post, as this is really a side track of what I am trying to do. I am really trying to find the equation of the tangent plane at the solution of the equation $P_0-P_n = \int_0^L f(x,P,Q)\,dx$ at the solution point $(P_0$, $P_n$, $Q)$2017-11-01
  • 0
    Done: https://math.stackexchange.com/questions/2499798/equation-of-tangent-plane-at-the-solution-of-an-ode2017-11-01