#!/usr/bin/perl
# 
# compute stuff that will help us to explain where bright spots appear in
#    a diffraction pattern
#
# MWR 5/5/2022

$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;
# an auxiliary quantity, which we'll use for plotting
$Q = $lambda / $a; 




# "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;

$start_y = 1.4300;
$end_y = 1.4600;
$d_y = 1.0E-6;

# 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;
  }

  # compute an auxiliary quantity, which we can use for plotting
  #   what is this angular distance from center, in terms of Q?
  $Q_angle = $theta / $Q;

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

}

exit 0;

  
  
  






