# Modeling deviations from a spherical cap?

**URL:** <https://community.wolfram.com/t/modeling-deviations-from-a-spherical-cap/18974>\
**Category:** Questions\
**Tags:** optics, modeling\
**Created:** [October 15, 2021, 12:31am UTC](https://community.wolfram.com/t/modeling-deviations-from-a-spherical-cap/18974 "2021-10-15T00:31:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![drargento](https://avatars.discourse-cdn.com/v4/letter/d/b2d939/32.png) [@drargento](https://community.wolfram.com/u/drargento)\
**Post date:** [October 15, 2021, 12:31am UTC](https://community.wolfram.com/t/modeling-deviations-from-a-spherical-cap/18974/1 "2021-10-15T00:31:48Z")

</div>

I have this point cloud that represents {x,y,z} coordinates of the surface of a convex lens. I would like to compute the deviations of each point from a spherical cap that would be the best fit to the data. I tried using FindFit, but the results don’t make any sense and I get widely different results depending on my initial guesses. Any suggestions?

```
FindFit[{#[[1]], #[[2]], #[[3]], 1} & /@ centerA, ((x - a)^2 + (y - b)^2 + (z - c)^2)/r^2, {{a, 0.005}, {b, 0.005}, {c, 1}, {r, 1}}, {x, y, z}]

```

[centerA.csv](https://community.wolfram.com/uploads/short-url/e0XCJaKW45RL2W6NXFZOVGfFwxZ.csv) (1.92 MB)

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [October 15, 2021, 7:23am UTC](https://community.wolfram.com/t/modeling-deviations-from-a-spherical-cap/18974/2 "2021-10-15T07:23:28Z")

</div>

You could do it with `Minimize`:

```
data = RandomReal[{0, 1}, {10, 3}];
dist[{x_, y_, z_}] = RegionDistance[Sphere[{a, b, c}, r], {x, y, z}];
Minimize[{Total@Map[dist, data], r > 0}, {a, b, c, r}]

```

---

<div class="post-metadata">

**Author:** ![drargento](https://avatars.discourse-cdn.com/v4/letter/d/b2d939/32.png) [@drargento](https://community.wolfram.com/u/drargento)\
**Post date:** [October 15, 2021, 5:02pm UTC](https://community.wolfram.com/t/modeling-deviations-from-a-spherical-cap/18974/3 "2021-10-15T17:02:39Z")

</div>

That works really nicely. Thanks. I didn’t know the RegionDistance function; very useful!
