CS代考 Indefinite Integration

Indefinite Integration

Copyright By PowCoder代写 加微信 powcoder

using LinearAlgebra, Plots, Interact

u(x) = \int_0^x \cos t dt = \sin x

x = range(0, 1; length=n)
h = step(x) # x[k+1]-x[k] = 1/(n-1)
d = [1; fill(1/h, n-1)]
l = fill(-1/h, n-1)
L = Bidiagonal(d, l, :L)

10×10 Bidiagonal{Float64, Vector{Float64}}:
1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
-9.0 9.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ -9.0 9.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ -9.0 9.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ -9.0 9.0 ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ -9.0 9.0 ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ -9.0 9.0 ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ -9.0 9.0 ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ -9.0 9.0 ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ -9.0 9.0

scatter(x, zero(x); label=”grid”)
m = (x[1:end-1] + x[2:end])/2
scatter!(m, zero(m); label=”mid”)

0.0:0.1111111111111111:1.0

@manipulate for n = 2:100, ω=1:100
x = range(0, 1; length=n)
m = (x[1:end-1] + x[2:end])/2

h = step(x) # x[k+1]-x[k] = 1/(n-1)
d = [1; fill(1/h, n-1)]
l = fill(-1/h, n-1)
L = Bidiagonal(d, l, :L)

f = x -> cos(ω*x)*ω
u = x -> sin(ω*x)

𝐟ᶠ = f.(x[1:end-1])
𝐟ᵐ = f.(m)
𝐮ᶠ = L \ [c; 𝐟ᶠ]
𝐮ᵐ = L \ [c; 𝐟ᵐ]

g = range(0,1; length=1000)
plot(g, u.(g); label=”exact”, legend=:bottomright, ylims=(-1,1))
scatter!(x, 𝐮ᶠ; label=”forward”)
scatter!(x, 𝐮ᵐ; label=”mid”)

function forward_err(u, c, f, n)
x = range(0, 1; length=n)
h = step(x) # x[k+1]-x[k] = 1/(n-1)
d = [1; fill(1/h, n-1)]
l = fill(-1/h, n-1)
L = Bidiagonal(d, l, :L)
𝐟ᶠ = f.(x[1:end-1])
𝐮ᶠ = L \ [c; 𝐟ᶠ]
norm(𝐮ᶠ – u.(x), Inf)

function mid_err(u, c, f, n)
x = range(0, 1; length=n)
m = (x[1:end-1] + x[2:end])/2
h = step(x) # x[k+1]-x[k] = 1/(n-1)
d = [1; fill(1/h, n-1)]
l = fill(-1/h, n-1)
L = Bidiagonal(d, l, :L)
𝐮 = L \ [c; 𝐟]
norm(𝐮 – u.(x), Inf)

mid_err (generic function with 1 method)

ns = 10 .^(1:8)
scatter(ns, forward_err.(sin, 0, cos, ns); yscale=:log10, xscale=:log10, label=”forward”)
plot!(ns, ns .^ (-1); label=”1/n”)
scatter!(ns, mid_err.(sin, 0, cos, ns); yscale=:log10, xscale=:log10, label=”mid”)
plot!(ns, ns .^ (-2); label=”1/n^2″)

We observe forward-diff gives us O(1/n) convergence, mid-diff gives us O(1/n^2).

We observe stability for both methods.

mid_err.(sin, 0, cos, 10^8)

2.9676261448230434e-13

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com