fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define PI 3.141592653589793
  5. #define I0 1.0 // Max intensity
  6. #define SLIT_WIDTH 0.001 // Slit width in meters
  7. #define WAVELENGTH 500e-9 // Wavelength in meters (500 nm)
  8.  
  9. void write_data_to_file(const char *filename) {
  10. FILE *fp = fopen(filename, "w");
  11. if (!fp) {
  12. perror("Failed to open file");
  13. return;
  14. }
  15.  
  16. for (double theta = -0.1; theta <= 0.1; theta += 0.001) {
  17. double beta = (PI * SLIT_WIDTH * sin(theta)) / WAVELENGTH;
  18. double intensity = I0 * pow(sin(beta) / beta, 2);
  19. fprintf(fp, "%f %f\n", beta, intensity);
  20. }
  21.  
  22. fclose(fp);
  23. }
  24.  
  25. int main() {
  26. const char *filename = "intensity_vs_beta.txt";
  27. write_data_to_file(filename);
  28.  
  29. // Plotting using GNUplot
  30. FILE *gnuplotPipe = popen("gnuplot -persistent", "w");
  31. if (gnuplotPipe) {
  32. fprintf(gnuplotPipe, "set title 'Single Slit Diffraction: Intensity vs. Beta'\n");
  33. fprintf(gnuplotPipe, "set xlabel 'Beta'\n");
  34. fprintf(gnuplotPipe, "set ylabel 'Intensity'\n");
  35. fprintf(gnuplotPipe, "plot '%s' using 1:2 with lines title 'Intensity'\n", filename);
  36. pclose(gnuplotPipe);
  37. } else {
  38. fprintf(stderr, "Could not open GNUplot.\n");
  39. }
  40.  
  41. return 0;
  42. }
  43.  
Success #stdin #stdout #stderr 0.01s 5288KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Failed to open file: Permission denied
sh: 1: gnuplot: not found