def test_noisy_runge(): n = 100 np.random.seed(3) x0 = 2 * np.random.rand(n) - 1.0 y0 = 1 / (1 + 25 * x0**2) y0 += 1.0e-1 * (2 * np.random.rand(*x0.shape) - 1) a = -1.5 b = +1.5 plt.plot(x0, y0, "xk") x = np.linspace(a, b, 201) # plt.plot(x, 1 / (1 + 25 * x ** 2), "-", color="0.8", label="1 / (1 + 25 * x**2)") lmbda = 0.2 u = smoothfit.fit1d(x0, y0, a, b, 200, degree=1, lmbda=lmbda, variant="dolfin") x = np.linspace(a, b, 201) vals = [u(xx) for xx in x] plt.plot(x, vals, "-") # plt.title(f"lmbda = {lmbda:.1e}") plt.xlim(a, b) plt.ylim(-0.2, 1.2) plt.grid() plt.gca().set_aspect("equal") # plt.show() plt.savefig("runge-noise-02.svg", bbox_inches="tight", transparent=True)
def lambda_effect(): n = 100 numpy.random.seed(1) x0 = 2 * numpy.random.rand(n) - 1.0 y0 = 1 / (1 + 25 * x0 ** 2) + 1.0e-1 * (2 * numpy.random.rand(*x0.shape) - 1) a = -1.5 b = +1.5 for k, lmbda in enumerate(numpy.logspace(-3, 1, num=41)): plt.plot(x0, y0, "xk") x = numpy.linspace(a, b, 201) # plt.plot(x, 1 / (1 + 25 * x ** 2), "-", color="0.8", label="1 / (1 + 25 * x**2)") u = smoothfit.fit1d(x0, y0, a, b, 1000, degree=1, lmbda=lmbda) x = numpy.linspace(a, b, 201) vals = [u(xx) for xx in x] plt.plot(x, vals, "-", color="#d62728") plt.title(f"lmbda = {lmbda:.1e}") plt.xlim(a, b) plt.ylim(-0.2, 1.2) # plt.show() plt.savefig( f"smoothfit-lambda-{k:02d}.png", bbox_inches="tight", transparent=True ) plt.close() return
def test_noisy_runge(): n = 100 rng = np.random.default_rng(123) x0 = 2 * rng.random(n) - 1.0 y0 = 1 / (1 + 25 * x0**2) y0 += 1.0e-1 * (2 * np.random.rand(*x0.shape) - 1) a = -1.5 b = +1.5 plt.plot(x0, y0, "xk") # x = np.linspace(a, b, 201) # plt.plot(x, 1 / (1 + 25 * x ** 2), "-", color="0.8", label="1 / (1 + 25 * x**2)") lmbda = 0.2 basis, u = smoothfit.fit1d(x0, y0, a, b, 200, degree=1, lmbda=lmbda) plt.plot(basis.mesh.p[0], u[basis.nodal_dofs[0]], "-", label="smooth fit") # plt.title(f"lmbda = {lmbda:.1e}") plt.xlim(a, b) plt.ylim(-0.2, 1.2) plt.grid() plt.gca().set_aspect("equal") # plt.show() # plt.savefig("runge-noise-02.svg", bbox_inches="tight", transparent=True) plt.savefig("runge-noise-02.png", bbox_inches="tight", transparent=True)
def test_1d(): n = 20 # x0 = numpy.linspace(-1.0, 1.0, n) numpy.random.seed(123) x0 = numpy.random.rand(n) * 2 - 1 # y0 = x0.copy() # y0 = 1 / (1 + 25*x0**2) + 5.0e-2 * (2*numpy.random.rand(n)-1) # y0 = numpy.exp(x0) + 1.0e-1 * (2*numpy.random.rand(n) - 1) # y0 = x0 + 1.0e-1 * (2*numpy.random.rand(n) - 1) # y0 = x0**3 + 1.0e-1 * (2*numpy.random.rand(n) - 1) y0 = numpy.sin(1 * numpy.pi * x0) # + 1.0e-1 * (2*numpy.random.rand(n) - 1) # y0 = 1 / (x0 + 2) + 1.0e-2 * (2*numpy.random.rand(n) - 1) a = -1.5 b = +1.5 u = smoothfit.fit1d(x0, y0, a, b, 10, degree=2, eps=1.0e-1) # ref = 1.5552074468182238 # print(assemble(u*u * dx)) # assert abs(assemble(u*u * dx) - ref) < 1.0e-2 * ref # x = u.function_space().mesh().coordinates() x = numpy.linspace(a, b, 201) vals = [u(xx) for xx in x] plt.plot(x0, y0, 'xk', label='data') plt.plot(x, vals, '-', color='k', alpha=0.3, label='smooth fit') plt.xlim(a, b) plt.legend() plt.show() return
def update(lmbda): basis, coeffs = smoothfit.fit1d(x0, y0, a, b, 500, degree=1, lmbda=lmbda) fit.set_xdata(basis.mesh.p[0]) fit.set_ydata(coeffs[basis.nodal_dofs[0]]) plt.title(f"lambda = {lmbda:.3e}") plt.ylim(-0.1, 1.1) plt.xlim(a, b) plt.legend()
def test_samples(): # From <https://en.wikipedia.org/wiki/Gauss%E2%80%93Newton_algorithm#Example> x0 = np.array([0.038, 0.194, 0.425, 0.626, 1.253, 2.500, 3.740]) y0 = np.array([0.050, 0.127, 0.094, 0.2122, 0.2729, 0.2665, 0.3317]) basis, u = smoothfit.fit1d(x0, y0, 0, 4, 1000, degree=1, lmbda=1.0) # plot the function plt.plot(basis.mesh.p[0], u[basis.nodal_dofs[0]], "-", label="smooth fit") plt.plot(x0, y0, "xk") plt.xlim(0, 4) plt.ylim(0) plt.grid() # plt.show() plt.savefig("smoothfit-samples.png", bbox_inches="tight", transparent=True)
def test_1d(solver, show=False): n = 20 # x0 = np.linspace(-1.0, 1.0, n) rng = np.random.default_rng(2) x0 = rng.random(n) * 2 - 1 # y0 = x0.copy() # y0 = 1 / (1 + 25*x0**2) + 5.0e-2 * (2*np.random.rand(n)-1) # y0 = np.exp(x0) + 1.0e-1 * (2*np.random.rand(n) - 1) # y0 = x0 + 1.0e-1 * (2*np.random.rand(n) - 1) # y0 = x0**3 + 1.0e-1 * (2*np.random.rand(n) - 1) y0 = np.sin(1 * np.pi * x0) # + 1.0e-1 * (2*np.random.rand(n) - 1) # y0 = 1 / (x0 + 2) + 1.0e-2 * (2*np.random.rand(n) - 1) a = -1.5 b = +1.5 lmbda = 1.0e-2 basis, coeffs = smoothfit.fit1d(x0, y0, a, b, 64, lmbda, degree=1, solver=solver) if solver == "Nelder-Mead": ref = 14.27840047789019 else: ref = 36.311654900989524 assert abs(np.dot(coeffs, coeffs) - ref) < 1.0e-10 * ref if show: plt.plot(basis.mesh.p[0], coeffs[basis.nodal_dofs[0]], "-", label="smooth fit") plt.plot(x0, y0, "xk", label="samples") x = np.linspace(a, b, 101) plt.plot(x, np.sin(np.pi * x), "-", color="0.8", label="original") plt.xlim(a, b) plt.legend() plt.show() return
def update(k): # k sample points xs = np.linspace(-1.0, 1.0, k) ys = f(xs) samples.set_xdata(xs) samples.set_ydata(ys) # plt.plot(xs, ys, "xk") basis, coeffs = smoothfit.fit1d(xs, ys, a, b, 200, degree=1, lmbda=1.0e-6) fit.set_xdata(basis.mesh.p[0]) fit.set_ydata(coeffs[basis.nodal_dofs[0]]) plt.title(f"{k} sample points") plt.ylim(-0.1, 1.1) plt.xlim(a, b) plt.legend()
def test_1d(solver, show=False): n = 20 # x0 = np.linspace(-1.0, 1.0, n) np.random.seed(2) x0 = np.random.rand(n) * 2 - 1 # y0 = x0.copy() # y0 = 1 / (1 + 25*x0**2) + 5.0e-2 * (2*np.random.rand(n)-1) # y0 = np.exp(x0) + 1.0e-1 * (2*np.random.rand(n) - 1) # y0 = x0 + 1.0e-1 * (2*np.random.rand(n) - 1) # y0 = x0**3 + 1.0e-1 * (2*np.random.rand(n) - 1) y0 = np.sin(1 * np.pi * x0) # + 1.0e-1 * (2*np.random.rand(n) - 1) # y0 = 1 / (x0 + 2) + 1.0e-2 * (2*np.random.rand(n) - 1) a = -1.5 b = +1.5 lmbda = 1.0e-2 u = smoothfit.fit1d(x0, y0, a, b, 64, lmbda, degree=1, solver=solver, variant="dolfin") if solver == "Nelder-Mead": ref = 0.659_143_389_243_738_2 else: ref = 1.417_961_801_095_804_4 val = assemble(u * u * dx) assert abs(val - ref) < 1.0e-10 * ref if show: # x = u.function_space().mesh().coordinates() x = np.linspace(a, b, 201) vals = [u(xx) for xx in x] plt.plot(x, vals, "-", label="smooth fit") plt.plot(x0, y0, "xk", label="samples") plt.plot(x, np.sin(np.pi * x), "-", color="0.8", label="original") plt.xlim(a, b) plt.legend() plt.show()
def test_samples(): # From <https://en.wikipedia.org/wiki/Gauss%E2%80%93Newton_algorithm#Example> x0 = numpy.array([0.038, 0.194, 0.425, 0.626, 1.253, 2.500, 3.740]) y0 = numpy.array([0.050, 0.127, 0.094, 0.2122, 0.2729, 0.2665, 0.3317]) u = smoothfit.fit1d(x0, y0, 0, 4, 1000, degree=1, lmbda=1.0) # plot the function x = numpy.linspace(0, 4, 201) vals = [u(xx) for xx in x] plt.plot(x, vals, "-", label="smooth fit") plt.plot(x0, y0, "xk") plt.xlim(0, 4) plt.ylim(0) plt.grid() # plt.show() plt.savefig("smoothfit-samples.svg", bbox_inches="tight", transparent=True) return