Advertisement
Guest User

Untitled

a guest
May 2nd, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. #include <cmath>
  5. #include <cstdlib>
  6. #include <string>
  7. #include <vector>
  8.  
  9.  
  10. using namespace std;
  11.  
  12. //////////////////////////////////////////////////////////////////////////////
  13. float translation()
  14.     {
  15.         float distance;
  16.  
  17.         cout << "Enter the distance traveled by the light (in centimeters): \n";
  18.         cin >> distance;
  19.  
  20.         while(distance <=0 )
  21.         {
  22.             cout << "This is invalid input. Enter the distance (in centimeters):\n";
  23.             cin >> distance;
  24.         }
  25.         return(distance);
  26.     }
  27.  
  28.  
  29.  
  30.  
  31.     //////////////////////////////////////////////////////////////////////////////
  32. float planar_refraction()
  33.     {
  34.  
  35.         float prvalue;
  36.         float index_refraction;
  37.         float index_refraction_prime;
  38.  
  39.         cout << "Enter the index of refraction (n) for the medium the light is in before refraction:\n";
  40.         cin >> index_refraction;
  41.         cout << "\n";
  42.         cout << "Enter the index of refraction (n') for the medium the light will be in after the refraction:\n";
  43.         cin >> index_refraction_prime;
  44.  
  45.         prvalue = (index_refraction_prime / index_refraction);
  46.  
  47.         return (prvalue);
  48.     }
  49.  
  50.  
  51.  
  52.     //////////////////////////////////////////////////////////////////////////////
  53. float spherical_refraction()
  54.     {
  55.         float index_refraction;
  56.         float index_refraction_prime;
  57.         float radius_converging;
  58.         float radius_diverging;
  59.         float srvalue;
  60.         int lens_shape;
  61.  
  62.         cout << "Enter the index of refraction (n) for the medium the light is in before refraction:\n";
  63.         cin >> index_refraction;
  64.         cout << "\n";
  65.         cout << "Enter the index of refraction (n') for the medium the light will be in after the refraction:\n";
  66.         cin >> index_refraction_prime;
  67.  
  68.         cout << "Enter in a '1' for a converging lens and a '2' for a diverging lens:\n";
  69.         cin >> lens_shape;
  70.  
  71.         while (lens_shape != 1 && lens_shape != 2)
  72.         {
  73.             cout << "Invalid input. Enter a '1' or '2':\n";
  74.             cin >> lens_shape;
  75.         }
  76.  
  77.         if (lens_shape == 1)
  78.         {
  79.             cout << "What is the radius of curvature for this converging lens? Only enter in the magnitude:\n";
  80.             cin >>  radius_converging;
  81.            
  82.  
  83.             while (radius_converging <= 0)
  84.             {
  85.                 cout << "You have entered invalid input. Enter a value greater than 0:\n";
  86.                 cin >> radius_converging;
  87.             }
  88.  
  89.             srvalue = ((index_refraction - index_refraction_prime ) / (radius_converging * index_refraction_prime));
  90.  
  91.         }
  92.         else
  93.         {
  94.             cout << "What is the radius of curvature for this diverging lens? Only enter in the magnitude:\n";
  95.             cin >>  radius_diverging;
  96.            
  97.  
  98.             while (radius_diverging <= 0)
  99.             {
  100.                 cout << "You have entered invalid input. Enter a value greater than 0:\n";
  101.                 cin >> radius_converging;
  102.             }
  103.  
  104.             radius_diverging = -(radius_diverging);
  105.  
  106.             srvalue = ((index_refraction - index_refraction_prime ) / (radius_diverging * index_refraction_prime));
  107.  
  108.         }
  109.  
  110.         return (srvalue);
  111.     }
  112.  
  113.  
  114.  
  115.  
  116.     //////////////////////////////////////////////////////////////////////////////
  117. float thin_lens()
  118.     {
  119.         int thinlens_shape;
  120.         float focal_length;
  121.         float tlvalue;
  122.  
  123.         cout << "Is the thin lens converging or diverging? Enter '1' for convering or '2' for diverging:\n";
  124.         cin >> thinlens_shape;
  125.  
  126.         if (thinlens_shape == 1)
  127.         {
  128.             cout << "What is the focal length for this converging lens? Only enter in the magnitude:\n";
  129.             cin >>  focal_length;
  130.  
  131.         }
  132.  
  133.         else
  134.         {
  135.             cout << "What is the focal length for this diverging lens? Only enter the magnitude:\n";
  136.             cin >> focal_length;
  137.  
  138.             while (focal_length <= 0)
  139.             {
  140.                 cout << "You have entered invalid input. Enter a value greater than 0:\n";
  141.                 cin >> focal_length;
  142.             }
  143.  
  144.             focal_length = -(focal_length);
  145.  
  146.             tlvalue = -1 / (focal_length);
  147.         }
  148.  
  149.         return tlvalue;
  150.     }
  151.  
  152.  
  153.    
  154. int main ()
  155. {
  156.  
  157. int decision = 1;
  158. int number_elements;
  159. int counter = 0;
  160.  
  161. int next_element;
  162.  
  163.  
  164. float tempArray [2][2] = //declaring arrays
  165. {
  166.     {1, 0,},
  167.     {0, 1,},
  168. };
  169. float tempArray1 [2][2] = //declaring arrays
  170. {
  171.     {0, 0,},
  172.     {0, 0,},
  173. };
  174.  
  175. float product [2][2] =
  176. {
  177.     {0, 0},
  178.     {0, 0,},
  179. };
  180.  
  181.    
  182.  
  183.  
  184.  
  185.    
  186.         //////////////////////////////////////////////////////////////////////////////
  187.  
  188. while (decision == 1) //while loop to keep program running to enter in new elements
  189.     {
  190.  
  191.  
  192.     cout << "This is a program that will perform matrix computations\n";
  193.     cout << "to output a system matrix, provided enough inputs are given.\n";
  194.     cout << endl;
  195.     cout << "How many elements does this system have? Note that a thick lens counts as three elements:\n";
  196.     cin >> number_elements;
  197.  
  198.     for (counter = 0; counter < number_elements; counter++)
  199.     {
  200.     cout << endl;
  201.     cout << endl;
  202.     cout << endl;
  203.     cout << "Enter in the next element. \n";
  204.     cout << "A translation through air or another medium is given by entering '1'.\n";
  205.     cout << "When light is refracted through a planar element, enter '2'.\n";
  206.     cout << "When light is refracted through a spherical element, enter '3'.\n";
  207.     cout << "When light travels through a thin lens, enter '4'.\n";
  208.  
  209.  
  210.     cin >> next_element; //new elemnt
  211.     cout << "\n";
  212.  
  213.     while((next_element != 1) && (next_element != 2) && (next_element != 3) && (next_element != 4))
  214.     {
  215.         cout << "Invalid input. Only enter '1', '2', '3', or '4':\n";
  216.         cin >> next_element;
  217.     }
  218.  
  219.     if(counter = 0) //this is for the first matrix calculation, so the element isn't multiplied by zero
  220.     {
  221.  
  222.  
  223.     if (next_element == 1) //for translation
  224.     {
  225.  
  226.     float distance = translation();
  227.  
  228.         tempArray[0][1] = distance;
  229.  
  230.         product[0][0] = tempArray[0][0];
  231.         product[0][1] = tempArray[0][1];
  232.         product[1][0] = tempArray[1][0];
  233.         product[1][1] = tempArray[1][1];
  234.     }
  235.  
  236.     if (next_element == 2) //for planar refraction
  237.     {
  238.         float plvalue = planar_refraction();
  239.  
  240.         tempArray[1][1] = plvalue;
  241.  
  242.         product[0][0] = tempArray[0][0];
  243.         product[0][1] = tempArray[0][1];
  244.         product[1][0] = tempArray[1][0];
  245.         product[1][1] = tempArray[1][1];
  246.        
  247.  
  248.         //finalsystem = tempArray * finalsystem;
  249.     }
  250.  
  251.     if (next_element == 3)
  252.     {
  253.         float index_refraction1;
  254.         float index_refraction_prime1;
  255.  
  256.         cout << "Enter the index of refraction (n) for the medium the light is in before refraction:\n";
  257.         cin >> index_refraction1;
  258.         cout << "\n";
  259.         cout << "Enter the index of refraction (n') for the medium the light will be in after the refraction:\n";
  260.         cin >> index_refraction_prime1;
  261.            
  262.         float srvalue = spherical_refraction();
  263.        
  264.         tempArray[1][0] = srvalue;
  265.         tempArray[1][1]= (index_refraction1/index_refraction_prime1);
  266.  
  267.         product[0][0] = tempArray[0][0];
  268.         product[0][1] = tempArray[0][1];
  269.         product[1][0] = tempArray[1][0];
  270.         product[1][1] = tempArray[1][1];
  271.        
  272.  
  273.     }
  274.  
  275.     if (next_element == 4)
  276.     {
  277.         thin_lens();
  278.         float tlvalue = thin_lens();
  279.        
  280.         tlvalue = tempArray [1][0];
  281.  
  282.         product[0][0] = tempArray[0][0];
  283.         product[0][1] = tempArray[0][1];
  284.         product[1][0] = tempArray[1][0];
  285.         product[1][1] = tempArray[1][1];
  286.        
  287.         //finalsystem = tempArray * finalsystem;
  288.        
  289.     }
  290.     }
  291.     else //this is the else statement where all matrix multiplies but the first go through
  292.     {
  293.  
  294.     if (next_element == 1) //for translation
  295.     {
  296.     float distance = translation();
  297.  
  298.         tempArray[0][1] = distance;
  299.  
  300.         for (int row = 0; row < 3; row++) {
  301.             for (int col = 0; col < 3; col++) {
  302.             // Multiply the row of A by the column of B to get the row, column of product.
  303.                 for (int inner = 0; inner < 2; inner++) {
  304.                     tempArray1[0][0] = product[0][0];
  305.                     tempArray1[0][1] = product[0][1];
  306.                     tempArray1[1][0] = product[1][0];
  307.                     tempArray1[1][1] = product[1][1];
  308.                    
  309.                 product[row][col] += tempArray[row][inner] * tempArray1[inner][col];
  310.                 }
  311.             }
  312.         }
  313.  
  314.     }
  315.  
  316.     if (next_element == 2) //for planar refraction
  317.     {
  318.         float plvalue = planar_refraction();
  319.  
  320.         tempArray[1][1] = plvalue;
  321.        
  322.         for (int row = 0; row < 3; row++) {
  323.             for (int col = 0; col < 3; col++) {
  324.             // Multiply the row of A by the column of B to get the row, column of product.
  325.                 for (int inner = 0; inner < 2; inner++) {
  326.                     tempArray1[0][0] = product[0][0];
  327.                     tempArray1[0][1] = product[0][1];
  328.                     tempArray1[1][0] = product[1][0];
  329.                     tempArray1[1][1] = product[1][1];
  330.                    
  331.                 product[row][col] += tempArray[row][inner] * tempArray1[inner][col];
  332.                 }
  333.             }
  334.         }
  335.  
  336.         //finalsystem = tempArray * finalsystem;
  337.     }
  338.  
  339.     if (next_element == 3)
  340.     {
  341.         float index_refraction1;
  342.         float index_refraction_prime1;
  343.  
  344.         cout << "Enter the index of refraction (n) for the medium the light is in before refraction:\n";
  345.         cin >> index_refraction1;
  346.         cout << "\n";
  347.         cout << "Enter the index of refraction (n') for the medium the light will be in after the refraction:\n";
  348.         cin >> index_refraction_prime1;
  349.            
  350.         float srvalue = spherical_refraction();
  351.        
  352.         tempArray[1][0] = srvalue;
  353.         tempArray[1][1]= (index_refraction1/index_refraction_prime1);
  354.  
  355.         for (int row = 0; row < 3; row++) {
  356.             for (int col = 0; col < 3; col++) {
  357.             // Multiply the row of A by the column of B to get the row, column of product.
  358.                 for (int inner = 0; inner < 2; inner++) {
  359.                     tempArray1[0][0] = product[0][0];
  360.                     tempArray1[0][1] = product[0][1];
  361.                     tempArray1[1][0] = product[1][0];
  362.                     tempArray1[1][1] = product[1][1];
  363.                    
  364.                 product[row][col] += tempArray[row][inner] * tempArray1[inner][col];
  365.                 }
  366.             }
  367.         }
  368.     }
  369.  
  370.     if (next_element == 4)
  371.     {
  372.         thin_lens();
  373.         float tlvalue = thin_lens();
  374.        
  375.         tlvalue = tempArray [1][0];
  376.        
  377.         for (int row = 0; row < 3; row++) {
  378.             for (int col = 0; col < 3; col++) {
  379.             // Multiply the row of A by the column of B to get the row, column of product.
  380.                 for (int inner = 0; inner < 2; inner++) {
  381.                     tempArray1[0][0] = product[0][0];
  382.                     tempArray1[0][1] = product[0][1];
  383.                     tempArray1[1][0] = product[1][0];
  384.                     tempArray1[1][1] = product[1][1];
  385.                    
  386.                 product[row][col] += tempArray[row][inner] * tempArray1[inner][col];
  387.                 }
  388.             }
  389.         }
  390.        
  391.     }
  392.     }
  393.     } //end for loop
  394.  
  395.    
  396.        
  397.     }
  398.    
  399.  
  400.     cout << product;
  401.     cout << "\n";
  402.     cout << "\n";
  403.     cout << "\n";
  404.     cout << "\n";
  405.     cout << "Do you wish to analyze another optical system? Enter '1' for 'yes' or '2' for 'no': \n";
  406.     cin >> decision;
  407.     if (decision != 1 && decision != 2)
  408.     {
  409.         cout << "This is invalid input. Enter a 1 for 'yes' or a 2 for 'no':\n";
  410.         cin >> decision;
  411.     }
  412.  
  413.     }//end int main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement