title: Diffraction using fourier transform in Matlab
body: <p>I am very new to Matlab and am trying to implement the following fresnel diffraction using the fourier transform:</p>
  
  <p><a href="http://upload.wikimedia.org/math/3/a/0/3a0e2fa60aeba794c51ed9e3c429d524.png" rel="nofollow noreferrer" title="Equation from http://en.wikipedia.org/wiki/Fresnel_diffraction"><img src="https://i.stack.imgur.com/g4rGf.png" alt="Equation from http://en.wikipedia.org/wiki/Fresnel_diffraction"></a></p>
  
  <p>This is taken from the following wikipedia page: <a href="http://en.wikipedia.org/wiki/Fresnel_diffraction" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Fresnel_diffraction</a></p>
  
  <p>I am trying a square aperture of width 5 cm.</p>
  
  <pre><code>clc;clear;
  
  lambda=1*10^-6;
  w=.05;
  z=2.0;
  k=(2*pi)/lambda;
  
  x1=linspace(-0.2,0.2,2048);
  y1=linspace(-0.2,0.2,2048);
  
  U1=((abs(x1)&lt;=(w)/2))&amp;((abs(y1)&lt;=(w)/2));
  u1=double(U1);
  
  figure(1)
  plot(x1,u1)
  
  g=u1'*exp(1i*(pi/(lambda*z))*(x1.^2+y1.^2));
  G=fftshift(fft2(g));
  
  h=(exp(1i*k*z)/(1i*lambda*z))*exp(1i*(pi/(lambda*z))*(x1.^2+y1.^2));
  H=fftshift(fft2(h));
  
  u2=H*G;
  
  figure(2)
  plot(x1,abs(u2));
  </code></pre>
  
  <p>When I plot the field u2, for any of the distances, z, that I try, it just shows up like some random pattern and not the expected diffraction pattern for a square aperture. </p>
  
  <p>Like I said, I am very new to MATLAB and find it difficult to understand. I think I am making this more complicated than it needs to be, and am implementing the integrals incorrectly.</p>
  
  <p>Any pointers or advice? I am quite stuck...</p>
  
  <p>Thanks!</p>