#!/usr/bin/perl
# 
# compute the amplitude of waves formed by a single-slit 
#    diffraction pattern
#
# MWR 4/23/2019

$debug = 1;

$mypi = 3.14159;

# width of slit (m)
$a = 0.00004;
# wavelength of light (m)
$lambda = 550.0E-9;
# distance to far wall (m)
$L = 10;



# "y" will be the distance away from mid-point of the far wall
#    in other words, how far to the side of center of pattern
#   
$start_y = 0.0;
$end_y = 5.5E-1;
$d_y = 1.0E-4;

# this will be the maximum value of the amplitude -- at center of pattern
$max_amp = -1;

# we can't actually start at zero, would cause divide-by-zero
for ($y = $start_y + $d_y; $y <= $end_y; $y += $d_y) {

  # compute the angle of this point from center of pattern
  #      as seen by the center of slit (radians)
  $theta = $y / $L;

#  $arg = (2.0*$mypi*$a*$y)/($lambda*$L);
 $arg = ($mypi*$a*$y)/($lambda*$L);

  $val = sin($arg);

  # do we need to multiply by this factor?
  if ($y != 0) {
#    $factor = ($lambda*$L)/(2.0*$mypi*$y);
    $factor = ($lambda*$L)/($mypi*$y);
  }
  else {
    $factor = 0;
  }
  $f_val = $factor*$val;

  $amp = $val*$val;
  $f_amp = $f_val*$f_val;

  # if this is the first iteration, set the maximum amplitude
  #   to the current value -- as this is (very close to) the center
  if ($max_amp == -1) {
    $max_amp = $f_amp;
  }
  

  printf " y %10.5f  theta %10.5e  val %10.5e  amp %10.5e  f_amp %10.5e  norm_inten  %10.5e \n",
           $y, $theta, $val, $amp, $f_amp, ($f_amp / $max_amp);

}

exit 0;

  
  
  






