Reputation: 1603
I'm trying to write a script that draws Fourier sums of at least certain nicely behaving functions using Riemann sums. Ignoring the fact that my math might be seriously off at the moment, I can't seem to be able to print multiple PDF-files during the same script, which goes as follows:
"""
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")
The trick mentioned here has no effect on the outcome, which is that only the first PDF gets printed. Any Julia gurus here that know what is causing the issue? I get no error messages whatsoever.
Upvotes: 0
Views: 570
Reputation: 1603
Alright, I've figured out what the problem is or was. At least when coding in Juno/Atom, when Julia is first started it assumes that the working directory is /home/<username>
, meaning if you issue a savefig()
-command, the images are printed there.
If you wish the images to appear at a specific location, either give the absolute path to savefig
, or put the line
cd("/path/to/desired/location")
at the beginning of your file.
How the first image somehow managed to appear in the desired folder still remains a mystery to me. I might have tried running Julia through bash, when working in said directory, but this must have been before I managed to get even the most rudimentary of figures to appear without issues.
Upvotes: 1
Reputation: 151
I ran the code on my system (Julia Version 0.6.1) without errors. Got 2 pdfs in my Folder: fun.pdf and repfun.pdf containing this:
Upvotes: 1
Reputation: 4571
Tried replicating your code, but the error is:
ERROR: LoadError: UndefVarError: StepRangeLen not defined
in include at boot.jl:261
in include_from_node1 at loading.jl:320
in process_options at client.jl:280
in _start at client.jl:378
while loading C:\Users\Евгений\Desktop\1.jl, in expression starting on line 433
There is not even line 433 in this code - very confused.
Upvotes: 0