写4个数学建模的经典案例及代码程序
时间: 2024-09-20 10:04:45 浏览: 74
数学建模是一种将实际问题转化为数学语言的过程,通过解决这些问题来得出量化解决方案。以下是四个经典数学建模案例以及简化的示例代码:
1. **人口增长模型** - 梯度增长模型(如 logistic growth model)
```python
import numpy as np
def population_growth(initial_population, growth_rate, carrying_capacity):
time = np.linspace(0, 50, 100) # 时间范围
population = initial_population * (carrying_capacity / (1 + growth_rate * np.exp(-time)))
return population
# 示例用法
initial_pop = 1000
growth_rate = 0.05
cap_carrying = 10000
population_series = population_growth(initial_pop, growth_rate, cap_carrying)
```
2. **经济预测模型** - 线性回归模型(如股票价格预测)
```python
from sklearn.linear_model import LinearRegression
# 假设我们有历史股票数据
data = {'days': [1, 2, 3, ..., 100], 'prices': [10, 11, 12, ..., 20]}
model = LinearRegression()
model.fit(data['days'].reshape(-1, 1), data['prices'])
# 预测未来一天的价格
future_day = 101
predicted_price = model.predict([[future_day]])
```
3. **物流路线优化** - 贪心算法或遗传算法(如旅行商问题)
```python
def tsp(city_list):
n = len(city_list)
best_path = [city_list[0]]
current_city = city_list[0]
total_distance = 0
for _ in range(n - 1):
min_distance = float('inf')
next_city = None
for i in range(1, n):
if i != current_city and distance_matrix[current_city][i] < min_distance:
min_distance = distance_matrix[current_city][i]
next_city = i
best_path.append(next_city)
total_distance += min_distance
current_city = next_city
best_path.append(best_path[0]) # 关回起点
total_distance += distance_matrix[current_city][0]
return best_path, total_distance
# 使用示例,假设distance_matrix是一个二维数组存储城市之间的距离
best_route, shortest_distance = tsp(city_list)
```
4. **电力需求预测** - 时间序列分析(如 ARIMA 或 LSTM)
```python
from statsmodels.tsa.arima_model import ARIMA
import pandas as pd
# 假设有电力消耗时间序列数据
data = pd.read_csv("electricity_consumption.csv")
model = ARIMA(data['consumption'], order=(1, 1, 1))
results = model.fit()
forecast = results.forecast(steps=10)[0]
print("未来10天的预测电力需求:", forecast)
```
阅读全文