YULIA: What is Erro! Lagrange polynomial function

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

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

While doing

LoadError: InexactError () when loading In [76], in an expression starting at line 1

in lg at In [74]: 11?

+3
source share
1 answer

ones range creates an Int64 array:

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

You cannot assign Float64 to an Int64 array (you get this error).

You want to simply use ones(n)to get a Float64 array:

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

Note: you do not need collectbefore repeating in the range:

for k = collect(1:n)

instead, iterate over the range:

for k = 1:n
+3
source

All Articles