html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8" />
<title>自由落下シミュレーション</title>
<style>
body {
font-family: sans-serif;
margin: 20px;
background: #f9f9f9;
}
canvas {
border: 1px solid #ccc;
background: white;
margin: 10px 0;
}
.control-panel input, .control-panel select {
margin: 4px;
}
fieldset {
margin-top: 10px;
}
label {
margin-right: 8px;
}
.graphs {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.graph-container {
flex: 1;
min-width: 300px;
}
.graph-container canvas {
width: 100%;
height: 300px;
}
.status {
margin-top: 10px;
font-weight: bold;
color: green;
}
.description {
margin-top: 30px;
background: #eef;
padding: 15px;
border-left: 4px solid #88a;
}
</style>
</head>
<body>
<h1>自由落下シミュレーション(高校・大学初年向け)</h1>
<div class="control-panel" role="form">
<fieldset>
<legend>モード選択</legend>
<label><input type="radio" name="mode" value="ideal" checked aria-label="理想(抵抗なし)">理想(抵抗なし)</label>
<label><input type="radio" name="mode" value="drag" aria-label="空気抵抗あり">抗力あり</label>
</fieldset>
<fieldset id="drag-settings" style="display:none;">
<legend>抗力モデル</legend>
<label><input type="radio" name="dragModel" value="linear" checked aria-label="一次抵抗">一次抵抗(Fd = c v)</label>
<label><input type="radio" name="dragModel" value="quadratic" aria-label="二次抵抗">二次抵抗(Fd = ½ ρ C<sub>d</sub> A v|v|)</label>
</fieldset>
<fieldset>
<legend>シミュレーションパラメータ</legend>
<label>初期高さ h [m]: <input type="number" id="h0" value="100" min="0" step="1" aria-label="初期高さ h"></label>
<label>初速度 v<sub>0</sub> [m/s]: <input type="number" id="v0" value="0" step="0.1" aria-label="初速度 v0"></label>
<label>重力 g [m/s²]: <input type="number" id="g" value="9.81" step="0.01" aria-label="重力加速度 g"></label><br>
<div id="linear-settings">
<label>抵抗係数 c [kg/s]: <input type="number" id="c" value="0.1" step="0.01" aria-label="一次抵抗係数c"></label>
</div>
<div id="quadratic-settings" style="display:none;">
<label>空気密度 ρ [kg/m³]: <input type="number" id="rho" value="1.225" step="0.01" aria-label="空気密度 rho"></label>
<label>抗力係数 C<sub>d</sub>: <input type="number" id="cd" value="0.47" step="0.01" aria-label="抗力係数 Cd"></label>
<label>断面積 A [m²]: <input type="number" id="area" value="0.05" step="0.01" aria-label="断面積 A"></label>
</div>
</fieldset>
<fieldset>
<legend>操作</legend>
<button id="startBtn" aria-label="開始">▶ 開始</button>
<button id="pauseBtn" aria-label="一時停止">⏸ 一時停止</button>
<button id="resetBtn" aria-label="リセット">🔁 リセット</button>
</fieldset>
</div>
<div class="status" id="statusMsg" aria-live="polite"></div>
<div class="graphs">
<div class="graph-container">
<strong>高さ y - 時間 t</strong>
<canvas id="graphYt" width="400" height="300" aria-label="高さ対時間 グラフ"></canvas>
</div>
<div class="graph-container">
<strong>速度 v - 時間 t</strong>
<canvas id="graphVt" width="400" height="300" aria-label="速度対時間 グラフ"></canvas>
</div>
<div class="graph-container">
<strong>速度 v - 高度 y(位相図)</strong>
<canvas id="graphVy" width="400" height="300" aria-label="速度対高度 グラフ"></canvas>
</div>
</div>
<div class="description" role="region" aria-labelledby="descTitle">
<h2 id="descTitle">モデル説明・仮定</h2>
<p>
本シミュレーションは、物体が地面に向かって自由落下する運動をモデル化します。
モード「理想」では空気抵抗なし、モード「抗力あり」では速度に比例する一次抵抗または、速度の2乗に比例する抗力を導入しています。
</p>
<p><strong>運動方程式:</strong></p>
<ul>
<li>理想: <math>ma = mg → dv/dt = g</math></li>
<li>一次抗力: <math>ma = mg - cv → dv/dt = g - (c/m)v</math></li>
<li>二次抗力: <math>ma = mg - (1/2)ρC<sub>d</sub>Av² → dv/dt = g - (ρC<sub>d</sub>Av²)/(2m)</math></li>
</ul>
<p>ただし質量$m$は省略されています(m=1kg仮定)。時刻t=0で高さ$h$、速度$v_0$。物体が地面(y=0)に到達すると停止します。</p>
</div>
<script>
// UI要素取得
const getEl = id => document.getElementById(id);
const hInput = getEl('h0');
const vInput = getEl('v0');
const gInput = getEl('g');
const cInput = getEl('c');
const rhoInput = getEl('rho');
const cdInput = getEl('cd');
const areaInput = getEl('area');
const startBtn = getEl('startBtn');
const pauseBtn = getEl('pauseBtn');
const resetBtn = getEl('resetBtn');
const statusMsg = getEl('statusMsg');
const ytCanvas = getEl('graphYt');
const vtCanvas = getEl('graphVt');
const vyCanvas = getEl('graphVy');
const ctxYt = ytCanvas.getContext('2d');
const ctxVt = vtCanvas.getContext('2d');
const ctxVy = vyCanvas.getContext('2d');
let mode = 'ideal';
let dragModel = 'linear';
let running = false;
let t = 0, dt = 0.02;
let y = 0, v = 0;
let pathYt = [], pathVt = [], pathVy = [];
document.querySelectorAll('input[name="mode"]').forEach(el => {
el.addEventListener('change', () => {
mode = document.querySelector('input[name="mode"]:checked').value;
document.getElementById('drag-settings').style.display = (mode === 'drag') ? 'block' : 'none';
updateParams();
});
});
document.querySelectorAll('input[name="dragModel"]').forEach(el => {
el.addEventListener('change', () => {
dragModel = document.querySelector('input[name="dragModel"]:checked').value;
document.getElementById('linear-settings').style.display = (dragModel === 'linear') ? 'block' : 'none';
document.getElementById('quadratic-settings').style.display = (dragModel === 'quadratic') ? 'block' : 'none';
updateParams();
});
});
[hInput, vInput, gInput, cInput, rhoInput, cdInput, areaInput].forEach(el => {
el.addEventListener('input', () => {
if (!running) updateParams();
});
});
function updateParams() {
t = 0;
y = parseFloat(hInput.value);
v = parseFloat(vInput.value);
pathYt = [], pathVt = [], pathVy = [];
drawAll();
statusMsg.textContent = "";
}
startBtn.onclick = () => {
if (!running) {
running = true;
requestAnimationFrame(step);
}
};
pauseBtn.onclick = () => running = false;
resetBtn.onclick = () => {
running = false;
updateParams();
};
function step() {
if (!running) return;
// 数値積分: 改良オイラー法
let g = parseFloat(gInput.value);
let c = parseFloat(cInput.value);
let rho = parseFloat(rhoInput.value);
let cd = parseFloat(cdInput.value);
let A = parseFloat(areaInput.value);
let a = computeAcceleration(v, g, c, rho, cd, A);
let v_mid = v + a * dt / 2;
let a_mid = computeAcceleration(v_mid, g, c, rho, cd, A);
v += a_mid * dt;
y -= v * dt;
t += dt;
pathYt.push([t, y]);
pathVt.push([t, v]);
pathVy.push([y, v]);
drawAll();
// 衝突判定
if (y <= 0) {
y = 0;
running = false;
statusMsg.textContent = `到達時刻: ${t.toFixed(2)} s, 終端速度: ${v.toFixed(2)} m/s`;
return;
}
requestAnimationFrame(step);
}
function computeAcceleration(v, g, c, rho, cd, A) {
if (mode === 'ideal') return g;
if (dragModel === 'linear') return g - c * v;
else return g - 0.5 * rho * cd * A * v * Math.abs(v);
}
function drawGraph(ctx, data, xlabel, ylabel, color = 'blue') {
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
if (data.length === 0) return;
let padding = 40;
let w = ctx.canvas.width;
let h = ctx.canvas.height;
let xMax = Math.max(...data.map(([x]) => x));
let xMin = Math.min(...data.map(([x]) => x));
let yMax = Math.max(...data.map(([, y]) => y));
let yMin = Math.min(...data.map(([, y]) => y));
yMax = Math.max(yMax, 1);
yMin = Math.min(yMin, 0);
function sx(x) {
return padding + (x - xMin) / (xMax - xMin) * (w - 2 * padding);
}
function sy(y) {
return h - padding - (y - yMin) / (yMax - yMin) * (h - 2 * padding);
}
// 軸
ctx.strokeStyle = '#000';
ctx.beginPath();
ctx.moveTo(padding, padding);
ctx.lineTo(padding, h - padding);
ctx.lineTo(w - padding, h - padding);
ctx.stroke();
// 軸ラベル
ctx.fillStyle = 'black';
ctx.fillText(xlabel, w / 2, h - 10);
ctx.fillText(ylabel, 5, 20);
// データ
ctx.beginPath();
ctx.strokeStyle = color;
data.forEach(([x, y], i) => {
if (i === 0) ctx.moveTo(sx(x), sy(y));
else ctx.lineTo(sx(x), sy(y));
});
ctx.stroke();
}
function drawAll() {
drawGraph(ctxYt, pathYt, '時間 t [s]', '高さ y [m]', 'blue');
drawGraph(ctxVt, pathVt, '時間 t [s]', '速度 v [m/s]', 'orange');
drawGraph(ctxVy, pathVy, '高さ y [m]', '速度 v [m/s]', 'green');
}
// 初期化
updateParams();
</script>
</body>
</html>