[數學建模-最佳化]擬合與插值
擬合
多項式擬合 numpy.polynomial
import
numpy
as
np
import
matplotlib。pyplot
as
plt
from
numpy。polynomial
import
Polynomial
as
P
rng
=
np
。
random
。
default_rng
(
1
)
x
=
np
。
linspace
(
0
,
2
*
np
。
pi
,
20
)
y
=
np
。
sin
(
x
)
+
rng
。
normal
(
scale
=
0。5
,
size
=
x
。
shape
)
p
=
P
。
fit
(
x
,
y
,
deg
=
5
)
# 次數
(
p
。
coef
)
plt
。
plot
(
x
,
y
,
‘o’
,
label
=
‘data’
)
xx
=
np
。
linspace
(
0
,
2
*
np
。
pi
,
200
)
plt
。
plot
(
xx
,
p
(
xx
),
‘——’
,
label
=
‘fit polynomial’
)
plt
。
xlabel
(
‘x’
)
plt
。
ylabel
(
‘y’
)
plt
。
legend
()
plt
。
show
()
[
0。08138754
-
3。38174606
-
0。40298685
5。58384396
0。35245668
-
2。4820491
]
函式擬合 scipy.optimize.curve_fit
返回值
popt: 最小二乘擬合得到引數
pcov: 協方差矩陣,其中np。sqrt(np。diag(pcov))為標準差
import
numpy
as
np
import
matplotlib。pyplot
as
plt
from
scipy。optimize
import
curve_fit
def
f
(
x
,
a
,
b
,
c
):
return
a
*
np
。
exp
(
-
b
*
x
)
+
c
xdata
=
np
。
linspace
(
0
,
4
,
50
)
y
=
f
(
xdata
,
2。5
,
1。3
,
0。5
)
# 待擬合函式
rng
=
np
。
random
。
default_rng
(
1
)
ydata
=
y
+
0。2
*
rng
。
normal
(
size
=
xdata
。
size
)
# 加入噪點
plt
。
plot
(
xdata
,
ydata
,
‘-’
,
label
=
‘data’
)
popt
,
pcov
=
curve_fit
(
f
,
xdata
,
ydata
)
# 擬合引數值, 協方差矩陣
(
popt
,
pcov
,
sep
=
‘
\n
’
)
plt
。
plot
(
xdata
,
f
(
xdata
,
*
popt
),
‘-’
,
label
=
‘(unconstrained) fit: a=
%5。3f
, b=
%5。3f
, c=
%5。3f
’
%
tuple
(
popt
))
popt
,
pcov
=
curve_fit
(
f
,
xdata
,
ydata
,
bounds
=
(
0
,
[
3。
,
1。
,
0。5
]))
(
popt
,
pcov
,
sep
=
‘
\n
’
)
plt
。
plot
(
xdata
,
f
(
xdata
,
*
popt
),
‘——’
,
label
=
‘(constrained) fit: a=
%5。3f
, b=
%5。3f
, c=
%5。3f
’
%
tuple
(
popt
))
plt
。
xlabel
(
‘x’
)
plt
。
ylabel
(
‘y’
)
plt
。
legend
()
plt
。
show
()
[
2。60437976
1。33487461
0。48456861
]
[[
0。01160596
0。00478154
-
0。00057784
]
[
0。00478154
0。01401482
0。00394553
]
[
-
0。00057784
0。00394553
0。00212948
]]
[
2。4936018
1。
0。36144807
]
[[
0。01155931
0。00216026
-
0。00169733
]
[
0。00216026
0。01217232
0。00623147
]
[
-
0。00169733
0。00623147
0。00467355
]]
插值
1-D 樣條scipy.interpolate.interp1d
引數
kind: ‘linear’,
‘nearest’
, ‘nearest-up’, ‘zero’,
‘slinear’, ‘quadratic’, ‘cubic’
,
‘previous’, ‘next’
import
numpy
as
np
import
matplotlib。pyplot
as
plt
from
scipy。interpolate
import
interp1d
x
=
np
。
linspace
(
0
,
10
,
11
,
endpoint
=
True
)
y
=
np
。
cos
(
-
x
**
2
/
9。0
)
f1
=
interp1d
(
x
,
y
,
kind
=
‘nearest’
)
f2
=
interp1d
(
x
,
y
,
kind
=
‘previous’
)
f3
=
interp1d
(
x
,
y
,
kind
=
‘next’
)
xx
=
np
。
linspace
(
0
,
10
,
1001
,
endpoint
=
True
)
plt
。
plot
(
x
,
y
,
‘o’
,
label
=
‘data’
)
plt
。
plot
(
xx
,
f1
(
xx
),
‘-’
,
label
=
‘nearest’
)
plt
。
plot
(
xx
,
f2
(
xx
),
‘——’
,
label
=
‘previous’
)
plt
。
plot
(
xx
,
f3
(
xx
),
‘:’
,
label
=
‘next’
)
plt
。
legend
()
plt
。
xlabel
(
‘x’
)
plt
。
ylabel
(
‘y’
)
plt
。
show
()
import
numpy
as
np
import
matplotlib。pyplot
as
plt
from
scipy。interpolate
import
interp1d
x
=
np
。
linspace
(
0
,
10
,
11
,
endpoint
=
True
)
y
=
np
。
cos
(
-
x
**
2
/
9。0
)
f4
=
interp1d
(
x
,
y
,
kind
=
‘linear’
)
f5
=
interp1d
(
x
,
y
,
kind
=
‘quadratic’
)
f6
=
interp1d
(
x
,
y
,
kind
=
‘cubic’
)
xx
=
np
。
linspace
(
0
,
10
,
1001
,
endpoint
=
True
)
plt
。
plot
(
x
,
y
,
‘o’
,
label
=
‘data’
)
plt
。
plot
(
xx
,
f4
(
xx
),
‘-’
,
label
=
‘linear’
)
plt
。
plot
(
xx
,
f5
(
xx
),
‘——’
,
label
=
‘quadratic’
)
plt
。
plot
(
xx
,
f6
(
xx
),
‘:’
,
label
=
‘cubic’
)
plt
。
legend
()
plt
。
xlabel
(
‘x’
)
plt
。
ylabel
(
‘y’
)
plt
。
show
()
#FormatImgID_5# 1-D B-樣條scipy.interpolate.splXXX
splrep(x, y, **args) B樣條插值曲線
1 <= k <= 5為曲線次數,預設為3
用作插值時s=0
splev(x, tck, der=0) 求樣條函式值和導數值
spalde(x, tck) 求所有階導數值
splint(a, b, tck) 求積分
import
numpy
as
np
import
matplotlib。pyplot
as
plt
from
scipy。interpolate
import
splev
,
splrep
,
splint
,
spalde
x
=
np
。
linspace
(
0
,
10
,
10
)
y
=
np
。
sin
(
x
)
spl3
=
splrep
(
x
,
y
,
k
=
3
)
# 三次樣條
spl2
=
splrep
(
x
,
y
,
k
=
2
)
# 二次樣條
xx
=
np
。
linspace
(
0
,
10
,
200
)
plt
。
plot
(
x
,
y
,
‘o’
,
label
=
‘data’
)
plt
。
plot
(
xx
,
np
。
sin
(
xx
),
‘——’
,
label
=
‘True’
)
plt
。
plot
(
xx
,
splev
(
xx
,
spl3
,
der
=
0
),
‘:’
,
label
=
‘Spline: k=3’
)
plt
。
plot
(
xx
,
splev
(
xx
,
spl2
,
der
=
0
),
‘——’
,
label
=
‘Spline: k=2’
)
plt
。
legend
()
plt
。
xlabel
(
‘x’
)
plt
。
ylabel
(
‘y’
)
plt
。
show
()
yders
=
spalde
(
xx
,
spl3
)
plt
。
figure
()
for
i
in
range
(
len
(
yders
[
0
])):
plt
。
plot
(
xx
,
[
d
[
i
]
for
d
in
yders
],
‘——’
,
label
=
f
“
{i}
derivative”
)
plt
。
legend
()
plt
。
xlabel
(
‘x’
)
plt
。
ylabel
(
‘y’
)
plt
。
title
(
‘All derivatives of a B-spline’
)
plt
。
show
()
def
integ
(
x
,
tck
,
constant
=-
1
):
x
=
np
。
atleast_1d
(
x
)
out
=
np
。
zeros
(
x
。
shape
,
dtype
=
x
。
dtype
)
for
n
in
range
(
len
(
out
)):
out
[
n
]
=
splint
(
0
,
x
[
n
],
tck
)
out
+=
constant
return
out
plt
。
plot
(
xx
,
integ
(
xx
,
spl3
),
‘-’
)
plt
。
title
(
‘Integral estimation from spline’
)
plt
。
xlabel
(
‘x’
)
plt
。
ylabel
(
‘y’
)
plt
。
show
()
參考資料.
Interpolation (scipy.interpolate) — SciPy v1.9.0 Manual
scipy.optimize.curve_fit — SciPy v1.9.0 Manual
Convenience Classes — NumPy v1.23 Manual
上一篇:有筒推薦|納麗德軍警產品線起底
下一篇:無人機拍攝?