gpt4 book ai didi

function - Julia :怎么了!拉格朗日多项式函数

转载 作者:行者123 更新时间:2023-12-03 06:25:04 26 4
gpt4 key购买 nike

function lg(X,Y,Xint)

n = length(X)
L = ones(1:n)

for k = collect(1:n)

L[k] = 1
for c = collect(1:n)
if c!=k
L[k] = (L[k]*( Xint - X[c] ))/( X[k] - X[c] )
end
end
end
return sum(Y.*L)
end

==========================

执行时

加载错误:不精确错误()加载 In[76] 时,在第 1 行开始的表达式中

在 In[74]:11 处的 lg 中?

最佳答案

ones 范围创建一个 Int64 数组:

julia> o = ones(1:3)
3-element Array{Int64,1}:
1
1
1

julia> o[1] = 3.5
ERROR: InexactError()
in setindex!(::Array{Int64,1}, ::Float64, ::Int64) at ./array.jl:339
in eval(::Module, ::Any) at ./boot.jl:226

您无法将 Float64 分配给 Int64 数组(您会收到此错误)。

您只想使用 ones(n) 来获取 Float64 数组:

julia> ones(3)
3-element Array{Float64,1}:
1.0
1.0
1.0
<小时/>

旁注:在迭代某个范围之前,您不需要收集:

for k = collect(1:n)

只需迭代范围即可:

for k = 1:n

关于function - Julia :怎么了!拉格朗日多项式函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37494160/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com