Skip to main content
6-Contributor
July 16, 2025
Solved

More Efficient use of Recursion Formulas to create arrays and to flatten arrays into data vectors

  • July 16, 2025
  • 2 replies
  • 1413 views

Hi all:

I am looking for help/tips regarding more efficient/practical ways in MathCAD 15 to perform recursive calculations as it relates to creation of arrays, vectorizing the arrays (row by row), and then augmenting these data vectors into a final array.  I have attached MathCAD 15 file to illustrate my questions.

The attached file is intended to perform a least-squares fitting of optical wavefront data (optical path difference data) to a set of Zernike polynomials.  I have tried to describe each portion of the script so that it is easier to understand what I am trying to accomplish.

The first portion of the script creates two arrays (rho and theta) that contain the respective values over the unit circle.

As shown, the next section explicitly lists the mathematical form for of the first 36 Zernike polynomials and evaluates them at each coordinate position.  Once evaluated, individual arrays are created for each, evaluated Zernike polynomial so that in the end we have 36 individual arrays of the proper dimensions.  As many of you are probably aware, the Zernike polynomials are an orthogonal set of functions that can be generated up to any order using a recursion formula.  You will note that I have typed out the radial portion of the Zernike polynomial and the conditional rules for angular portion.  For this portion of the routine what I am hoping is if someone can help me create a solve block or iterative loop that allows me to only write out the recursion formulas for the Zernike polynomials if I provide a range for the n and integer values.

The portion of the routine takes the 36 individual Zernike arrays, converts them into data vectors (row by row), and then augments them into a single array.  The script in this section borrows from a method that I saw somebody show on the PTC community board a couple of years ago.  This approach does work, but again was wondering if there was a way to to perform these operations more efficiently.

The next section simply reads in the data file and then flattens it into a data vector.  The last section performs a series of matrix operations to make the arrays the correct dimensions and to calculate the coefficients for each individual Zernike term.

 

In brief, I am wondering if someone can help me more efficiently write out the second and third sections of this script by using the recursion formulates for Zernike polynomials and a more efficient way to flatten the arrays into data vectors.  Thanks in advance.

Best answer by terryhendicott

Hi,

Have included a worksheet that shows the difference between:-

a) calculating by formulas Zj(rho,theta) that needs a Normalization constant, and

b) calculating using the direct polynomial definition Znm(rho,theta) that does not need the Normalization constant. 

 

The Normalization worksheet shows the difference between a) and b)

Capture.JPG

Capture2.JPG

Capture3.JPG

 

I have made some alterations to the original file that stops recreation of the "file" matrix when you change the plot above.

Capture4.jpg

Cheers

Terry

2 replies

21-Topaz II
July 17, 2025

Hi,

Taken a while to answer but the enclosed worksheet will help.

Cheers

Terry

6-Contributor
July 17, 2025

Thanks Terry.  I appreciate the response and work.  I will review and let you know if I have any questions.

21-Topaz II
July 17, 2025

Hi,

Have made a correction and some additional information.  See items in yellow.

Capture.JPG

Capture3.JPG

Capture2.JPG

If you set the 3D plot to the following setting you get a plan view of disk in exact orientation as the Wikipedia disks.

Capture4.jpg

Cheers

Terry

 

19-Tanzanite
July 18, 2025

Not that I know what I'm doing here (unlike Terry), but this looked like it could be fun! So, perhaps the attached might be of some use.

 

I get the following images, which are similar to those on Wikipedia, though rotated:

Zernicke.png

21-Topaz II
July 18, 2025

Hi Alan,

Do not profess to know what I am doing.  Like you do it for fun.

Cheers

Terry