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.


