🙄

関数の特異値分解2

2023/12/09に公開

続いて、特異値分解して得られた近似関数とオリジナルの関数の誤差をプロットしてみる

using LinearAlgebra
using Plots

b = 10
n = 412
h1 = b / n
x = -b/2:h1:b/2
y = -b/2:h1:b/2
n1 = size(x)[1]
# Create a mesh grid
#X, Y = meshgrid(x, y)

# Calculate the original function
alpha1 = 1
A1 = zeros(n1, n1)

for i = 1:n1
    A1[i, :] .= exp.(-alpha1 .* sqrt.(x[i]^2 .+ y[:].^2))
end
# Perform singular value decomposition
U1, S1, V1 = svd(A1)



# Approximate the original function
r1 = 5
Ar1 = zeros(size(A1))
for i = 1:r1
    Ar = sigmas[i] * U1[:, i] * V1[:, i]'
    Ar1 += Ar
end

# Plot the approximation
p1 = surface(x, y, Ar1)

# Calculate and plot the error
ER_A = abs.(A1 - Ar1)
p2 = surface(x, y, ER_A)

# Display the plots
plot(p1, p2)

Discussion