Skip to main content

Hello, my problem is the following:


I would like to create a spherical surface (z=r-sqrt(r^2-(x2.^2)-(y2.^2))) using grid sag. Firstly, I created a MATLAB script to generate the .dat file which contains the surface info with this aspect:

nx ny delx dely unitflag xdec ydec
z dz/dx dz/dy d2z/dxdy nodata
.
.

The problem is that when I import the file to Zemax, it creates a surface like this one instead of a sphere:

My MATLAB Code:

%% Generating the sphere
x=linspace(-5,5,1000);
y=linspace(-5,5,1000);
>x2,y2]=meshgrid(x,y);
r=40; %radius (mm)
z=r-sqrt(r^2-(x2.^2)-(y2.^2)); %Equation

%% Zemax file
%Vector definition (nx ny delx dely unitflag(mm) decx decy)
dat==1000 1000 0.01 0.01 0 0 0];
%z y derivatives vector:
dzdx=zeros(1000000,1);
dzdy=zeros(1000000,1);
dzdxdy=zeros(1000000,1);
nodata=zeros(1000000,1);
z_columna=reshape(z,1000000,1);

dato==z_columna dzdx dzdy dzdxdy nodata];

%File creation (.dat):
eval(('fid=fopen("esfera40.dat", ''w'');']);
fprintf(fid, '%d %d %1.10g %1.10g %1.f %1.f %1.f \n', dat(1), dat(2), dat(3), dat(4), dat(5), dat(6), dat(7));
fprintf(fid, '%0.20g %d %d %d %d\n', dato);
fclose(fid);

Try a very simple modification of your second fprintf statement (replacing the array “dato” with its transpose):

fprintf(fid, '%0.20g %d %d %d %d\n', dato');

 


Hi @RosaVA - I wrote a Knowledgebase article on this topic in 2020. It provides Matlab code for generating a DAT file with a given equation. Perhaps that could help? The article is here: How to write a Grid Sag DAT file programmatically – Knowledgebase (zemax.com)


Reply