最近在做一个黎曼积分演示动画,在选中插值选项Uniform random后,矩形的插值位置一直在变动导致矩形面积也一直在变化,求高手处理一下
Manipulate[f[x_] := Which[0 <= x <= t0, v0, x > t0, v0 + aa (x - t0)];
rectangles =
Select[Table[{Opacity[0.05], EdgeForm[Gray],
Rectangle[{i (s)/n, 0}, {(i + 1) (s)/n, heightfunction[i]}]}, {i,
0, n - 1, 1}], #[[3, 2, 2]] > 1 &];
Show[{Plot[
Which[0 <= x <= t0, v0, x > t0, v0 + aa (x - t0)], {x, 0, s},
PlotStyle -> Thick, AxesOrigin -> {0, 0},
PlotRange -> {0, v0 + 5},
PlotLabel -> Style["黎曼积分求和演示", 18, Blue],
AxesLabel -> {Style["s", 16, Red], Style["m/s", 16, Red]}],
Graphics[{rectangles,
Text[Style[
"显示的矩形面积之和为" <>
ToString[
Total[Area /@
Cases[rectangles, Rectangle[{_, _}, {_, _?(# > 1 &)}],
Infinity]
] // N], 14], {0.75 s, 0.9 ( v0 + 5)}]}]},
ImageSize -> Large], {{s, 6}, 1, 20}, {{n, 15}, 1, 80, 1}, {{t0, 1},
0, 2, 0.1}, {{v0, 25}, 0, 50, 1}, {{aa, -1}, -10, 0,
0.1}, {{heightfunction, (Mean[{f[# s/n],
f[(# + 1) s/n]}] &)}, {(f[# (s)/n] &) ->
"left", (Mean[{f[# (s)/n], f[(# + 1) s/n]}] &) ->
"midpoint", (f[
RandomVariate[UniformDistribution[{# s/n, (# + 1) s/n}]]] &) ->
"Uniform random", (f[(# + 1) s/n] &) -> "right"},
ControlType -> SetterBar}]
