- Official account number in the same name "ManTou Steamed bread " to update , Give me some advice. ,ballball u.
- Welcome to any questions CSDN Comment on personal message 、 Welcome official account ,vx Direct messages
- Program reply in official account “ManTouex5” obtain .
interpolation 、 Fitting and differential equations -python Realization
problem 1 Estimation of the number of vehicles
Title Description
In order to master the traffic situation of a bridge, the traffic management department should , At one end of the bridge at different intervals , Continuous record 1min Number of vehicles passing through the bridge , Continuous observation for one day 24h Through the vehicle , The vehicle data are shown in the table below . Try to build a model to estimate how many vehicles will cross the bridge during the day .
python Realization ( Key procedures )
def get_line(xn, yn):
def line(x):
index = -1
# find x Range
for i in range(1, len(xn)):
if x <= xn[i]:
index = i - 1
break
else:
i += 1
if index == -1:
return -100
# interpolation
result = (x - xn[index + 1]) * yn[index] / float((xn[index] - xn[index + 1])) + (x - xn[index]) * yn[
index + 1] / float((xn[index + 1] - xn[index]))
return result
return line
time = [0, 2, 4, 5, 6, 7, 8,
9, 10.5, 11.5, 12.5, 14, 16, 17,
18, 19, 20, 21, 22, 23, 24]
num = [2, 2, 0, 2, 5, 8, 25,
12, 5, 10, 12, 7, 9, 28,
22, 10, 9, 11, 8, 9, 3]
# Piecewise linear interpolation function
lin = get_line(time, num)
# time_n = np.arange(0, 24, 1/60)
time_n = np.linspace(0, 24, 24*60+1)
num_n = [lin(i) for i in time_n]
sum_num = sum(num_n)
print(" Estimate the number of vehicles passing in a day :%d" % sum_num)
result
problem 2 Average price of used car
Title Description
The survey data of used car prices in the United States in a certain year are shown in the table below , among x i x_i xi Indicates the service life of the car , y i y_i yi Represents the corresponding average price . Try to analyze the form of curve fitting the data given in the table , And predict the use 4.5 What is the average price of a car after the year ?
Python Realization ( Key procedures )
from scipy.optimize import curve_fit
def func(x, a, b, c): # Exponential function fitting
return a * (b**(x-1)) + c
year = np.arange(1, 11, 1)
price = [2615, 1943, 1494, 1087, 765, 538, 484, 290, 226, 204]
popt, pcov = curve_fit(func, year, price)
a = popt[0]
b = popt[1]
c = popt[2]
price_fit = func(year, a, b, c)
result
problem 3 Solution of differential equations
Title Description
Find the following system of differential equations ( Natural convection in vertical heating plate ) The numerical solution of
{ d 3 f d η 3 + 3 f d 2 f d η 2 − 2 ( d f d η ) 2 + T = 0 d 2 T d η 2 + 2.1 f d T d η = 0 \left\{\begin{array}{l}\frac{\mathrm{d}^{3} f}{\mathrm{d} \eta^{3}}+3 f \frac{\mathrm{d}^{2} f}{\mathrm{d} \eta^{2}}-2\left(\frac{\mathrm{d} f}{\mathrm{d} \eta}\right)^{2}+T=0 \\ \frac{\mathrm{d}^{2} T}{\mathrm{d} \eta^{2}}+2.1 f \frac{\mathrm{d} T}{\mathrm{d} \eta}=0\end{array}\right. ⎩⎨⎧dη3d3f+3fdη2d2f−2(dηdf)2+T=0dη2d2T+2.1fdηdT=0
When known η = 0 \eta=0 η=0 when , f = 0 , d f d η = 0 , d 2 f d η 2 = 0.68 , T = 1 , d T d η = − 0.5 f=0, \frac{\mathrm{d} f}{\mathrm{d} \eta}=0, \frac{\mathrm{d}^{2} f}{\mathrm{d} \eta^{2}}=0.68, T=1, \frac{\mathrm{d} T}{\mathrm{d} \eta}=-0.5 f=0,dηdf=0,dη2d2f=0.68,T=1,dηdT=−0.5 It is required to be in the interval [0,10] Draw the curve of the numerical solution .
Python Realization ( Key procedures )
from scipy.integrate import solve_ivp
def natural_convection(eta, y): # The order of higher order differential equation with two unknown functions is reduced , Get by 2+3 A system of first order differential equations
T1 = y[0]
T2 = y[1]
f1 = y[2]
f2 = y[3]
f3 = y[4]
return T2, -2.1*f1*T2, f2, f3, -3*f1*f3 + 2*(f2**2)-T1
eta = np.linspace(0, 10, 1000)
eta_span = [0, 10]
init = np.array([ 1, -0.5, 0, 0, 0.68])
curve = solve_ivp(natural_convection, eta_span, init, t_eval=eta)
result
problem 4 Number of rabbits
Title Description
The number of rabbits in a certain area is continuous 9 Annual statistics ( Company : One hundred thousand ) As shown in the following table . forecast t = 9, 10 The number of rabbits .
Python Realization ( Key procedures )
import numpy as np
year = np.arange(0, 9, 1)
num = [5, 5.9945, 7.0932, 8.2744, 9.5073, 10.7555, 11.9804, 13.1465, 14.2247]
fit = np.polyfit(year, num, 1)
print(" Linear fitting expression :", np.poly1d(fit))
num_fit = np.polyval(fit, year)
plt.plot(year, num, 'ro', label=' Raw data ')
plt.plot(year, num_fit, 'b-',label=' Fit the curve ')
year_later = np.arange(8, 11, 0.5)
num_fit_curve = fit[0] * year_later + fit[1]