MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

### Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

# Problem in running the following code

Asked by Pranjal Pathak on 5 Feb 2013
This question is closed and may reopen in the future if edited.

Can anyone please help me to run the following matlab code?

EDIT: formatted code [the cyclist]

```% This function reconstructs wave front from slope matrices dZx and dZy
%With spacing dx upto 'terms' Zernike polynomials
```
```function A=ZernikeReconstruction(dZx, dZy, dx, terms);
[n, m]=size(dZx);
nn= n;
xx=reshape(dZx, nn^2, 1);
yy=reshape(dZy, nn^2, 1);
ss=[xx yy]';
S=reshape(ss, 2*nn^2, 1);
clear xx; clear yy; clear ss;
X=calcMatrix(nn, terms, dx);
[U, W, V]=svd(X, 0);
clear X;
W=pinv(W);
A=V*W*U'*S;
clear U; clear W; clear V; clear S;
pack;
% This function calculate the matrix for Zernike derivatives
function Z=calcMatrix(nn, terms, dx);
R=(nn-1)*dx/2;
[X, Y]=meshgrid(-R:dx:R);
r=sqrt(X.^2+Y.^2);
Z=zeros(2*nn^2, terms);
for i=1:terms
z=zeros(1, terms+1);
z(i+1)=1;
S=ZernikePolynomials(1, terms, nn-1, nn-1, Z);
[dZx, dZy]=gradient(S, dx);
dZx(r>R)=0;
dZy(r>R)=0;
xx=reshape(dZx, nn^2, 1);
yy=reshape(dZy, nn^2, 1);
ss=[xx yy]';
Z(:, i)=reshape(ss, 2*nn^2, 1);
end
% This function calculates the wavefront based on a set of Zernike co-efficients Z
% to get frame size (nn+1)*(nn+1).
function S=ZernikePolynomials(nn, z);
terms=length(z)-1;
[X, Y]=meshgrid(-1:2/nn:1);
r= sqrt(X.^2+Y.^2);
r(X.^2+Y.^2>1)=0;
Theta=atan2(Y, X);
S=zeros(nn+1);
for i=0:terms
[n, m]=single2doubleZ(i);
if (m==0)
pa=sqrt(n+1);
else
pa=sqrt(2*(n+1));
end
coef=pa;
Surf=zeros(nn+1);
for s=0:(n-abs(m))/2
c1=n-s;
c2=(n+m)/2-s;
c3=(n-m)/2-s;
Surf=Surf+(-1)^s*factorial(c1)/factorial(s)../factorial(c2)/factorial(c3)*power(r, n-2*s);
end
if (m<0)
Surf=Surf.*sin(abs(m)*Theta);
else if(m>0)
Surf=Surf.*cos(m*Theta);
end
S=S+z(i+1)*coef*Surf;
end
S(r>1)=0;
% This function converts single to double index in Zernike polynomials
function [n, m]=single2doubleZ(jj);
n=floor(sqrt(2*jj+1)+0.5-1;
m=2*jj-n*(n+2);
```

## 3 Comments

the cyclist on 5 Feb 2013

[I've edited your code for readability. Please do look at the link that José-Luis provided.]

You have provided a lot of code, which is fine. Could you please give us some guidance as to what error or warning you are getting, or what the problem is?

Jan Simon on 5 Feb 2013

@Pranjal Pathak: Please explain what kind of help you need. "Helping to run the code" could mean, that there is an error message or that you do not have a computer with installed Matlab...

## Products

No products are associated with this question.

Contact us