Sure! Below is the HTML and JavaScript code for the interactive web app that depicts standing waves on strings with four sliders labeled L, f, T, and μ (mu). When the value of f is within 2% of the value of ( f_0 = \frac{1}{2L} \sqrt{\frac{T}{\mu}} ), a large-amplitude sine wave with a wavelength of ( 2L ) is drawn. When L changes, the width of the graph changes appropriately.
You can copy this code into an HTML file and open it in a web browser to see it in action.
<!DOCTYPE html>
<html>
<head>
<title>Standing Waves on a String</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
h1 {
text-align: center;
}
.controls {
margin-bottom: 20px;
}
.control-group {
margin: 10px 0;
}
label {
display: block;
font-weight: bold;
}
input[type=range] {
width: 100%;
}
.value-display {
font-weight: normal;
margin-left: 10px;
}
#canvas-container {
border: 1px solid #ccc;
overflow: auto;
}
</style>
</head>
<body>
<h1>Standing Waves on a String</h1>
<div class="controls">
<div class="control-group">
<label for="L">Length L (m): <span class="value-display" id="LValue">1.00</span></label>
<input type="range" id="L" name="L" min="0.5" max="2" step="0.01" value="1" oninput="updateValues()">
</div>
<div class="control-group">
<label for="f">Frequency f (Hz): <span class="value-display" id="fValue">100.00</span></label>
<input type="range" id="f" name="f" min="10" max="1000" step="1" value="100" oninput="updateValues()">
</div>
<div class="control-group">
<label for="T">Tension T (N): <span class="value-display" id="TValue">10.00</span></label>
<input type="range" id="T" name="T" min="1" max="100" step="1" value="10" oninput="updateValues()">
</div>
<div class="control-group">
<label for="mu">Linear density μ (kg/m): <span class="value-display" id="muValue">0.0100</span></label>
<input type="range" id="mu" name="mu" min="0.001" max="0.1" step="0.001" value="0.01" oninput="updateValues()">
</div>
</div>
<div id="canvas-container">
<canvas id="waveCanvas" width="300" height="200"></canvas>
</div>
<script>
function updateValues() {
var LSlider = document.getElementById("L");
var fSlider = document.getElementById("f");
var TSlider = document.getElementById("T");
var muSlider = document.getElementById("mu");
var LValue = document.getElementById("LValue");
var fValue = document.getElementById("fValue");
var TValue = document.getElementById("TValue");
var muValue = document.getElementById("muValue");
var L = parseFloat(LSlider.value);
var f = parseFloat(fSlider.value);
var T = parseFloat(TSlider.value);
var mu = parseFloat(muSlider.value);
LValue.innerText = L.toFixed(2);
fValue.innerText = f.toFixed(2);
TValue.innerText = T.toFixed(2);
muValue.innerText = mu.toFixed(4);
// Calculate fundamental frequency
var f0 = (1 / (2 * L)) * Math.sqrt(T / mu);
// Compute relative difference
var delta_f = Math.abs(f - f0) / f0;
// Adjust canvas size according to L
var canvas = document.getElementById("waveCanvas");
var pixelsPerMeter = 300; // Adjust this scaling factor as needed
canvas.width = L * pixelsPerMeter; // Canvas width in pixels
var ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw the wave if delta_f <= 0.02
if (delta_f <= 0.02) {
drawWave(ctx, L, canvas.width, canvas.height);
} else {
// Optionally draw nothing or a flat line
drawFlatLine(ctx, canvas.width, canvas.height);
}
}
function drawWave(ctx, L, width, height) {
// Draw a sine wave with amplitude A and wavelength λ = 2L
var A = height * 0.4; // Amplitude is 40% of canvas height
var lambda = 2 * L; // Wavelength is 2L
var pixelsPerMeter = width / L;
// Loop over x positions
ctx.beginPath();
for (var x = 0; x <= width; x++) {
var physicalX = x / pixelsPerMeter;
var y = A * Math.sin((2 * Math.PI / lambda) * physicalX);
if (x === 0) {
ctx.moveTo(x, height / 2 - y);
} else {
ctx.lineTo(x, height / 2 - y);
}
}
ctx.strokeStyle = "blue";
ctx.lineWidth = 2;
ctx.stroke();
}
function drawFlatLine(ctx, width, height) {
ctx.beginPath();
ctx.moveTo(0, height / 2);
ctx.lineTo(width, height / 2);
ctx.strokeStyle = "black";
ctx.lineWidth = 1;
ctx.stroke();
}
// Initialize the display
updateValues();
</script>
</body>
</html>
Explanation:
-
HTML Structure: The HTML code creates a simple page with four sliders (input[type="range"]) labeled L, f, T, and μ. Each slider is accompanied by a label that displays its current value.
-
Canvas for Drawing: A <canvas> element with the id waveCanvas is used to draw the wave. The canvas width is adjusted dynamically based on the value of L.
-
JavaScript Functions:
updateValues(): This function is called whenever any of the sliders change. It reads the slider values, updates the displayed values, calculates the fundamental frequency ( f_0 ), checks if the current frequency f is within 2% of ( f_0 ), adjusts the canvas size, and either draws the standing wave or a flat line.
drawWave(ctx, L, width, height): This function draws the standing wave as a sine wave with amplitude based on the canvas height and wavelength ( \lambda = 2L ).
drawFlatLine(ctx, width, height): This function draws a flat line across the canvas when the frequency is not within 2% of ( f_0 ).
-
Styling: Basic CSS is included to style the page and make the sliders and canvas look presentable.
-
Scaling: The variable pixelsPerMeter is used to scale the physical length of the string to pixel units for drawing on the canvas. The amplitude and wavelength are scaled accordingly.
How It Works:
- The user adjusts the sliders to change the values of L, f, T, and μ.
- The
updateValues() function calculates the fundamental frequency ( f_0 ) and determines if the current frequency f is within 2% of ( f_0 ).
- If f is within 2% of ( f_0 ), the
drawWave() function is called, and a standing wave is drawn on the canvas.
- If not, a flat line is drawn, indicating no standing wave.
- When L changes, the canvas width adjusts to ensure the wave is displayed over the correct length.
Note: You can adjust the pixelsPerMeter variable in the JavaScript code to change how the physical length scales to pixels on your screen.
Feel free to customize the code further to suit your specific needs or preferences.