Here's the HTML and JavaScript code for an interactive web app that depicts standing waves on strings. The app includes four sliders labeled L, f, T, and μ (mu). When the frequency f is within 2% of the value calculated by the formula ( f_0 = \frac{1}{2L}\sqrt{\frac{T}{\mu}} ), the app draws a large-amplitude sine wave with a wavelength of ( 2L ).
You can copy the code below into a single HTML file and open it in a web browser to see the interactive app in action.
<!DOCTYPE html>
<html>
<head>
<title>Standing Waves on a String</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
}
#controls {
margin: 20px auto;
width: 80%;
max-width: 600px;
}
.slider-container {
margin: 15px 0;
}
label {
display: block;
font-weight: bold;
margin-bottom: 5px;
}
input[type="range"] {
width: 100%;
}
canvas {
border: 1px solid black;
margin-top: 20px;
}
</style>
</head>
<body>
<h1>Standing Waves on a String</h1>
<div id="controls">
<div class="slider-container">
<label for="lengthSlider">Length (L): <span id="lengthValue">1.00</span> m</label>
<input type="range" id="lengthSlider" min="0.5" max="5" value="1" step="0.01">
</div>
<div class="slider-container">
<label for="frequencySlider">Frequency (f): <span id="frequencyValue">1.00</span> Hz</label>
<input type="range" id="frequencySlider" min="0.1" max="10" value="1" step="0.01">
</div>
<div class="slider-container">
<label for="tensionSlider">Tension (T): <span id="tensionValue">1.00</span> N</label>
<input type="range" id="tensionSlider" min="0.1" max="10" value="1" step="0.01">
</div>
<div class="slider-container">
<label for="muSlider">Mass per unit length (μ): <span id="muValue">0.10</span> kg/m</label>
<input type="range" id="muSlider" min="0.01" max="1" value="0.1" step="0.01">
</div>
</div>
<canvas id="waveCanvas" width="600" height="300"></canvas>
<script>
// Get references to sliders and display elements
const lengthSlider = document.getElementById('lengthSlider');
const frequencySlider = document.getElementById('frequencySlider');
const tensionSlider = document.getElementById('tensionSlider');
const muSlider = document.getElementById('muSlider');
const lengthValue = document.getElementById('lengthValue');
const frequencyValue = document.getElementById('frequencyValue');
const tensionValue = document.getElementById('tensionValue');
const muValue = document.getElementById('muValue');
const canvas = document.getElementById('waveCanvas');
const ctx = canvas.getContext('2d');
// Initialize variables
let L = parseFloat(lengthSlider.value);
let f = parseFloat(frequencySlider.value);
let T = parseFloat(tensionSlider.value);
let mu = parseFloat(muSlider.value);
// Update displayed slider values
function updateValues() {
lengthValue.textContent = L.toFixed(2);
frequencyValue.textContent = f.toFixed(2);
tensionValue.textContent = T.toFixed(2);
muValue.textContent = mu.toFixed(2);
}
// Function to draw the wave
function drawWave() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Calculate the theoretical frequency
const f0 = (1 / (2 * L)) * Math.sqrt(T / mu);
// Allowed frequency range (±2%)
const f_min = f0 * 0.98;
const f_max = f0 * 1.02;
// Determine amplitude based on frequency proximity to f0
let amplitude;
if (f >= f_min && f <= f_max) {
amplitude = 50; // Large amplitude
} else {
amplitude = 10; // Small amplitude
}
// Set up wave parameters
const wavelength = 2 * L;
const k = (2 * Math.PI) / wavelength; // Wave number
const dx = canvas.width / 1000; // Increment for drawing
const x_scale = canvas.width / (2 * L); // Scale x to canvas width
const y_center = canvas.height / 2;
// Draw the wave
ctx.beginPath();
ctx.lineWidth = 2;
ctx.strokeStyle = '#0077be';
for (let x = 0; x <= canvas.width; x += dx) {
const x_physical = x / x_scale;
const y = amplitude * Math.sin(k * x_physical);
if (x === 0) {
ctx.moveTo(x, y_center - y);
} else {
ctx.lineTo(x, y_center - y);
}
}
ctx.stroke();
// Draw the equilibrium line
ctx.beginPath();
ctx.lineWidth = 1;
ctx.strokeStyle = '#555';
ctx.moveTo(0, y_center);
ctx.lineTo(canvas.width, y_center);
ctx.stroke();
}
// Update function to refresh values and redraw wave
function update() {
L = parseFloat(lengthSlider.value);
f = parseFloat(frequencySlider.value);
T = parseFloat(tensionSlider.value);
mu = parseFloat(muSlider.value);
updateValues();
drawWave();
}
// Add event listeners to sliders
lengthSlider.addEventListener('input', update);
frequencySlider.addEventListener('input', update);
tensionSlider.addEventListener('input', update);
muSlider.addEventListener('input', update);
// Initial update to set default values
update();
</script>
</body>
</html>
How the App Works
- Sliders: Adjust the four sliders to change the values of Length (L), Frequency (f), Tension (T), and Mass per unit length (μ). The current values are displayed next to each slider.
- Standing Wave Condition: The app calculates the theoretical frequency ( f_0 ) using the formula:
[
f_0 = \frac{1}{2L}\sqrt{\frac{T}{\mu}}
]
- Amplitude Change: If the chosen frequency f is within ±2% of ( f_0 ), a large-amplitude standing wave is displayed. Otherwise, a small-amplitude wave is shown.
- Wave Drawing: The wave is drawn on an HTML5 canvas element using JavaScript's Canvas API. The wave's shape updates in real-time as you adjust the sliders.
Features
- Interactive Controls: Real-time updating of the wave as you adjust the parameters.
- Dynamic Display: Visual representation of how changing physical parameters affects standing wave formation.
- Responsive Scaling: The wave scales appropriately to fit the canvas dimensions based on the length L.
Visualization Details
- The x-axis represents the length of the string from 0 to ( 2L ).
- The y-axis represents the displacement of the string at each point along its length.
- The equilibrium position of the string (no displacement) is indicated by a horizontal line in the center of the canvas.
- The wave displayed is a sine wave representing the fundamental mode of vibration with a wavelength of ( 2L ).
Customize and Enhance
Feel free to modify the code to enhance the visualization:
- Adjust Amplitudes: Change the values of
amplitude for both the large and small amplitudes to better suit your display.
- Add Higher Modes: Extend the app to display higher harmonics by adjusting the wavelength and frequency relationship.
- Include Damping: Implement damping effects by decreasing the amplitude over time or distance.
- Visual Improvements: Customize colors, add grid lines, or include animation to show the wave oscillating over time.
Note: Ensure that your web browser supports HTML5 and the Canvas API to properly display and interact with the app.