- 注册时间
- 2017-1-14
- 最后登录
- 1970-1-1
- 威望
- 星
- 金币
- 枚
- 贡献
- 分
- 经验
- 点
- 鲜花
- 朵
- 魅力
- 点
- 上传
- 次
- 下载
- 次
- 积分
- 11148
- 在线时间
- 小时
|
各位!这代码错在那里??谢谢各位!!!
{{3, 0.225045}, {4, 0.462183}, {5, 0.66083}, {6, 0.826997}, {7, 0.97415}, {8, 1.09881}, {9, 1.22222}, {10, 1.3246}, {11, 1.3999}, {12, 1.49023}, {13, 1.54949}, {14, 1.61062}, {15, 1.65932},
{16, 1.71313}, {17, 1.76778}, {18, 1.80131}, {19, 1.83594}, {20, 1.87825}, {21, 1.92483}, {22, 1.94676}, {23, 1.97752}, {24, 2.00061}, {25, 2.03386}, {26, 2.05089}, {27, 2.08}, {28, 2.10234}}
generatePoint[n_] := RandomPoint[Disk[], n]; hullPolygon[pts_] := MeshPrimitives[ConvexHullMesh[pts], 2][[1]]; hullArea[pts_] := Area[hullPolygon[pts]];
expectedArea[n_, samples_ : 2000] := Mean[Table[hullArea[generatePoint[n]], {samples}]]; Table[{n, expectedArea[n, 2000]}, {n, 3, 28}]
提速一下。——代码没变。
generatePoint[n_] := RandomPoint[Disk[], n]; hullPolygon[pts_] := MeshPrimitives[ConvexHullMesh[pts], 2][[1]]; hullArea[pts_] := Area[hullPolygon[pts]];
expectedArea[n_, samples_ : 500] := Mean[Table[hullArea[generatePoint[n]], {samples}]]; Table[{3^n, expectedArea[3^n, 500]}, {n, 12}]
{{3, 0.220445}, {9, 1.22915}, {27, 2.07739}, {81, 2.60394}, {243, 2.87862}, {729, 3.0119}, {2187, 3.07887}, {6561, 3.11126}, {19683, 3.12706}, {59049, 3.13456}, {177147, 3.13821}, {531441, 3.13997}}
特别地,n=1000000。——代码没变。
generatePoint[n_] := RandomPoint[Disk[], n]; hullPolygon[pts_] := MeshPrimitives[ConvexHullMesh[pts], 2][[1]]; hullArea[pts_] := Area[hullPolygon[pts]];
expectedArea[n_, samples_ : 100] := Mean[Table[hullArea[generatePoint[n]], {samples}]]; Table[{n, expectedArea[n, 100]}, {n, 1000000, 1000000}]
{{1000000, 3.14052}}
(*1. 生成圆内均匀随机点*)generatePoint[n_] := RandomPoint[Disk[], n];
(*2. 获取凸包多边形*)hullPolygon[pts_] := MeshPrimitives[ConvexHullMesh[pts], 2][[1]];
(*3. 计算凸包面积*)hullArea[pts_] := Area[hullPolygon[pts]];
(*4. 蒙特卡洛期望*)expectedArea[n_, samples_ : 2000] := Mean[Table[hullArea[generatePoint[n]], {samples}]];
(*5. 计算 n=3 到 28,全部模拟*)Table[{n, expectedArea[n, 2000]}, {n, 3, 28}]
DeepSeek——说上面的代码是错的。下面的代码才是正确的——会出来正确数据。——我这个Mathematica12.1不能用。——我就想看看这些数据!谢谢各位!!谢谢!!!
python
- import numpy as np
- from scipy.spatial import ConvexHull
- def expected_area(n, samples=200000):
- total = 0.0
- for _ in range(samples):
- # 单位圆内均匀取点
- theta = np.random.uniform(0, 2*np.pi, n)
- r = np.sqrt(np.random.uniform(0, 1, n))
- pts = np.column_stack([r*np.cos(theta), r*np.sin(theta)])
- if n == 3:
- area = 0.5 * abs(np.cross(pts[1]-pts[0], pts[2]-pts[0]))
- else:
- hull = ConvexHull(pts)
- area = hull.volume # 在2D中volume指面积
- total += area
- return total / samples
- for n in range(3, 11):
- print(f"n={n}: {expected_area(n):.4f}")
复制代码 |
|