WIP; better figures

This commit is contained in:
jverzani
2025-07-02 14:05:09 -04:00
parent 5013211954
commit c38a7c9f1d
5 changed files with 380 additions and 51 deletions

View File

@@ -190,6 +190,40 @@ Clearly for a given partition and choice of $c_i$, the above can be computed. Ea
#| hold: true
#| echo: false
gr()
function left_riemann(n)
empty_style = (xaxis=([], false),
yaxis=([], false),
framestyle=:origin,
legend=false)
axis_style = (arrow=true, side=:head, line=(:gray, 1))
rectangle = (x, y, w, h) -> Shape(x .+ [0,w,w,0], y .+ [0,0,h,h])
f = x -> -(x+1/2)*(x-1)*(x-3) + 1
a, b= 1, 3
plot(; empty_style...)
plot!(f, a, b; line=(:black, 3))
plot!([a-.25, b+.25], [0,0]; axis_style...)
plot!([a-.1, a-.1], [-.25, .5 + f(a/2 +b/2)]; axis_style...)
Δ = (b-a)/n
for i ∈ 0:n-1
xᵢ = a + i*Δ
plot!(rectangle(xᵢ, 0, Δ, f(xᵢ)), opacity=0.5, color=:red)
end
area = round(sum(f(a + i*Δ)*Δ for i ∈ 0:n-1), digits=3)
annotate!([
(a, 0, text(L"a", :top)),
(b, 0, text(L"b", :top)),
(a, f(a/2+b/2), text("\$L_{$n} = $area\$", :left))
])
current()
end
#=
rectangle(x, y, w, h) = Shape(x .+ [0,w,w,0], y .+ [0,0,h,h])
function ₙ(j)
a = ("₋","","","₀","₁","₂","₃","₄","₅","₆","₇","₈","₉")
@@ -210,6 +244,7 @@ function left_riemann(n)
title!("L$(ₙ(n)) = $a")
p
end
=#
anim = @animate for i ∈ (2,4,8,16,32,64)
left_riemann(i)
@@ -631,19 +666,19 @@ Before continuing, we define a function to compute Riemann sums for us with an
```{julia}
#| eval: false
riemann(f, a, b, n; method="right") = riemann(f, range(a,b,n+1); method=method)
function riemann(f, xs; method="right")
Ms = (left = (f,a,b) -> f(a),
right= (f,a,b) -> f(b),
Ms = (left = (f,a,b) -> f(a),
right = (f,a,b) -> f(b),
trapezoid = (f,a,b) -> (f(a) + f(b))/2,
simpsons = (f,a,b) -> (c = a/2 + b/2; (1/6) * (f(a) + 4*f(c) + f(b))),
simpsons = (f,a,b) -> (c = a/2 + b/2; (1/6) * (f(a) + 4*f(c) + f(b)))
)
_riemann(Ms[Symbol(method)], f, xs)
end
function _riemann(M, f, xs)
M = Ms[Symbol(method)}
xs = zip(xs[1:end-1], xs[2:end])
sum(M(f, a, b) * (b-a) for (a,b) ∈ xs)
end
riemann(f, a, b, n; method="right") =
riemann(f, range(a,b,n+1); method)
```
(This function is defined in `CalculusWithJulia` and need not be copied over if that package is loaded.)
@@ -699,7 +734,7 @@ Consider a function $g(x)$ defined through its piecewise linear graph:
```{julia}
#| echo: false
g(x) = abs(x) > 2 ? 1.0 : abs(x) - 1.0
plot(g, -3,3)
plot(g, -3,3; legend=false)
plot!(zero)
```
@@ -710,6 +745,24 @@ plot!(zero)
We could add the signed area over $[0,1]$ to the above, but instead see a square of area $1$, a triangle with area $1/2$ and a triangle with signed area $-1$. The total is then $1/2$.
This figure may make the above decomposition more clear:
```{julia}
#| echo: false
let
g(x) = abs(x) > 2 ? 1.0 : abs(x) - 1.0
xs = [ -3, -2, -1, 1, 2, 3]
plot(; legend=false, aspect_ratio=:equal)
plot!(Shape([-3,-2,-2,-3], [0,0,1,1]); fill=(:gray,))
plot!(Shape([-2,-1,-1,-2], [0,0,0,1]); fill=(:gray10,))
plot!(Shape([-1,0,1], [0,-1,0]); fill=(:gray90,))
plot!(Shape([1,2,2,1], [0,1,0,0]); fill=(:gray10,))
plot!(Shape([2,3,3,2], [0,0,1,1]); fill=(:gray,))
end
```
* Compute $\int_{-3}^{3} g(x) dx$: