1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
#include <iostream> #include <cmath> #include <vector> #include <iomanip> // Constants const double PI = 3.141592653589793; const double WAVELENGTH = 500e-9; // Wavelength of light in meters (e.g., 500 nm) const double SLIT_WIDTH = 1e-6; // Width of the slit in meters (e.g., 1 micron) const double SCREEN_DISTANCE = 1.0; // Distance from slit to screen in meters // Function to calculate the intensity at a point on the screen double calculateIntensity(double y) { double beta = (PI * SLIT_WIDTH * y) / (WAVELENGTH * SCREEN_DISTANCE); if (beta == 0) { return 1.0; // Maximum intensity at the center } else { return pow(sin(beta) / beta, 2); } } // Function to simulate and display the diffraction pattern void simulateDiffractionPattern(int points, double screenSize) { std::vector<double> intensity(points); double step = screenSize / points; double maxIntensity = 0.0; for (int i = 0; i < points; ++i) { double y = -screenSize / 2 + i * step; intensity[i] = calculateIntensity(y); if (intensity[i] > maxIntensity) { maxIntensity = intensity[i]; } } // Display the diffraction pattern std::cout << "Simulated Diffraction Pattern:\n"; for (int i = 0; i < points; ++i) { int barLength = static_cast<int>((intensity[i] / maxIntensity) * 50); std::cout << std::setw(3) << i << " | "; for (int j = 0; j < barLength; ++j) { std::cout << "*"; } std::cout << "\n"; } } int main() { int points; double screenSize; std::cout << "Enter the number of points to simulate: "; std::cin >> points; std::cout << "Enter the screen size in meters: "; std::cin >> screenSize; simulateDiffractionPattern(points, screenSize); return 0; } |
Explanation
- Constants:
- The program defines constants like the wavelength of light (
WAVELENGTH
), slit width (SLIT_WIDTH
), and the distance from the slit to the screen (SCREEN_DISTANCE
). These values are set in meters, representing typical values for visible light and slit experiments.
- The program defines constants like the wavelength of light (
- calculateIntensity Function:
- This function returns the relative intensity, which is normalized by the maximum intensity.
- simulateDiffractionPattern Function:
- This function simulates the diffraction pattern by calculating the intensity at various points across the screen.
- The intensity is stored in a vector, and the maximum intensity is determined to normalize the display.
- The function then visualizes the pattern as a bar graph, where each bar’s length corresponds to the calculated intensity at that point.
- Main Function:
- The main function prompts the user to input the number of points to simulate and the screen size.
- It then calls
simulateDiffractionPattern
to generate and display the diffraction pattern.
Usage
- Screen Size: The screen size represents the width of the screen where the diffraction pattern will be observed.
- Points: The number of points represents the resolution of the simulation. More points give a finer pattern.