Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- #include <cmath>
- #include <cstdlib>
- #include <string>
- #include <vector>
- using namespace std;
- //////////////////////////////////////////////////////////////////////////////
- float translation()
- {
- float distance;
- cout << "Enter the distance traveled by the light (in centimeters): \n";
- cin >> distance;
- while(distance <=0 )
- {
- cout << "This is invalid input. Enter the distance (in centimeters):\n";
- cin >> distance;
- }
- return(distance);
- }
- //////////////////////////////////////////////////////////////////////////////
- float planar_refraction()
- {
- float prvalue;
- float index_refraction;
- float index_refraction_prime;
- cout << "Enter the index of refraction (n) for the medium the light is in before refraction:\n";
- cin >> index_refraction;
- cout << "\n";
- cout << "Enter the index of refraction (n') for the medium the light will be in after the refraction:\n";
- cin >> index_refraction_prime;
- prvalue = (index_refraction_prime / index_refraction);
- return (prvalue);
- }
- //////////////////////////////////////////////////////////////////////////////
- float spherical_refraction()
- {
- float index_refraction;
- float index_refraction_prime;
- float radius_converging;
- float radius_diverging;
- float srvalue;
- int lens_shape;
- cout << "Enter the index of refraction (n) for the medium the light is in before refraction:\n";
- cin >> index_refraction;
- cout << "\n";
- cout << "Enter the index of refraction (n') for the medium the light will be in after the refraction:\n";
- cin >> index_refraction_prime;
- cout << "Enter in a '1' for a converging lens and a '2' for a diverging lens:\n";
- cin >> lens_shape;
- while (lens_shape != 1 && lens_shape != 2)
- {
- cout << "Invalid input. Enter a '1' or '2':\n";
- cin >> lens_shape;
- }
- if (lens_shape == 1)
- {
- cout << "What is the radius of curvature for this converging lens? Only enter in the magnitude:\n";
- cin >> radius_converging;
- while (radius_converging <= 0)
- {
- cout << "You have entered invalid input. Enter a value greater than 0:\n";
- cin >> radius_converging;
- }
- srvalue = ((index_refraction - index_refraction_prime ) / (radius_converging * index_refraction_prime));
- }
- else
- {
- cout << "What is the radius of curvature for this diverging lens? Only enter in the magnitude:\n";
- cin >> radius_diverging;
- while (radius_diverging <= 0)
- {
- cout << "You have entered invalid input. Enter a value greater than 0:\n";
- cin >> radius_converging;
- }
- radius_diverging = -(radius_diverging);
- srvalue = ((index_refraction - index_refraction_prime ) / (radius_diverging * index_refraction_prime));
- }
- return (srvalue);
- }
- //////////////////////////////////////////////////////////////////////////////
- float thin_lens()
- {
- int thinlens_shape;
- float focal_length;
- float tlvalue;
- cout << "Is the thin lens converging or diverging? Enter '1' for convering or '2' for diverging:\n";
- cin >> thinlens_shape;
- if (thinlens_shape == 1)
- {
- cout << "What is the focal length for this converging lens? Only enter in the magnitude:\n";
- cin >> focal_length;
- }
- else
- {
- cout << "What is the focal length for this diverging lens? Only enter the magnitude:\n";
- cin >> focal_length;
- while (focal_length <= 0)
- {
- cout << "You have entered invalid input. Enter a value greater than 0:\n";
- cin >> focal_length;
- }
- focal_length = -(focal_length);
- tlvalue = -1 / (focal_length);
- }
- return tlvalue;
- }
- int main ()
- {
- int decision = 1;
- int number_elements;
- int counter = 0;
- int next_element;
- float tempArray [2][2] = //declaring arrays
- {
- {1, 0,},
- {0, 1,},
- };
- float tempArray1 [2][2] = //declaring arrays
- {
- {0, 0,},
- {0, 0,},
- };
- float product [2][2] =
- {
- {0, 0},
- {0, 0,},
- };
- //////////////////////////////////////////////////////////////////////////////
- while (decision == 1) //while loop to keep program running to enter in new elements
- {
- cout << "This is a program that will perform matrix computations\n";
- cout << "to output a system matrix, provided enough inputs are given.\n";
- cout << endl;
- cout << "How many elements does this system have? Note that a thick lens counts as three elements:\n";
- cin >> number_elements;
- for (counter = 0; counter < number_elements; counter++)
- {
- cout << endl;
- cout << endl;
- cout << endl;
- cout << "Enter in the next element. \n";
- cout << "A translation through air or another medium is given by entering '1'.\n";
- cout << "When light is refracted through a planar element, enter '2'.\n";
- cout << "When light is refracted through a spherical element, enter '3'.\n";
- cout << "When light travels through a thin lens, enter '4'.\n";
- cin >> next_element; //new elemnt
- cout << "\n";
- while((next_element != 1) && (next_element != 2) && (next_element != 3) && (next_element != 4))
- {
- cout << "Invalid input. Only enter '1', '2', '3', or '4':\n";
- cin >> next_element;
- }
- if(counter = 0) //this is for the first matrix calculation, so the element isn't multiplied by zero
- {
- if (next_element == 1) //for translation
- {
- float distance = translation();
- tempArray[0][1] = distance;
- product[0][0] = tempArray[0][0];
- product[0][1] = tempArray[0][1];
- product[1][0] = tempArray[1][0];
- product[1][1] = tempArray[1][1];
- }
- if (next_element == 2) //for planar refraction
- {
- float plvalue = planar_refraction();
- tempArray[1][1] = plvalue;
- product[0][0] = tempArray[0][0];
- product[0][1] = tempArray[0][1];
- product[1][0] = tempArray[1][0];
- product[1][1] = tempArray[1][1];
- //finalsystem = tempArray * finalsystem;
- }
- if (next_element == 3)
- {
- float index_refraction1;
- float index_refraction_prime1;
- cout << "Enter the index of refraction (n) for the medium the light is in before refraction:\n";
- cin >> index_refraction1;
- cout << "\n";
- cout << "Enter the index of refraction (n') for the medium the light will be in after the refraction:\n";
- cin >> index_refraction_prime1;
- float srvalue = spherical_refraction();
- tempArray[1][0] = srvalue;
- tempArray[1][1]= (index_refraction1/index_refraction_prime1);
- product[0][0] = tempArray[0][0];
- product[0][1] = tempArray[0][1];
- product[1][0] = tempArray[1][0];
- product[1][1] = tempArray[1][1];
- }
- if (next_element == 4)
- {
- thin_lens();
- float tlvalue = thin_lens();
- tlvalue = tempArray [1][0];
- product[0][0] = tempArray[0][0];
- product[0][1] = tempArray[0][1];
- product[1][0] = tempArray[1][0];
- product[1][1] = tempArray[1][1];
- //finalsystem = tempArray * finalsystem;
- }
- }
- else //this is the else statement where all matrix multiplies but the first go through
- {
- if (next_element == 1) //for translation
- {
- float distance = translation();
- tempArray[0][1] = distance;
- for (int row = 0; row < 3; row++) {
- for (int col = 0; col < 3; col++) {
- // Multiply the row of A by the column of B to get the row, column of product.
- for (int inner = 0; inner < 2; inner++) {
- tempArray1[0][0] = product[0][0];
- tempArray1[0][1] = product[0][1];
- tempArray1[1][0] = product[1][0];
- tempArray1[1][1] = product[1][1];
- product[row][col] += tempArray[row][inner] * tempArray1[inner][col];
- }
- }
- }
- }
- if (next_element == 2) //for planar refraction
- {
- float plvalue = planar_refraction();
- tempArray[1][1] = plvalue;
- for (int row = 0; row < 3; row++) {
- for (int col = 0; col < 3; col++) {
- // Multiply the row of A by the column of B to get the row, column of product.
- for (int inner = 0; inner < 2; inner++) {
- tempArray1[0][0] = product[0][0];
- tempArray1[0][1] = product[0][1];
- tempArray1[1][0] = product[1][0];
- tempArray1[1][1] = product[1][1];
- product[row][col] += tempArray[row][inner] * tempArray1[inner][col];
- }
- }
- }
- //finalsystem = tempArray * finalsystem;
- }
- if (next_element == 3)
- {
- float index_refraction1;
- float index_refraction_prime1;
- cout << "Enter the index of refraction (n) for the medium the light is in before refraction:\n";
- cin >> index_refraction1;
- cout << "\n";
- cout << "Enter the index of refraction (n') for the medium the light will be in after the refraction:\n";
- cin >> index_refraction_prime1;
- float srvalue = spherical_refraction();
- tempArray[1][0] = srvalue;
- tempArray[1][1]= (index_refraction1/index_refraction_prime1);
- for (int row = 0; row < 3; row++) {
- for (int col = 0; col < 3; col++) {
- // Multiply the row of A by the column of B to get the row, column of product.
- for (int inner = 0; inner < 2; inner++) {
- tempArray1[0][0] = product[0][0];
- tempArray1[0][1] = product[0][1];
- tempArray1[1][0] = product[1][0];
- tempArray1[1][1] = product[1][1];
- product[row][col] += tempArray[row][inner] * tempArray1[inner][col];
- }
- }
- }
- }
- if (next_element == 4)
- {
- thin_lens();
- float tlvalue = thin_lens();
- tlvalue = tempArray [1][0];
- for (int row = 0; row < 3; row++) {
- for (int col = 0; col < 3; col++) {
- // Multiply the row of A by the column of B to get the row, column of product.
- for (int inner = 0; inner < 2; inner++) {
- tempArray1[0][0] = product[0][0];
- tempArray1[0][1] = product[0][1];
- tempArray1[1][0] = product[1][0];
- tempArray1[1][1] = product[1][1];
- product[row][col] += tempArray[row][inner] * tempArray1[inner][col];
- }
- }
- }
- }
- }
- } //end for loop
- }
- cout << product;
- cout << "\n";
- cout << "\n";
- cout << "\n";
- cout << "\n";
- cout << "Do you wish to analyze another optical system? Enter '1' for 'yes' or '2' for 'no': \n";
- cin >> decision;
- if (decision != 1 && decision != 2)
- {
- cout << "This is invalid input. Enter a 1 for 'yes' or a 2 for 'no':\n";
- cin >> decision;
- }
- }//end int main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement