LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 3 years have passed since last update.

プログラミング問題集解答例(問26)

Last updated at Posted at 2019-12-18
import numpy as np
f = lambda x: 1/(1 + np.exp(-x))
g = lambda x: 1.0/(1.0+x**2)
h = lambda x: np.sin(x)

x_observed = np.linspace(-10, 10, 11) # 観測点
def lagurange(x_observed, y_observed):
    def accept_variables(x):
        interpolation = 0
        px = [1] * len(x_observed)
        qx = [1] * len(x_observed)
        for i in range (len(x_observed)):
            for j in range (len(x_observed)):
                if i != j:
                    px[i] = px[i] * (x - x_observed[j])
                    qx[i] = qx[i] * (x_observed[i] - x_observed[j])
            interpolation = interpolation + px[i] * y_observed[i] / qx[i]
        return interpolation
    return accept_variables
fitted_curve = lagurange(x_observed, f(x_observed))
fitted_curve
<function __main__.lagurange.<locals>.accept_variables>
x_latent = np.linspace(-10, 10, 101)
fitted_curve(x_latent)
array([ 0.54402111,  1.87516858,  2.42669307,  2.44151156,  2.11182678,
        1.58666499,  0.97861224,  0.36980792, -0.18274842, -0.64252334,
       -0.98935825, -1.21591199, -1.32459162, -1.32492766, -1.23135279,
       -1.06134513, -0.83389931, -0.56829098, -0.28310231,  0.00452185,
        0.2794155 ,  0.52875707,  0.74228372,  0.91237695,  1.03404053,
        1.10479017,  1.12447229,  1.09502822,  1.02021822,  0.90531846,
        0.7568025 ,  0.58201782,  0.38886626,  0.18549619, -0.01998685,
       -0.2197856 , -0.40664816, -0.57407311, -0.71648025, -0.82934527,
       -0.90929743, -0.95418025, -0.96307581, -0.93629387, -0.8753277 ,
       -0.78277906, -0.66225497, -0.51823979, -0.35594594, -0.18114742,
        0.        ,  0.18114742,  0.35594594,  0.51823979,  0.66225497,
        0.78277906,  0.8753277 ,  0.93629387,  0.96307581,  0.95418025,
        0.90929743,  0.82934527,  0.71648025,  0.57407311,  0.40664816,
        0.2197856 ,  0.01998685, -0.18549619, -0.38886626, -0.58201782,
       -0.7568025 , -0.90531846, -1.02021822, -1.09502822, -1.12447229,
       -1.10479017, -1.03404053, -0.91237695, -0.74228372, -0.52875707,
       -0.2794155 , -0.00452185,  0.28310231,  0.56829098,  0.83389931,
        1.06134513,  1.23135279,  1.32492766,  1.32459162,  1.21591199,
        0.98935825,  0.64252334,  0.18274842, -0.36980792, -0.97861224,
       -1.58666499, -2.11182678, -2.44151156, -2.42669307, -1.87516858,
       -0.54402111])
%matplotlib inline
import matplotlib.pyplot as plt

fitted_curve = lagurange(x_observed, f(x_observed))
plt.scatter(x_observed, f(x_observed), label="observed")
plt.plot(x_latent, f(x_latent), label="latent")
plt.plot(x_latent, fitted_curve(x_latent), label="fitted")
plt.grid()
plt.legend()
plt.show()

output_4_0.png

%matplotlib inline
import matplotlib.pyplot as plt

for func in [f, g, h]:
    fitted_curve = lagurange(x_observed, func(x_observed))
    plt.scatter(x_observed, func(x_observed), label="observed")
    plt.plot(x_latent, func(x_latent), label="latent")
    plt.plot(x_latent, fitted_curve(x_latent), label="fitted")
    plt.grid()
    plt.legend()
    plt.show()

output_5_0.png

output_5_1.png

output_5_2.png

0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up