-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20191011_diffSpectral.jl
More file actions
53 lines (41 loc) · 989 Bytes
/
20191011_diffSpectral.jl
File metadata and controls
53 lines (41 loc) · 989 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using Test; using PyPlot; using Revise; using BenchmarkTools; using AbstractFFTs; using FFTW;
#= test function without discontinuity in derivative at t=0 =#
function funk1(x)
return (1/2)*x^2 + (1/3)*x^3 - (1/4)*x^4
end
function funk1dot(x)
return x + x^2 - x^3
end
function funk2(x)
return x + (1/2)*x^2 - (1/3)*x^3
end
function funk2dot(x)
return 1 + x - x^2
end
even(x) = x%2==0
function spectralmultiplier(x, L)
N = length(x)
out = similar(x, Complex{Float64})
for k in 0:(N-1)
if k<(N/2)
out[k+1] = x[k+1]*2*π*im*k/L
elseif k>(N/2)
out[k+1] = x[k+1]*2*π*im*(k-N)/L
end
end
if even(N); out[N÷2]=0.0; end
return out
end
function specdifftest(f, fdot)
yff = fft(f)
yffdot = spectralmultiplier(yff, t[end])
ydot_approx = real.(ifft(yffdot))
plot(t, fdot)
plot(t, ydot_approx, "--", alpha=0.2)
return
end
dt = 0.01
t = Vector{Float64}(0.0:dt:8*pi)
#y = -cos.(t)
#ydot = sin.(t)
specdifftest(funk1.(t), funk1dot.(t))