1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import os import numpy as np import matplotlib.pyplot as plt os.chdir(r'D:\projects\wordpress\ex41') os.getcwd() N = 5 u0 = 1 u1 = 2 dx = 1.0 / (N + 1) A = (np.eye(N, k=-1) - 2 * np.eye(N) + np.eye(N, k=1)) / dx**2 print(A) d = -5 * np.ones(N) d[0] -= u0 / dx**2 d[N-1] -= u1 / dx**2 u = np.linalg.solve(A, d) x = np.linspace(0, 1, N+2) U = np.hstack([[u0], u, [u1]]) fig, ax = plt.subplots(figsize=(8, 4)) ax.plot(x, U) ax.plot(x[1:-1], u, 'ks') ax.set_xlim(0, 1) ax.set_xlabel(r"$x$", fontsize=18) ax.set_ylabel(r"$u(x)$", fontsize=18) fig.savefig("ch11-fdm-1d.pdf") fig.tight_layout() plt.savefig("example41.png", dpi=100) plt.show() plt.close() <img class=" wp-image-201 aligncenter" src="https://gantovnik.com/bio-tips/wp-content/uploads/2019/01/example41.png" alt="example41" width="644" height="322" /> |
Recent Comments