Biomathematicus

Science, Technology, Engineering, Art, Mathematics

While on a plane, my laptop ran out of battery. I was working in MATLAB. I switched to my iPad and started coding in Pythonista a simple program to produce points on a sphere. After half an an hour, I had coded a program that computed the coordinates of points along meridians. I needed this to test an algorithm in artificial neural networks that differentiates points belonging to two different concentric spheres. The source code is at the bottom.

Points on a sphere, developed with Pythonista on an iPad

When I landed, I sent to source code to my laptop. I planned to simply call the Python function from the larger MATLAB program. But once I ran the code, there was a syntax error due to differences in versions of the plotlib library. In the iPad, the following instruction works:

ax = fig.gca(projection='3d')

However, in my laptop, I had to use:

ax = fig.add_subplot(projection='3d')

Not a biggie, I thought. However, the the result of the same program running from a laptop was very different:

Running the same program on a laptop produces this plot

This is one of those “W.T.F?” or “What to Fathom?” moments. “Well,” I thought, “It’s just a matter of finding the correct syntax for the latest version of the library.” However, why bother? Generative artificial intelligence can code and translate across programming languagesโ€ฆ at least that is the claim.

Enter ChatGPT. I entered the command โ€œtranslate the following program into MATLAB,โ€ followed by the program I coded on the iPad. The result was yet a third version of what I needed to achieve.

The translation by ChatGPT of the Python program PointsOnASphere to MATLAB

Here you have it. Three different platforms, โ€œsameโ€ program, three different results. Of course, it is trivial so solve the problem in each platform. In the end, instances like this are the reasons why I stick to MATLAB for algorithm development… and ChatGPT has a long way to go.

# Points on a sphere
# By Juan B. Gutiรฉrrez - biomathematicus.me
import math as mt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure(1)
ax = fig.add_subplot(projection='3d')
#ax = fig.gca(projection='3d')

# initialize variables
stepCnt = 30
radius = 10
x = np.empty((stepCnt + 1,))
y = np.empty((stepCnt + 1,))
z = np.empty((stepCnt + 1,))
x[0], y[0], z[0] = (radius, 0, 0)
Theta = np.linspace(0, 2*mt.pi, stepCnt)
Phi = np.linspace(0, mt.pi, stepCnt)

for phi in Theta: #yes, I know... I swapped phi and theta during development.. ooops
    i = 0
    for theta in Phi:
        x[i] = radius * mt.sin(theta) * mt.cos(phi)
        y[i] = radius * mt.sin(theta) * mt.sin(phi)
        z[i] = radius * mt.cos(theta)
        i = i + 1
    ax.scatter(x, y, z)


ax.set_xlabel("X Axis")
ax.set_ylabel("Y Axis")
ax.set_zlabel("Z Axis")
ax.set_title("Points on a sphere")

plt.show()