Guest

Tim Waters

By: a guest on Jul 27th, 2009  |  syntax: None  |  size: 2.21 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. clear all; %clears all variables
  2. close all; %closes all figures
  3.  
  4. eps = .0001 %can also customize; input('Enter the tick-size (epsilon) ');
  5. n1 = 1 %input('Enter the index of refraction in region containing A ');
  6. n2 = 1.5 %input('Enter the index of refraction in region containing B ');
  7. mirror_axis = 0:eps:1; %cut (0,1) into grid points
  8. c = mirror_axis'; %creates a column vector
  9. a = ones(length(c),1)*rand; %ones(rows,columns) returns a matrix of 1s
  10. b = ones(length(c),1)*rand; %rand gives a random scalar in (0,1)
  11.  
  12. %Compute the optical path length, opl
  13. d_ac = sqrt(a.^2 + c.^2);
  14. d_cb = sqrt((1-c).^2 + b.^2);
  15. opl_ac=n1*d_ac;
  16. opl_cb=n2*d_cb;
  17. opl = opl_ac + opl_cb;
  18.  
  19. opl_min = min(opl);
  20. while opl(i.html">i)~= opl_min
  21.     i.html">i = i.html">i+1;
  22. end
  23. % i now contains the location of opl_min in the vector opl
  24. %************************plot results *************************************
  25. m = a(1)+b(1); %slope of straiht line path connecting a to -b
  26. plot(c,zeros(size(c)),'r:') %plots interface axis y=0
  27.  
  28. hold on; %allows you to add more things to the plot
  29.  
  30. y_ab = -m*c + a(1);
  31. plot(c,y_ab) %plots straight line path
  32.  
  33. m_a = a(1)/c(i.html">i);
  34. y_ac = -m_a*c(1:i.html">i)+ a(1);
  35. plot(c(1:i.html">i),y_ac) %plots computed shortest path from A to C
  36.  
  37. m_b = b(1)/(1-c(i.html">i));
  38. y_cb = -m_b*c(i.html">i:length(c)) + m_b*c(i.html">i);
  39. plot(c(i.html">i:length(c)),y_cb) %plots computed shortest path from A to C
  40.  
  41. text(0,a(1),'Point A'); %labels via text(x,y,'string')
  42. text(1,-b(1),'Point -B');
  43. plot(c(i.html">i),0,'kx') %plots point c with a black x over it
  44. text(c(i.html">i),0,sprintf('Point C = %0.5g ',c(i.html">i)));
  45.  
  46. d_ab = sqrt(1+(b(1)+a(1))^2); %dist between pts A and -B
  47. i_ab=floor((a(1)/m)/eps); %location of straight line path
  48. title({'Straight Line Path and Least Time Path for n1/n2 interface';
  49.    sprintf('Least time path dist = %0.5g ',(d_ac(i.html">i) + d_cb(i.html">i)));
  50.   sprintf('Straight line path dist = %0.5g ',d_ab);
  51.   sprintf('Least time path opl = %0.5g ',opl_min);
  52.   sprintf('Straight line path opl = %0.5g ',opl(i_ab))})
  53.  
  54. %Check Answer with Snell's Law
  55. n1/n2
  56. c_ratio=(1-c(i.html">i))/c(i.html">i);
  57. answr = c_ratio*d_ac(i.html">i)/d_cb(i.html">i);
  58. xlabel({'Snells Law says n_i/n_f = (1-c)d_{ac}/(c*d_{cb})';
  59.     sprintf('Thus compare n_i/n_f = %0.5g ',n1/n2);
  60.     sprintf('with (1-c)d_ac/(c*d_cb) = %0.5g ',answr)});
  61. hold off