绘图时发现OutOfMemoryError()怎么解决?

using Plots
x = [341948,1092759,5491,49375,1340000,365,2500,78200,867023,14000,23700,70700,304500,138000,2602000]
y = [4.81,5.88,3.31,4.90,5.62,2.76,2.27,3.85,5.21,3.70,3.27,4.31,4.42,4.39,5.05]
function get_Lagrange_interpolation_value(xx,yy,x)
    @assert length(xx) == length(yy)
    y = 0.0
    N = length(xx)
    for i in 1:N
        w = 1.0
        for j in 1:N
            if j != i
                w *= (x-xx[j]) / (xx[i]-xx[j])
            end
        end
        y += yy[i]*w
    end
    return y
end
x1,x2=extrema(x)
xx = x1:0.01:x2
yy = zero(xx);
for i in 1:length(xx)
    yy[i] = get_Lagrange_interpolation_value(x,y,xx[i])
end
scatter(x,y,label="data")
p = plot!(xx,yy,label="P(x)",title="Lagrange Interpolation",aspect_ratio=:equal)

加内存条吧(为啥非要8个字符)

你能把整个代码发一下吗
也有可能是占用内存太多,可以再试一下

您好,全部代码已经附在问题当中。谢谢您的建议!
不过,我重试了好几次,每次都要运行很长的时间,最后就说OutOfMemoryError()
请问您遇到过这样的情况吗?

这是说电脑本身的内存不够才会出现这样的问题吗?

绘图的数据,比如xx,yy你能都打出来吗

我正在运行,内存占用大概 13.7G.也就是说,你至少要有16G 内存.另外,第二个图会出错

GKS: character ignored due to unicode error
GKS: character ignored due to unicode error
GKS: character ignored due to unicode error
GKS: character ignored due to unicode error
GKS: character ignored due to unicode error
GKS: character ignored due to unicode error
GKS: character ignored due to unicode error
GKS: character ignored due to unicode error
GKS: character ignored due to unicode error
GKS: character ignored due to unicode error
GKS: file write error (fd=52, cc=-1)
write: 错误的地址

你的xx划分太细了,从365到2602000每0.01分隔,差不多2.6亿的数量,每个数据是Float64,这就要2GB的数据存储xx,yy也同样需要这么大。
我看你最后是x,y和xx,yy画在一张图上,应该不需要这么高的分辨度吧。像x这样数值跨度比较大的情况,建议在对数空间里划分,类似

exp.(log(x1):0.01:log(x2))

另外你的画图不需要aspect_ratio=:equal,建议改为对数画图。

可以的,在jupyter里面执行的时候是成功的

原来是这样,谢谢您的建议!

了解了,谢谢您!

京ICP备17009874号-2