0% found this document useful (0 votes)
4 views

Chap9 CurveFitting Interpolation

The document discusses curve fitting and interpolation techniques, focusing on how to find optimal parameters for functions that best fit observed data. It includes examples of fitting a quadratic function to air density data with altitude and using various interpolation methods from the SciPy library. Additionally, it covers multivariate interpolation and optimization routines for minimizing functions.

Uploaded by

yekeentaiwo107
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Chap9 CurveFitting Interpolation

The document discusses curve fitting and interpolation techniques, focusing on how to find optimal parameters for functions that best fit observed data. It includes examples of fitting a quadratic function to air density data with altitude and using various interpolation methods from the SciPy library. Additionally, it covers multivariate interpolation and optimization routines for minimizing functions.

Uploaded by

yekeentaiwo107
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

chap9_CurveFitting_Interpolation

February 8, 2023

0.1 CURVE FITTING AND INTERPOLATION


0.2 CURVE FITTING
Curve fitting is a type of optimization that finds an optimal set of parameters for a defined function
that best fits a given set of observations.
Curve fitting is an optimization problem that finds a line that best fits a collection of observations.
It is easiest to think about curve fitting in two dimensions, such as a graph.
Consider that we have collected examples of data from the problem domain with inputs and outputs.
The x-axis is the independent variable or the input to the function. The y-axis is the dependent
variable or the output of the function. We don’t know the form of the function that maps examples
of inputs to outputs, but we suspect that we can approximate the function with a standard function
form.
Curve fitting involves first defining the functional form of the mapping function (also called the
basis function or objective function), then searching for the parameters to the function that result
in the minimum error.
Error is calculated by using the observations from the domain and passing the inputs to our
candidate mapping function and calculating the output, then comparing the calculated output to
the observed output.
Once fit, we can use the mapping function to interpolate or extrapolate new points in the domain. It
is common to run a sequence of input values through the mapping function to calculate a sequence
of outputs, then create a line plot of the result to show how output varies with input and how well
the line fits the observed points.
The key to curve fitting is the form of the mapping function.
Table below shows how the relative density 𝜌 of air varies with altitude ℎ. Plot a graph of � versus
h. Fit the data in the Table to a quadratic function. Find 𝜌 at ℎ = 8 𝑘𝑚.

ℎ(𝑘𝑚) 𝜌
0.000 1.0000
1.525 0.8617
3.050 0.7385
4.575 0.6292
6.100 0.5328
7.625 0.4481

1
ℎ(𝑘𝑚) 𝜌
9.150 0.3741

[1]: from pylab import plot,show


from numpy import array

h = array([0,1.525,3.050,4.575,6.10,7.625,9.150])
rho = array([1,0.8617,0.7385,0.6292,0.5328,0.4481,0.3741])

plot(h,rho)
show()

[3]: from pylab import plot,show


from numpy import array
from scipy.optimize import curve_fit

h = array([0,1.525,3.050,4.575,6.10,7.625,9.150])
rho = array([1,0.8617,0.7385,0.6292,0.5328,0.4481,0.3741])

def fquad(h,a,b,c):
return a*h**2+b*h+c

yquad,errq = curve_fit(fquad,h,rho)
a=yquad[0]
b=yquad[1]

2
c=yquad[2]
print('a = ',a)
print('b = ',b)
print('c = ',c)
#print('error = ',errq)

print("rho at h=8.0 km is",fquad(8.0,a,b,c))


#print("rho at h=4.575 km is",fquad(4.575,a,b,c))

a = 0.0027632101009355467
b = -0.09344730684922747
c = 0.9988952381654874
rho at h=8.0 km is 0.4281622298315426

1 INTERPOLATION
The package scipy.interpolate contains a large variety of functions and classes for interpolation and
splines in one and more dimensions. Some of the more important are described in this section.

1.1 Univariate Interpolation


The most straightforward one-dimensional interpolation functionality is provided by
scipy.interpolate.interp1d . Given arrays of points x and y , a function is returned, which
can be called to generate interpolated values at intermediate values of x . The default interpolation
scheme is linear, but other options allow for different schemes.
Kind = ‘linear’, ‘nearest’, ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’, ‘previous’, ‘next’.
Table below shows how the relative density 𝜌 of air varies with altitude ℎ. Use interpolation to find
𝜌 at ℎ = 8 𝑘𝑚.

ℎ(𝑘𝑚) 𝜌
0.000 1.0000
1.525 0.8617
3.050 0.7385
4.575 0.6292
6.100 0.5328
7.625 0.4481
9.150 0.3741

[7]: import numpy as np


from scipy.interpolate import interp1d

from numpy import array

h = array([0,1.525,3.050,4.575,6.10,7.625,9.150])
rho = array([1,0.8617,0.7385,0.6292,0.5328,0.4481,0.3741])

