ln2近似值的计算

您所在的位置:网站首页 ln2*ln2怎么算 ln2近似值的计算

ln2近似值的计算

2023-10-23 20:31| 来源: 网络整理| 查看: 265

ln2近似值的计算 题目与要求

【问题分析】分别用方法一和方法二法估算ln2的值(具体方法描述见下文),并比较两种方法的优缺点,对两种方法求解的结果可视化展示。

需求分析

在这里插入图片描述

设计

在这里插入图片描述

结果与分析

程序截图 在这里插入图片描述

由上图可以直观的看出:方法二比方法一收敛的要快;由以上的程序设计得知方法二要比方法一算法的时间复杂度高。两种方法各有利弊,应该根据实际需要进行选择。

源代码 import matplotlib.pyplot as plt import numpy as np def method01_ln2(num): ln2_collection = [] ln2 = 0 temp = -1 for i in range(num): temp = temp * (-1) ln2 = ln2 + temp * (1 / (i + 1)) ln2_collection.append(ln2) return ln2_collection def method02_ln2(num): ln2_collection2 = [] ln2 = 0 temp2 = 1 / 3 for i in range(0, num): temp1 = 2 * i + 1 temp2 = temp2 * 9 ln2 = ln2 + 1 / (temp1 * temp2) ln2_collection2.append(ln2 * 2) return ln2_collection2 n = 100 x_method01_ln2 = np.arange(0, n) x_method02_ln2 = np.arange(0, n) y_method01_ln2 = method01_ln2(n) y_method02_ln2 = method02_ln2(n) plt.scatter(x_method01_ln2, np.array(y_method01_ln2)) plt.scatter(x_method02_ln2, np.array(y_method02_ln2)) plt.title("ln2 estimate", fontsize=24) plt.xlabel("x", fontsize=14) plt.ylabel("y", fontsize=14) plt.tick_params(axis='both', labelsize=14) plt.legend(['method01', 'method02'], loc='upper right') plt.show()


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3