欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實現(xiàn)數(shù)學模型(插值、擬合和微分方程)

 更新時間:2020年11月13日 10:13:08   作者:ManTou饅頭  
這篇文章主要介紹了python實現(xiàn)數(shù)學模型(插值、擬合和微分方程),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

問題1 車輛數(shù)量估計

題目描述

交通管理部門為了掌握一座橋梁的通行情況,在橋梁的一端每隔一段不等的時間,連續(xù)記錄1min內(nèi)通過橋梁的車輛數(shù)量,連續(xù)觀測一天24h的通過車輛,車輛數(shù)據(jù)如下表所示。試建立模型分析估計這一天中總共有多少車輛通過這座橋梁。

在這里插入圖片描述

python 實現(xiàn)(關(guān)鍵程序)

def get_line(xn, yn):
    def line(x):
        index = -1
        # 找出x所在的區(qū)間
        for i in range(1, len(xn)):
            if x <= xn[i]:
                index = i - 1
                break
            else:
                i += 1
        if index == -1:
            return -100
        # 插值
        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]
# 分段線性插值函數(shù)
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("估計一天通過的車輛:%d" % sum_num)

結(jié)果

在這里插入圖片描述在這里插入圖片描述

問題2 舊車平均價格

題目描述

某年美國舊車價格的調(diào)查資料如下表所示,其中 x i x_i xi​表示轎車的使用年數(shù), y i y_i yi​表示相應的平均價格。試分析用什么形式的曲線擬合表中所給的數(shù)據(jù),并預測使用4.5年后轎車的平均價格大致為多少?

在這里插入圖片描述

Python 實現(xiàn)(關(guān)鍵程序)

from scipy.optimize import curve_fit
def func(x, a, b, c): # 指數(shù)函數(shù)擬合
  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)

結(jié)果

在這里插入圖片描述
在這里插入圖片描述

問題3 微分方程組求解

題目描述

求下列微分方程組(豎直加熱板的自然對流)的數(shù)值解

Python實現(xiàn)(關(guān)鍵程序)

from scipy.integrate import solve_ivp
def natural_convection(eta, y): # 將含有兩個未知函數(shù)的高階微分方程降階,得到由2+3個一階微分方程組成的方程組
  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)

結(jié)果

在這里插入圖片描述

問題4 野兔數(shù)量 題目描述

某地區(qū)野兔的數(shù)量連續(xù)9年的統(tǒng)計數(shù)量(單位:十萬)如下表所示.預測t = 9, 10時野兔的數(shù)量。

在這里插入圖片描述

Python實現(xiàn)(關(guān)鍵程序)

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("線性擬合表達式:", np.poly1d(fit))
num_fit = np.polyval(fit, year)
plt.plot(year, num, 'ro', label='原始數(shù)據(jù)')
plt.plot(year, num_fit, 'b-',label='擬合曲線')
year_later = np.arange(8, 11, 0.5)
num_fit_curve = fit[0] * year_later + fit[1]

結(jié)果

在這里插入圖片描述

到此這篇關(guān)于python實現(xiàn)數(shù)學模型(插值、擬合和微分方程)的文章就介紹到這了,更多相關(guān)python數(shù)學模型內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論