'taking derivative using sparse matrix

I’m trying to solve the following problem for around 2 days, but have not had success. I want to calculate the derivative using the sparse matrix, but the result doesn’t true. I think there is a mistake in the solution function, but I cannot find it.

class Cahn_Hillard(object):

    def __init__(self, n, X, T, dt, skip):
        self.n = n
        self.X = X
        self.dx = 1 / (n - 1)
        self.T = T
        self.dt = dt
        self.N = int(self.T / self.dt)

    def __call__(self):

        central = self.forward_diff_sparse()
        itr = int(self.T / self.dt)
        for i in range(0, itr + 1):
            if i == 0:
                c = np.random.uniform(0, 1, (self.n, 1))
            else:
                c_next = self.solution(c, central)
                c = c_next
                print(i)

        return c

    def forward_diff_sparse(self):

        sparse_mat = np.eye(self.n) - np.eye(self.n, k=-1)
        return sparse_mat

    def solution(self, c, central):
        # calculate derivative of concentration
        deriv = central.dot(c) / self.dx

        # calculate difusion coeffcient 
        D_eff = (1 - 2 * self.X * c * (1 - c)) * deriv
        Diff = central.dot(D_eff) / self.dx

        # Calculate the next step concentration
        next_c = c + self.dt * Diff
        return next_c

It would be great if you help me.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source