用python求出1-100的和的五种方法

#方法一
sum2=0
for i in range(1,101):
sum2+=i
print(sum2)

#方法二
def fsum(n):
s=0
for i in range(1,n+1):
s+=i
print(s)
fsum(100)

#方法三 while循环实现
def fsum1(n):
i=0 #初始化变量
s=0
while i<n+1: #条件判断
s+=i #循环体
i+=1 #改变变量
print(s)
fsum1(100)

#方法四 递归的思路
def fsum2(n):
if n==1:
return 1
else:
return n+fsum2(n-1)

print(fsum2(100))

#方法五 一句代码搞定
print(sum(list(range(1,101))))

  • 4
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
人口模型是用来描述人口增长和变化的数学模型,其中最常用的五种方法是:欧拉方法、改进欧拉方法、龙格-库塔方法、四阶龙格-库塔方法和欧拉前瞻后插法。下面是 Python 实现这五种方法的完整代码: 1. 欧拉方法 ```python import matplotlib.pyplot as plt # 定义欧拉方法函数 def euler(f, x0, y0, h, n): # 初始化 x 和 y x = [x0] y = [y0] # 迭代计算 for i in range(n): y1 = y[-1] + h * f(x[-1], y[-1]) y.append(y1) x.append(x[-1] + h) # 返回结果 return x, y # 定义人口增长模型函数 def population_growth(t, P): r = 0.2 K = 1000 return r * P * (1 - P/K) # 设置参数 x0 = 0 y0 = 100 h = 0.1 n = 100 # 计算欧拉方法的结果 x, y = euler(population_growth, x0, y0, h, n) # 绘图 plt.plot(x, y) plt.xlabel('Time') plt.ylabel('Population') plt.title('Euler Method for Population Growth') plt.show() ``` 2. 改进欧拉方法 ```python import matplotlib.pyplot as plt # 定义改进欧拉方法函数 def improved_euler(f, x0, y0, h, n): # 初始化 x 和 y x = [x0] y = [y0] # 迭代计算 for i in range(n): y_half = y[-1] + 0.5 * h * f(x[-1], y[-1]) y1 = y[-1] + h * f(x[-1] + 0.5 * h, y_half) y.append(y1) x.append(x[-1] + h) # 返回结果 return x, y # 定义人口增长模型函数 def population_growth(t, P): r = 0.2 K = 1000 return r * P * (1 - P/K) # 设置参数 x0 = 0 y0 = 100 h = 0.1 n = 100 # 计算改进欧拉方法的结果 x, y = improved_euler(population_growth, x0, y0, h, n) # 绘图 plt.plot(x, y) plt.xlabel('Time') plt.ylabel('Population') plt.title('Improved Euler Method for Population Growth') plt.show() ``` 3. 龙格-库塔方法 ```python import matplotlib.pyplot as plt # 定义龙格-库塔方法函数 def runge_kutta(f, x0, y0, h, n): # 初始化 x 和 y x = [x0] y = [y0] # 迭代计算 for i in range(n): k1 = h * f(x[-1], y[-1]) k2 = h * f(x[-1] + 0.5 * h, y[-1] + 0.5 * k1) k3 = h * f(x[-1] + 0.5 * h, y[-1] + 0.5 * k2) k4 = h * f(x[-1] + h, y[-1] + k3) y1 = y[-1] + (k1 + 2 * k2 + 2 * k3 + k4) / 6 y.append(y1) x.append(x[-1] + h) # 返回结果 return x, y # 定义人口增长模型函数 def population_growth(t, P): r = 0.2 K = 1000 return r * P * (1 - P/K) # 设置参数 x0 = 0 y0 = 100 h = 0.1 n = 100 # 计算龙格-库塔方法的结果 x, y = runge_kutta(population_growth, x0, y0, h, n) # 绘图 plt.plot(x, y) plt.xlabel('Time') plt.ylabel('Population') plt.title('Runge-Kutta Method for Population Growth') plt.show() ``` 4. 四阶龙格-库塔方法 ```python import matplotlib.pyplot as plt # 定义四阶龙格-库塔方法函数 def rk4(f, x0, y0, h, n): # 初始化 x 和 y x = [x0] y = [y0] # 迭代计算 for i in range(n): k1 = h * f(x[-1], y[-1]) k2 = h * f(x[-1] + 0.5 * h, y[-1] + 0.5 * k1) k3 = h * f(x[-1] + 0.5 * h, y[-1] + 0.5 * k2) k4 = h * f(x[-1] + h, y[-1] + k3) y1 = y[-1] + (k1 + 2 * k2 + 2 * k3 + k4) / 6 y.append(y1) x.append(x[-1] + h) # 返回结果 return x, y # 定义人口增长模型函数 def population_growth(t, P): r = 0.2 K = 1000 return r * P * (1 - P/K) # 设置参数 x0 = 0 y0 = 100 h = 0.1 n = 100 # 计算四阶龙格-库塔方法的结果 x, y = rk4(population_growth, x0, y0, h, n) # 绘图 plt.plot(x, y) plt.xlabel('Time') plt.ylabel('Population') plt.title('RK4 Method for Population Growth') plt.show() ``` 5. 欧拉前瞻后插法 ```python import matplotlib.pyplot as plt # 定义欧拉前瞻后插法函数 def predictor_corrector(f, x0, y0, h, n): # 初始化 x 和 y x = [x0] y = [y0] # 迭代计算 for i in range(n): y_predict = y[-1] + h * f(x[-1], y[-1]) y_correct = y[-1] + 0.5 * h * (f(x[-1], y[-1]) + f(x[-1] + h, y_predict)) y.append(y_correct) x.append(x[-1] + h) # 返回结果 return x, y # 定义人口增长模型函数 def population_growth(t, P): r = 0.2 K = 1000 return r * P * (1 - P/K) # 设置参数 x0 = 0 y0 = 100 h = 0.1 n = 100 # 计算欧拉前瞻后插法的结果 x, y = predictor_corrector(population_growth, x0, y0, h, n) # 绘图 plt.plot(x, y) plt.xlabel('Time') plt.ylabel('Population') plt.title('Predictor-Corrector Method for Population Growth') plt.show() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值