0
$\begingroup$

First time posting here, so I apologize for any missing info upfront. I am working on a program in VBA that calculates a function (which itself calls another function), then calculates the derivative of that function, then uses that to form a Jacobian matrix to calculate the solution via the Newton-Raphson method. My question relates to taking the derivative of a function, $f$.

First, the function has the general form: $f(i, h, q_0, A, x, v(x, X_{min}))$. $i$ is a counter variable, and $h, q_0, A$ and $X_{min}$ are constant single value inputs. The remaining arguments are all members of the same array $x$ or another function $v(x, X_{min})$. The form of the function changes based on the value of $i$. The inputs from the array may be, depending on the value of $i$:

  1. $x_i$ and $x_{i+1}$
  2. $x_i$, $x_{i-1}$, $x_{i+1}$
  3. $x_{i}$ and $x_{i-1}$

The inputs from $v(x)$ may be, again depending on i:

  1. $v(x_i)$
  2. $v(x_{i-1})$ and $v(x_i)$
  3. $v(x_{i-1})$

Here is an example of the function in question: $$f = \frac{1}{h}\bigg[\frac{q_0}{A} * (x_{i + 1} - x_i) + v(x_{i - 1}, X_{min}) * x_{i - 1} - v(x_i, X_{min}) * x_i)\bigg]$$

I am using this form of the finite difference method: $$f′(x) = \frac{−f(x+ 2∆x) + 8f(x+ ∆x) −8f(x−∆x) + f(x−2∆x)}{12\Delta x}$$

My question is, for each time I use this function, do I need to apply the $∆x$ to each instance of the array element passed to the function? Ie, for the case where $x_i$ and $x_{i+1}$ are called, do I need to add or subtract $∆x$ to both $x_i$ and $x_{i+1}$? This would give $x_i$ $+/-$ $1$ or $2∆x$, as well as $x_{i+1}$ $+/-$ $1$ or $2∆x$. Or, according to this method, is it better to calculate the finite difference around $x_i$ only?

And further, for the case where both $v(x_{i-1})$ and $v(x_i)$ are passed to the function $f$, would adding and subtracting $∆x$ to each instance of the $x$ array passed into the $v$ function be required? While I'm at it (and I hope this is overkill), would I also need to calculate the finite difference centered around the values returned by $v(x)$ as well?

Alternatively, is there simply a better way to determine the derivative of this function than the way I'm going about it? I hope I have provided enough info. I didn't include the code, as I think that is a separate question, to be explored once I have an answer to this question.

Thank you!!

$\endgroup$
2
  • $\begingroup$ I edited part of your question to use mathjax formatting for the equations. Let me know if I messed anything up and see if you can extend this to the rest of your question $\endgroup$ – Tyberius Jul 20 at 18:42
  • $\begingroup$ Thank you, I will return to this later today and try to format the rest in mathjax as you suggested. $\endgroup$ – Alex Wohlgemuth Jul 20 at 19:01
0
$\begingroup$

With $f(x, y(x))$ while one could technically split up $\frac{\partial f}{\partial y}$ and $\frac{\partial y}{\partial x}$, it isn't necessary. You can just directly perturb both $x$'s at the same time to get the total derivative on $f$ w.r.t. $x$. But, in your equations, $f$ does not actually take $v$ as an input. It might call $v$ with different inputs (the same way it calls $-$, $v$ isn't special). So, it really is just $f(x)$ here.

When it comes to perturbing vector inputs like $x$, you will need to do it one component at the time, meaning you will have to call $f$ many times here. Each perturb $x_i$ gives you one column of your Jacobian. If you happen to know that a certain $x_j$ isn't going to affect $f_i(x)$ at all, you can of course skip all those computations and just fill in the 0 in your jacobian.

There are many issues with numerical derivatives, accuracy, computational time, etc. I would consider a different method if possible. Analytical derivation, automatic differentiation, or switch to a completely different method, like Nelder-Mead or something.

$\endgroup$
1
  • $\begingroup$ Ok thank you for your suggestion! I will look into automatic differentiation and see what I can learn about it and how to apply it here. Thanks! $\endgroup$ – Alex Wohlgemuth Jul 22 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.