Asked 1 month ago by CometScientist546
Why does my Python code produce incorrect finite difference weights from a Taylor series expansion?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 month ago by CometScientist546
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I'm trying to compute the weights for a 5-point finite difference approximation of the first derivative f′(x) using Taylor series. I aim to find coefficients a, b, c, d, e such that:
af(x+2Δx) + bf(x+Δx) + cf(x) + df(x−Δx) + ef(x−2Δx)
approximates f′(x).
I proceeded with the following steps:
PYTHONimport numpy as np A = np.array([ [1, 1, 1, 1, 1], # Coefficients of f(x) [2, 1, 0, -1, -2], # Coefficients of f'(x) [4, 1, 0, 1, 4], # Coefficients of f''(x) [8, 1, 0, -1, 8], # Coefficients of f'''(x) [16, 1, 0, 1, 16] # Coefficients of f''''(x) ]) b = np.array([0, 1, 0, 0, 0]) # Targeting f'(x) z = np.linalg.solve(A, b) print(z)
The output is:
[0.25, 0, -0, 0, -0.25]
However, the expected weights for the first derivative are approximately:
[-1/12, 2/3, 0, -2/3, 1/12]
What I Tried:
Despite these checks, the computed weights remain incorrect. Am I missing something in the setup of the matrix or in my Python implementation?
Comments 0
•Answers 3
•Views 80
Answer by TychoAI Agent • 1 month ago
Below is the key insight: one of the rows in your system is set up with an incorrect sign. In your derivation, the Taylor expansion for f(x – 2Δx) contributes a term to f‴(x) with a negative sign, but in your matrix you used +8 for that coefficient.
Expanding about x:
• f(x + 2h) = f(x) + 2h·f′(x) + (4h²/2)·f″(x) + (8h³/6)·f‴(x) + (16h⁴/24)·f⁽⁴⁾(x) + …
• f(x + h) = f(x) + h·f′(x) + (h²/2)·f″(x) + (h³/6)·f‴(x) + (h⁴/24)·f⁽⁴⁾(x) + …
• f(x – h) = f(x) – h·f′(x) + (h²/2)·f″(x) – (h³/6)·f‴(x) + (h⁴/24)·f⁽⁴⁾(x) + …
• f(x – 2h) = f(x) – 2h·f′(x) + (4h²/2)·f″(x) – (8h³/6)·f‴(x) + (16h⁴/24)·f⁽⁴⁾(x) + …
After clearing the denominators by multiplying the appropriate rows, you want your system to satisfy:
In your Python matrix, the fourth row is given as
[8, 1, 0, -1, 8]
But based on the derivation it should be
[8, 1, 0, -1, -8]
That sign on the coefficient multiplying e must be negative, otherwise the system is not representing the Taylor expansion correctly and you end up with the wrong weights.
Below is the corrected Python snippet:
PYTHONimport numpy as np A = np.array([ [1, 1, 1, 1, 1], # f(x) coefficients [2, 1, 0, -1, -2], # f'(x) coefficients [4, 1, 0, 1, 4], # f''(x) coefficients (multiplied by 2) [8, 1, 0, -1, -8], # f'''(x) coefficients (multiplied by 6) [16, 1, 0, 1, 16] # f⁽⁴⁾(x) coefficients (multiplied by 24) ]) b = np.array([0, 1, 0, 0, 0]) # Targeting the first derivative z = np.linalg.solve(A, b) print(z)
When you run this corrected code, you should obtain the weights:
a = -1/12, b = 2/3, c = 0, d = -2/3, e = 1/12
These match the theoretical five-point central difference scheme for the first derivative:
f'(x) ≈ (-f(x+2h) + 8f(x+h) - 8f(x-h) + f(x-2h))⁄(12h)
Your original matrix had the wrong sign in the row corresponding to the third derivative (f‴(x)). Correcting the sign to –8 in that row will yield the expected finite-difference weights.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
Answer by AuroraCollector374 • 1 month ago
Write the Taylor series out carefully:
Hence,
where
Inverting:
The weights on the points (x-2Δx,x-Δx,x,x+Δx,x+2Δx) (note that these are the opposite way round to yours) are the relevant rows in the weight matrix
For example (as in the code below) you get (Δx)f' from row number 1 (counting from 0) of the matrix W:
PLAINTEXT[ 8.33333333e-02 -6.66666667e-01 2.96059473e-16 6.66666667e-01 -8.33333333e-02]
which is, essentially, [1/12,-2/3,0,2/3,-1/12] (reminder again: my points are the opposite way round to yours, so reversing the list).
PYTHONimport numpy as np A = np.array( [ # coefficients in f = A (deltax fderiv) [ 1, -2, 2, -4/3, 2/3 ], [ 1, -1, 1/2, -1/6, 1/24 ], [ 1, 0, 0, 0, 0 ], [ 1, 1, 1/2, 1/6, 1/24 ], [ 1, 2, 2, 4/3, 2/3 ] ] ) W = np.linalg.inv( A ) derivative = 1 # this is (deltax)f' print( W[derivative,:] )
No comments yet.
Answer by EtherealRover508 • 1 month ago
You have a typo in the 4th line of the matrix A, the last element should be -8 instead of 8:
PYTHONA = np.array([ [1, 1, 1, 1, 1], # Coefficients of f(x) [2, 1, 0, -1, -2], # Coefficients of f'(x) [4, 1, 0, 1, 4], # Coefficients of f''(x) [8, 1, 0, -1, -8], # Coefficients of f'''(x) [16, 1, 0, 1, 16] # Coefficients of f''''(x) ])
No comments yet.
No comments yet.