3
f_linear = interp1d(h, rho)
f_cubic = interp1d(h, rho, kind='cubic')
f_nearest = interp1d(h, rho, kind='nearest')
f_quadratic = interp1d(h, rho, kind='quadratic')
f_slinear = interp1d(h, rho,kind='slinear')
f_zero = interp1d(h, rho,kind='zero')

hh=8.0

print('linear = ',f_linear(hh))
print('cubic = ',f_cubic(hh))
print('nearest = ',f_nearest(hh))
print('slinear = ',f_slinear(hh))
print('quadratic = ',f_quadratic(hh))
print('zero = ',f_zero(hh))

linear = 0.42990327868852457
cubic = 0.4289469617028989
nearest = 0.4481
slinear = 0.42990327868852457
quadratic = 0.4289242135593473
zero = 0.4481

1.1.1 Another Example:


consider the function

𝑓(𝑥) = 𝐴𝑒−𝑘𝑥 cos(2𝜋𝜈𝑥)

[9]: import numpy as np


from scipy.interpolate import interp1d
import matplotlib . pyplot as plt
A,nu,k = 10, 4, 2

def f(x, A, nu , k):


return A * np.exp(-k*x) * np.cos (2* np.pi * nu * x)
xmax, nx = 0.5, 8
x = np. linspace (0, xmax , nx)
y = f(x, A, nu , k)
f_nearest = interp1d (x, y, kind='nearest')
f_linear = interp1d (x, y)
f_cubic = interp1d (x, y, kind='cubic')
x2 = np. linspace (0, xmax , 100)
plt.plot(x, y, 'o', label ='data points')
plt.plot(x2 , f(x2 , A, nu , k), label ='exact')

4
plt.plot(x2 , f_nearest (x2), label ='nearest')
plt.plot(x2 , f_linear (x2), label ='linear')
plt.plot(x2 , f_cubic (x2), label ='cubic')
plt. legend ()
plt.show ()

1.2 Multivariate Interpolation


1.2.1 Interpolation from a Rectangular Grid
The simplest two-dimensional interpolation routine is scipy.interpolate.interp2d . It requires a two-
dimensional array of values, z , and the two (one-dimensional) coordinate arrays x and y to which
they correspond. These arrays need not have constant spacing.
Three kinds of interpolation spline are supported through the kind argument: ‘linear’ (the default),
‘cubic’ and ‘quintic’ .

1.3 Example:
In the following example, we calculate the function

𝜋𝑥 𝑦/2
𝑧(𝑥, 𝑦) = sin ( )𝑒
2

on a grid of points (𝑥, 𝑦) which is not evenly spaced in the y-direction. We then use
scipy.interpolate.interp2d to interpolate these values onto a finer, evenly spaced (𝑥, 𝑦) grid

5
[10]: import numpy as np
from scipy.interpolate import interp2d
import matplotlib.pyplot as plt
x = np.linspace(0, 4, 13)
y = np.array([0, 2, 3, 3.5, 3.75 , 3.875 , 3.9375 , 4])
X, Y = np.meshgrid(x, y)
Z = np.sin(np.pi*X/2)*np.exp(Y/2)
x2 = np. linspace (0, 4, 65)
y2 = np. linspace (0, 4, 65)
f = interp2d(x, y, Z, kind='cubic')
Z2 = f(x2, y2)
fig, ax = plt.subplots(nrows =1, ncols =2)
ax[0].pcolormesh(X, Y, Z)
X2, Y2 = np.meshgrid (x2 , y2)
ax[1].pcolormesh(X2 , Y2 , Z2)
plt.show()

1.3.1 Minimization
SciPy’s optimization routines minimize a function of one or more variables, f (x 1 , x 2 , . . . , x n
). To find the maximum, one determines the minimum of − f (x 1 , x 2 , . . . , x n ).

1.3.2 Example
Consider the Himmelblau’s function

6
𝑓(𝑥, 𝑦) = (𝑥2 + 𝑦 − 11)2 + (𝑥 + 𝑦2 − 7)2 .

To find a minimum, call minimize with some initial guess, say (x, y) = (0, 0):

[2]: def f(X):


x, y = X
return (x**2 + y - 11)**2 + (x + y**2 - 7)**2

from scipy.optimize import minimize


out=minimize (f, (0, 0))
print(out)

fun: 1.3782261326630835e-13
hess_inv: array([[ 0.01578229, -0.0094806 ],
[-0.0094806 , 0.03494937]])
jac: array([-3.95019832e-06, -1.19075540e-06])
message: 'Optimization terminated successfully.'
nfev: 64
nit: 10
njev: 16
status: 0
success: True
x: array([2.99999994, 1.99999999])

[6]: print(f((2.99999994,1.99999999)))

1.4689999624268296e-13

1.4 Assignment: Study Nonlinear Least-Squares Fitting


SciPy’s general nonlinear least-squares fitting routine is scipy.optimize.leastsq , which has as its
most basic call signature: scipy.optimize.leastsq(func, x0, args=()) .
This will attempt to fit a sequence of data points, y , to a model function, f , which depends on
one or more fit parameters.
[ ]:

You might also like