-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathharmonic.html
More file actions
97 lines (96 loc) · 4.68 KB
/
Copy pathharmonic.html
File metadata and controls
97 lines (96 loc) · 4.68 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Harmonic Motion Calculator</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Simple Harmonic Motion Calculator</h1>
<label for="equationType">Choose what to calculate:</label>
<select id="equationType" onchange="updateInputs()">
<option value="force">Restoring Force (F = -kx)</option>
<option value="omega">Angular Frequency (ω = sqrt(k/m))</option>
<option value="period">Period (T = 2π√(m/k))</option>
</select>
<br><br>
<div id="forceInputs">
<h3>Mass (m):</h3>
<input type="number" id="massF" placeholder="Mass (kg)">
<br>
<h3>Spring constant (k):</h3>
<input type="number" id="springF" placeholder="Spring constant (N/m)">
<br>
<h3>Displacement (x):</h3>
<input type="number" id="displacementF" placeholder="Displacement (m)">
<br>
</div>
<div id="omegaInputs" style="display:none;">
<h3>Mass (m):</h3>
<input type="number" id="massO" placeholder="Mass (kg)">
<br>
<h3>Spring constant (k):</h3>
<input type="number" id="springO" placeholder="Spring constant (N/m)">
<br>
</div>
<div id="periodInputs" style="display:none;">
<h3>Mass (m):</h3>
<input type="number" id="massP" placeholder="Mass (kg)">
<br>
<h3>Spring constant (k):</h3>
<input type="number" id="springP" placeholder="Spring constant (N/m)">
<br>
</div>
<br>
<button id="calculate" onclick="calculate()">Calculate</button>
<div id="result"></div>
<br>
<p>Note: This calculator assumes a simple harmonic oscillator (like a mass on a spring). Select which value to calculate.</p>
<br>
<script>
function updateInputs() {
const type = document.getElementById('equationType').value;
document.getElementById('forceInputs').style.display = type === 'force' ? '' : 'none';
document.getElementById('omegaInputs').style.display = type === 'omega' ? '' : 'none';
document.getElementById('periodInputs').style.display = type === 'period' ? '' : 'none';
}
function calculate() {
const type = document.getElementById('equationType').value;
let result = '';
if (type === 'force') {
const m = parseFloat(document.getElementById('massF').value);
const k = parseFloat(document.getElementById('springF').value);
const x = parseFloat(document.getElementById('displacementF').value);
if (isNaN(m) || isNaN(k) || isNaN(x) || m <= 0 || k <= 0) {
document.getElementById('result').innerText = 'Please enter valid positive numbers for mass, spring constant, and displacement.';
return;
}
const F = -k * x;
result = `<p>Restoring Force (F): ${F.toFixed(2)} N</p>`;
} else if (type === 'omega') {
const m = parseFloat(document.getElementById('massO').value);
const k = parseFloat(document.getElementById('springO').value);
if (isNaN(m) || isNaN(k) || m <= 0 || k <= 0) {
document.getElementById('result').innerText = 'Please enter valid positive numbers for mass and spring constant.';
return;
}
const omega = Math.sqrt(k / m);
result = `<p>Angular Frequency (ω): ${omega.toFixed(2)} rad/s</p>`;
} else if (type === 'period') {
const m = parseFloat(document.getElementById('massP').value);
const k = parseFloat(document.getElementById('springP').value);
if (isNaN(m) || isNaN(k) || m <= 0 || k <= 0) {
document.getElementById('result').innerText = 'Please enter valid positive numbers for mass and spring constant.';
return;
}
const T = 2 * Math.PI * Math.sqrt(m / k);
result = `<p>Period (T): ${T.toFixed(2)} s</p>`;
}
document.getElementById('result').innerHTML = result;
}
window.onload = updateInputs;
</script>
</body>
</html>
</html>