在Julia中保存多个PDF文件

时间:2018-06-23 13:04:39

标签: plot printing save julia

我正在尝试编写一个脚本,该脚本使用Riemann和求出至少某些性能良好的函数的傅里叶求和。忽略了我的数学目前可能严重不足的事实,我似乎无法在同一脚本中打印多个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")

here中提到的技巧对结果没有影响,即仅打印第一个PDF。这里的任何茱莉亚大师都知道是什么引起了这个问题?我没有收到任何错误消息。

3 个答案:

答案 0 :(得分:1)

我在系统上运行了代码(Julia版本0.6.1),没有错误。我的文件夹中有2个pdf文件:fun.pdf和repfun.pdf包含以下内容: fun.pdf

repfun.pdf

答案 1 :(得分:1)

好的,我已经知道问题出在哪里了。至少在使用Juno / Atom进行编码时,首次启动Julia时,它会假定工作目录为/home/<username>,这意味着如果发出savefig()命令,则图像将打印在该目录中。

如果您希望图像显示在特定位置,请给出savefig的绝对路径,或将其放在一行中

cd("/path/to/desired/location")

在文件的开头。

第一张图像以某种方式设法出现在所需文件夹中的方式仍然是我的一个谜。在上述目录中工作时,我可能曾经尝试过通过bash运行Julia,但这一定是在我设法使最基本的数字出现而没有问题之前进行的。

答案 2 :(得分:0)

试图复制您的代码,但错误是:

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 

此代码中甚至没有第433行-非常困惑。