Python Forum
Finite difference scheme in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Finite difference scheme in python
#1
I am trying to develop central finite difference scheme (lexicographical) coefficients. My code is,

    import numpy as np
    from scipy.sparse import spdiags, identity
    x = np.linspace(0, 9, num=10)
    nx = len(x)
`   hx = (x[-1] - x[0])/(nx - 1)
    one_x = np.ones((nx, 1))
    x_coeffs = np.hstack((one_x, one_x))
    x_coeffs = np.insert(x_coeffs, 1, -2, axis=1)
    # So far so good, then carry on
    x_diff = spdiags(x_coeffs.transpose(), np.asarray([0, 1, 2]), nx-2, nx)
    nx_id = identity(nx)
    Dx = np.kron(nx_id, x_diff.transpose())
    Dx = 1/hx*Dx
Let us try it on a dummy example.

x = np.linspace(0, 4, num=5)
f = np.power(x, 2)
dfdx = Dx * f
But the result for this dummy example is (array([ -4., -6., -8., -10.]). Any idea about what could go wrong?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ValueError: The condensed distance matrix must contain only finite values. kisumsam 1 4,600 Dec-29-2019, 10:14 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020