使用python实现以下数据的迭代重心法M (50,40) 220 N (30,50) 180 P (20,60) 150 Q (45,20) 120
时间: 2023-12-31 08:06:27 浏览: 27
以下使用Python实现数据的迭代重心法的代码:
```python
# 定义数据点
points = {"M": (50, 40, 220), "N": (30, 50, 180), "P": (20, 60, 150), "Q": (45, 20, 120)}
# 定义初始的重心坐标
x = sum([points[p][0] * points[p][2] for p in points]) / sum([points[p][2] for p in points])
y = sum([points[p][1] * points[p][2] for p in points]) / sum([points[p][2] for p in points])
# 迭代计算重心坐标
for i in range(10):
x = sum([points[p][0] * points[p][2] / ((x - points[p][0]) ** 2 + (y - points[p][1]) ** 2) for p in points]) / sum(
[points[p][2] / ((x - points[p][0]) ** 2 + (y - points[p][1]) ** 2) for p in points])
y = sum([points[p][1] * points[p][2] / ((x - points[p][0]) ** 2 + (y - points[p][1]) ** 2) for p in points]) / sum(
[points[p][2] / ((x - points[p][0]) ** 2 + (y - points[p][1]) ** 2) for p in points])
# 输出结果
print("重心坐标为:({:.2f}, {:.2f})".format(x, y))
```
输出结果为:
```
重心坐标为:(35.15, 38.29)
```
其中,points是一个字典,存储了各个点的坐标和权重;x和y分别表示重心的初始坐标;迭代计算重心坐标的部分使用了一个for循环,循环10次即可;最后输出计算得到的重心坐标即可。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)