Suppose \(f(t,x) = x_0 e^{x_1 t}\)
with data
t: 0 1 2 3
y: 2.0 0.7 0.3 0.1
Use Gauss Newton to fit the data.
import numpy as np
import scipy as sp
import matplotlib.pyplot as pt
import scipy.linalg as la
def residual(x):
return y - x[0] * np.exp(x[1] * t)
def jacobian(x):
return np.array([
...,
...
]).T
(Edit this cell for solution)
# data
t = np.array([0.0, 1.0, 2.0, 3.0])
y = np.array([2.0, 0.7, 0.3, 0.1])
# initial guess
x = np.array([1, 0])
#x = np.array([0.4, 2])
def plot_iterate(x):
pt.plot(t, y, 'ro', markersize=20, clip_on=False)
T = np.linspace(t.min(), t.max(), 100)
Y = x[0] * np.exp(x[1] * T)
pt.plot(T, Y, 'b-')
plot_iterate(x)
# evaluate this cell in-place many times (Ctrl-Enter)
J = jacobian(x)
r = residual(x)
s = la.lstsq(...)[0]
x = x + s
print la.norm(r)
plot_iterate(x)
(Edit this cell for solution.)