On Fri, May 27, 2016 at 8:56 PM, <[email protected]> wrote:
> Lagrange Poly
>
> 1 function lg(X,Y,Xint)
> 2
> 3 n = length(X)
> 4 L = ones(1:n)
You probably want `ones(n)` here. `ones(1:n)` returns an array with
the same element type and shape with `1:n` and therefore is a
`Vector{Int}`. You cannot assign an arbitrary floating point to a
integer vector, which is what causing the `InexactError` you see.
> 5
> 6 for k = collect(1:n)
Don't do this, `for k = 1:n` is much better
> 7
> 8
> 9 for c = collect(1:n)
Same here
> 10 if c!=k
> 11 L[k] = (L[k]*( Xint - X[c] ))/( X[k] - X[c] ) # problem
> 12 end
> 13 end
> 14 end
> 15 return sum(Y.*L)
> 16 end
>
>
> ===========================
>
> x = collect(1:10)
>
> y = x.^2 - 2.*x + rand()
>
>
> When Executed
>
> lg(x,y,2.5)
>
>
> LoadError: InexactError() while loading In[76], in expression starting on
> line 1
>
> in lg at In[74]:11?