Post Reply 
Approximating function derivatives
01-30-2024, 11:06 PM
Post: #24
RE: Approximating function derivatives
We can get all derivative formulas in XCas. Here is same code for HP Prime Cas
Note that code does not care about symmetry. Use whatever points you had available.

Cas> fitted(xs) := simplify(symb2poly(lagrange(xs, apply(f,xs))))
Cas> make_diff(xs) := reverse(fitted(xs)) .* map(range(len(xs)), n->n!/h^n)

Cas> make_diff([-1,0,1])
Cas> simplify(Ans)

\(\displaystyle [ f(0), \frac{f(1) - f(-1)}{2h}, \frac{f(1)-2f(0)+f(-1)}{h^2}] \)

With many points, resulting derivatives may be messy.

For central differences, we may use symmetry to make formula more compact.
We know sum of f's gives even derivatives, differences gives odd.

Let g^n = (f(n) ± f(-n)), except for g^0 = 1 = f(0)
Sign depends on parity of derivatives. (odd = -, even = +)

Cas> make_diff([-2,-1,1,2]) | f = (x-> when(x<0,0,g^x))
Cas> simplify(Ans)

\( \displaystyle [
\frac{-g^2+4g}{6},
\frac{-g^2+8g}{12h},
\frac{g^2-g}{3 h^2},
\frac{g^2-2g}{2 h^3}
] \)

Undo g's, we get back f's
(01-26-2024 08:33 AM)Thomas Klemm Wrote:  \(
\begin{bmatrix}
f(x_0) \\
{f}''(x_0) h^2 \\
\end{bmatrix}
\approx
\begin{bmatrix}
\frac{2}{3} & - \frac{1}{6} \\
- \frac{1}{3} & \frac{1}{3} \\
\end{bmatrix}
\cdot
\begin{bmatrix}
f_{+1} + f_{-1} \\
f_{+2} + f_{-2} \\
\end{bmatrix}
\)

\(
\begin{bmatrix}
{f}'(x_0) h \\
{f}'''(x_0) h^3 \\
\end{bmatrix}
\approx
\begin{bmatrix}
\frac{2}{3} & - \frac{1}{12} \\
- 1 & \frac{1}{2} \\
\end{bmatrix}
\cdot
\begin{bmatrix}
f_{+1} - f_{-1} \\
f_{+2} - f_{-2} \\
\end{bmatrix}
\)
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Approximating function derivatives - Pekis - 01-24-2024, 02:31 PM
RE: Approximating function derivatives - Albert Chan - 01-30-2024 11:06 PM



User(s) browsing this thread: 1 Guest(s)