3
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

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

When Executed

LoadError: InexactError() while loading In[76], in expression starting on line 1

in lg at In[74]:11?

1 Answers1

3

ones of a 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 can't assign a Float64 into a Int64 Array (you get this error).

You want to just use ones(n) to get a Float64 Array:

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

Side note: You don't need to collect before iterating over a range:

for k = collect(1:n)

instead just iterate over the range:

for k = 1:n
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535