Hi. I am an optical engineer and I already have this code with me. Its quite helpful for visualisation of optical aberrations.
I have seen the code you have uploaded, and especially liked how the code started with defining everything. The spherical aberration you implemented is also correct.
But the later part is errenous.
Also you have implemented only few terms (4).
I can give you the correct code for upto 10 zernike terms which will include piston, tilt x and y, coma, astigmatism, defocus and spherical aberration etc.
For identification of which zernike terms corresponds to what aberration, I will also add comments for indicating them.
You can have the option of having the zernike function in polar coordinate or even Cartesian coordinate system. I prefer the Cartesian option.
Let me know. And best of luck to you.