console
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>指数分布期望自测 | 银行等待时间例题</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<style>
*{box-sizing:border-box;font-family:system-ui,-apple-system,Segoe UI,Roboto,sans-serif;}
body{background:#f7f9fc;padding:20px;max-width:1000px;margin:0 auto;color:#222;}
.card{background:#fff;border-radius:16px;padding:24px;margin-bottom:20px;box-shadow:0 4px 16px #00000010;}
h2{color:#2563eb;margin-top:0;font-size:1.3rem;}
.question{font-size:1.05rem;line-height:1.7;margin-bottom:16px;}
.input-row{display:flex;gap:10px;align-items:center;flex-wrap:wrap;margin:16px 0;}
input[type="number"]{padding:10px 12px;border:1px solid #cbd5e1;border-radius:8px;width:140px;font-size:1rem;}
button{padding:10px 16px;border:none;border-radius:8px;background:#2563eb;color:#fff;font-size:1rem;cursor:pointer;transition:0.2s;}
button:hover{background:#1d4ed8;}
.btn-secondary{background:#64748b;}
.btn-secondary:hover{background:#475569;}
.answer-block{margin-top:16px;padding:16px;background:#eff6ff;border-radius:10px;display:none;line-height:1.8;}
.result-correct{color:#16a34a;font-weight:bold;}
.result-wrong{color:#dc2626;font-weight:bold;}
.slider-row{margin:16px 0;}
.slider-row label{display:block;margin-bottom:6px;}
input[type="range"]{width:100%;}
.chart-wrap{width:100%;height:320px;margin-top:10px;}
.tip{color:#64748b;font-size:0.9rem;}
</style>
</head>
<body>
<div class="card">
<h2>📖 原题:顾客银行等待时间(指数分布)</h2>
<div class="question">
设顾客在某银行的窗口等待服务的时间 $X$(以分计)服从指数分布,其概率密度:
$$
f(x)=
\begin{cases}
\displaystyle \frac15 e^{-x/5}, & x>0\\[4pt]
0, & x\le 0
\end{cases}
$$
试求顾客等待服务的平均时间(期望 $E(X)$)?
</div>
<div class="input-row">
<span>你的答案:</span>
<input id="ansInput" type="number" step="0.01" placeholder="输入期望数值">
<button onclick="checkAnswer()">提交校验</button>
<button class="btn-secondary" onclick="toggleAnswer()">查看答案与推导</button>
</div>
<div id="checkResult"></div>
<div id="answerBlock" class="answer-block">
<strong>✅ 标准答案:$\boldsymbol{E(X)=5}$(分钟)</strong>
<br><br>
指数分布通用形式:$f(x)=\lambda e^{-\lambda x},\;x>0$,期望 $E(X)=\dfrac1\lambda$
<br>
本题对比:$\lambda=\dfrac15$,所以 $E(X)=\dfrac{1}{1/5}=5$
<br><br>
积分严格推导:
$$
\begin{aligned}
E(X)&=\int_{0}^{+\infty} x\cdot f(x)dx
=\int_{0}^{+\infty} x\cdot \frac15 e^{-x/5} dx \\
&=-x e^{-x/5}\bigg|_{0}^{+\infty}+\int_{0}^{+\infty}e^{-x/5}dx \\
&=0+\Big[-5e^{-x/5}\Big]_{0}^{+\infty}=5
\end{aligned}
$$
</div>
</div>
<div class="card">
<h2>📊 交互式图像调节面板(指数分布 $f(x)=\lambda e^{-\lambda x}$)</h2>
<div class="slider-row">
<label>速率参数 $\lambda =$ <span id="lamVal">0.20</span> (原题 λ=0.2)</label>
<input type="range" id="lamSlider" min="0.05" max="2.0" step="0.01" value="0.2" oninput="updateChart()">
<div class="tip">拖动滑块修改 λ,期望 $E(X)=1/\lambda$ 自动更新,图中红色竖线 = 期望位置</div>
</div>
<div class="chart-wrap">
<canvas id="expChart"></canvas>
</div>
</div>
<script>
// 标准答案
const trueAns = 5;
let chart;
let lam = 0.2;
function toggleAnswer(){
const block = document.getElementById("answerBlock");
block.style.display = block.style.display === "block" ? "none" : "block";
}
function checkAnswer(){
const val = parseFloat(document.getElementById("ansInput").value);
const resDom = document.getElementById("checkResult");
if(isNaN(val)){
resDom.innerHTML = "<span class='result-wrong'>请输入合法数字!</span>";
return;
}
if(Math.abs(val - trueAns) < 0.001){
resDom.innerHTML = "<span class='result-correct'>✅ 正确!期望就是5分钟</span>";
}else{
resDom.innerHTML = `<span class='result-wrong'>❌ 不对,正确期望是 ${trueAns}</span>`;
}
}
function updateChart(){
lam = parseFloat(document.getElementById("lamSlider").value);
document.getElementById("lamVal").innerText = lam.toFixed(2);
const mu = 1 / lam; //期望
//生成x数据
const xs = [];
const pdfYs = [];
const cdfYs = [];
const maxX = Math.max(mu * 4,10);
for(let x=0;x<=maxX;x+=0.15){
xs.push(x);
const pdf = lam * Math.exp(-lam * x);
pdfYs.push(pdf);
const cdf = 1 - Math.exp(-lam * x);
cdfYs.push(cdf);
}
if(chart) chart.destroy();
const ctx = document.getElementById("expChart").getContext("2d");
chart = new Chart(ctx,{
type:"line",
data:{
labels:xs,
datasets:[
{label:"概率密度 PDF $f(x)$",data:pdfYs,borderColor:"#2563eb",fill:false,pointRadius:0,borderWidth:2},
{label:"分布函数 CDF $F(x)$",data:cdfYs,borderColor:"#10b981",fill:false,pointRadius:0,borderWidth:2}
]
},
options:{
responsive:true,maintainAspectRatio:false,
plugins:{
title:{display:true,text:`指数分布 λ=${lam.toFixed(2)},期望 E(X)=${mu.toFixed(3)}`,font:{size:14}},
annotation:{
annotations:{
expectLine:{type:"line",xMin:mu,xMax:mu,borderColor:"#ef4444",borderWidth:2,label:{display:true,content:"E(X)期望",position:"top"}}
}
}
},
scales:{x:{title:{display:true,text:"x 等待时间"}},y:{title:{display:true,text:"f(x) / F(x)"}}}
}
})
}
//初始化
updateChart();
</script>
</body>
</html>