This Python script is used to calculate the speed of sound at room temperature using different frequencies emitted by a speaker. The script also calculates the percentage error of the measured values with respect to the theoretical value.
This script requires the following Python libraries:
- numpy
- matplotlib
To run the script, follow these steps:
- Ensure that you have Python installed on your machine. You can download Python from the official website.
- Install the required dependencies using pip:
pip install numpy matplotlib
-
Navigate to the directory containing the
soundspeed.py
script in your terminal. -
Run the script using the following command:
python soundspeed.py
- The script first defines the frequencies of the sound emitted by the speaker and the theoretical speed of sound in air at room temperature.
- It then calculates the speed of sound for each frequency by multiplying the frequency with the corresponding wavelength. This is based on the formula
$$v = f \lambda$$ where$$v$$ is the speed of sound,$$f$$ is the frequency, and$$\lambda$$ is the wavelength. - The script calculates the percentage error of the measured speed of sound with respect to the theoretical value. This is based on the formula
$$\text{Error %} = \left|\frac{v_{\text{measured}} - v_{\text{theoretical}}}{v_{\text{theoretical}}}\right| \times 100$$ . - The results for each frequency, including the wavelength, the measured speed of sound, and the percentage error, are printed out.
- Finally, the script plots a graph of wavelength versus frequency.
If you want to use this script for your own experiment, you may need to modify the following parts of the script:
frequencies
: Replace the array with the frequencies of the sound emitted by your speaker.wavelengths
: Replace the array with the wavelengths corresponding to your frequencies.v_s_theoretical
: Replace the value with the theoretical speed of sound in your medium (if not air at room temperature).
After modifying these values, you can run the script to calculate the speed of sound and the percentage error for your own experiment.