Initialize Packages

md"""
# Initialize Packages
"""
239 μs
begin
using Colors, ColorVectorSpace, ImageShow, FileIO, ImageIO
using PlutoUI
using HypertextLiteral
: @htl, @htl_str

using LinearAlgebra
using ForwardDiff
using Symbolics
using ForwardDiff: jacobian
end
39.6 s
PlutoUI.TableOfContents()
4.1 ms

Symbolic

md"""
# Symbolic
"""
224 μs
@variables z, η
224 ms
f(x) = x^m -2;
461 μs
f′ (generic function with 1 method)
f′(x) = ForwardDiff.derivative(f, x)
471 μs

m = 1

md"""
m = $(@bind m Slider(1:6, show_value=true))
"""
285 ms

2+z

f(z)
1.1 s

2+z+η

f(z+η)
58.4 ms

2+z+η

expand(f(z+η))
3.8 s

1

f′(z)
245 ms

2+z+η

f(z) + η*f′(z)
130 ms

0

expand(f(z+η)) - (f(z) + η*f′(z))
59.8 ms

Implementation

md"""
# Implementation
"""
226 μs

Newton 1D

md"""
## Newton 1D
"""
231 μs
newton1D (generic function with 2 methods)
function newton1D(f, x0=37)
f′(x) = ForwardDiff.derivative(f, x)
x = x0
for i in 1:10
x -= f(x)/f′(x)
end
return x
end
2.7 ms
3.0
newton1D(x -> x^2-9)
64.8 ms

Newton2D

md"""
## Newton2D
"""
227 μs
T (generic function with 1 method)
T(α) = ((x,y),) -> [x + α*y^2, y + α*x^2]
1.4 ms

α =0.0

md""" α =$(@bind α Slider(0.0:0.01:1.0, show_value=true))"""
124 ms
T(α)([0.3, 0.4])
23.2 μs
@variables a, b, δ, ϵ
131 μs
T(α)([(a+δ), (b+ϵ)])
213 ms
image
image=expand.(T(α)([(a+δ), (b+ϵ)]))
322 ms

[1.00.00.01.0]

jacobian(T(α), [a, b])
3.5 s
2×2 Matrix{Text{Num}}:
 1.0  0.0
 0.0  1.0
jacobian(T(α), [a, b]) .|> Text
929 ms
simplify.(expand.(image -T(α)([a,b]) - jacobian(T(α), [a,b])*[δ, ϵ]))
815 ms
newton2D_step (generic function with 1 method)
function newton2D_step(T, x)
J = ForwardDiff.jacobian(T, x)
δ = J/ T(x)
return x - δ
end
649 μs
newton2D (generic function with 2 methods)
function newton2D(T, x0, n=10)
x = x0
for i in 1:n
x = newton2D_step(T, x)
end
return x
end
2.1 ms
inverse (generic function with 3 methods)
begin
function inverse(f, y, x0=[0,0])
return newton2D(x -> f(x) - y, x0)
end
inverse(f) = y -> inverse(f, y)
end
2.2 ms
-1
f(1)
3.0 ms
T(α)( [0.3, 0.4] )
24.3 μs

DimensionMismatch: Both inputs should have the same number of columns

  1. /(::Matrix{Float64}, ::Vector{Float64})@generic.jl:1142
  2. newton2D_step(::Main.var"workspace#3".var"#4#6"{Main.var"workspace#3".var"#2#3"{Float64}, Vector{Float64}}, ::Vector{Int64})@Other: 3
  3. newton2D(::Function, ::Vector{Int64}, ::Int64)@Other: 4
  4. newton2D@Other: 2[inlined]
  5. inverse@Other: 3[inlined]
  6. top-level scope@Local: 1[inlined]
inverse(T(α),[0.3, 0.4])
---