Я пытаюсь написать скрипт, который рисует суммы Фурье, по крайней мере, определенных функций с хорошим поведением, используя суммы Римана.Игнорируя тот факт, что моя математика может быть серьезно отключена в данный момент, я не могу печатать несколько PDF-файлов во время одного и того же сценария, который выглядит следующим образом:
"""
RepeatFunction(domain::Array{Real,1},fvals::Array{Complex{Real}},
repeatN::Int64,period::Float64=2*pi)
Builds a periodic function centered at origin in both directions from a set of
given function values, but more imporrtantly, also stretches out the domain to
accommodate this new extended array.
"""
function RepeatFunction(domain::Array{Float64,1},fvals::Array{Float64,1},
N::Int64,period::Float64=2*pi)
# Extending the domain
for n in 1:N/2
domain = [
linspace(domain[1]-period,domain[2],length(fvals));
domain;
linspace(domain[end],domain[end-1]+period,length(fvals))];
end
# Repeating the function
if N % 2 == 0
fvals = repeat(fvals,outer=[N+1]);
else
fvals = repeat(fvals, outer=[N]);
end
return domain, fvals
end
"""
RiemannSum(domain::Array{Float64},fvals::Array{Float64})::Float64
Calculates the discrete Riemann sum of a real valued function on a given
equipartitioned real domain.
"""
function RiemannSum(domain::Array{Complex{Real},1},fvals::Array{Complex{Real},1})
try
L = domain[end] - domain[1];
n = length(fvals);
return sum(fvals * L / n);
catch
println("You most likely attempted to divide by zero.
Check the size of your domain.")
return NaN
end
end
"""
RiemannSum(domain::StepRange{Real,Real},fvals::StepRange{Real,Real})::Float64
Calculates the discrete Riemann sum of a function on a given
equipartitioned domain.
"""
function RiemannSum(domain,fvals)
try
L = domain[end] - domain[1];
n = length(fvals);
return sum(fvals * L / n);
catch
println("You most likely attempted to divide by zero.
Check the size of your domain.")
return NaN
end
end
"""
RiemannSum(domain::StepRange{Real,Real},fvals::StepRange{Real,Real})::Float64
Calculates the discrete Riemann sum of a function on a given
equipartitioned domain.
"""
function RiemannSum(domain::StepRangeLen{Real,Base.TwicePrecision{Real},Base.TwicePrecision{Real}},
fvals::StepRangeLen{Real,Base.TwicePrecision{Real},Base.TwicePrecision{Real}})
try
L = domain[end] - domain[1];
n = length(fvals);
return sum(fvals * L / n);
catch
println("You most likely attempted to divide by zero.
Check the size of your domain.")
return NaN
end
end
"""
RiemannSum(domain::StepRange{Real,Real},fvals::StepRange{Real,Real})::Float64
Calculates the discrete Riemann sum of a function on a given
equipartitioned domain.
"""
function RiemannSum(domain,fvals)
try
L = domain[end] - domain[1];
n = length(fvals);
return sum(fvals * L / n);
catch
println("You most likely attempted to divide by zero.
Check the size of your domain.")
return NaN
end
end
"""
FourierCoefficient(domain,fvals)
Calculates an approximation to the Fourier coefficient for a function with
a period equal to the domain length.
"""
function FourierCoefficient(domain,fvals,n::Int64,T::Float64)
return 1/ T * RiemannSum(domain,fvals*exp(-1im * n * 1/T));
end
"""
FourierSum(domain.fvals)
Calculates the Fourier sum of a function on a given domain.
"""
function FourierSum(domain,fvals, N::Int64,T::Float64)
return [sum(FourierCoefficient(domain,fvals,n,T)*exp(1im * n * 1/T)) for n in -N:N];
end
using Plots;
pyplot()
n = 10;
T = 2*pi;
x = collect(linspace(-pi,pi,2n+1));
f = x.^2 + x;
funplot = plot(x,f);
#display(funfig)
savefig("./fun.pdf")
#println("Φ =",real(Φ))
x,repf = RepeatFunction(x,f,6,T)
repfunplot = plot(x,repf,reuse=false);
#display(repfunfig)
savefig("./repfun.pdf")
Упомянутый трюк здесь не влияет на результат, так как печатается только первый PDF.Любые гуру Юлии, которые знают, что является причиной проблемы?Я не получаю никаких сообщений об ошибках.