Question

I am working in a script for solve lagrange interpolation in matlab. I am new in it and i don't know how it works.

The data for interpolation are the next

X = [0 1 2 4 8]
Y = [1 5 10 24 50]

Then i build the polynomial od lagrange at this way:

syms x

l1 = ( (x-X(2)) * (x-X(3)) * (x-X(4)) * (x-X(5)) ) / ( (X(1)-X(2)) * (X(1)-X(3)) * (X(1)-X(4)) *(X(1)-X(5)) )

l2 = ( (x-X(1)) * (x-X(3)) * (x-X(4)) * (x-X(5)) ) / ( (X(2)-X(1)) * (X(2)-X(3)) * (X(2)-X(4)) *(X(2)-X(5)) )

l3 = ( (x-X(1)) * (x-X(2)) * (x-X(4)) * (x-X(5)) ) / ( (X(3)-X(1)) * (X(3)-X(2)) * (X(3)-X(4)) *(X(3)-X(5)) )

l4 = ( (x-X(1)) * (x-X(2)) * (x-X(3)) * (x-X(5)) ) / ( (X(4)-X(1)) * (X(4)-X(2)) * (X(4)-X(3)) *(X(4)-X(5)) )

l5 = ( (x-X(1)) * (x-X(2)) * (x-X(3)) * (x-X(4)) ) / ( (X(5)-X(1)) * (X(5)-X(2)) * (X(5)-X(3)) *(X(5)-X(4)) )

interp = l1*Y(1) + l2*Y(2) + l3*Y(3) + l4*Y(4) + l5*Y(5)

but then when i want evaluate it or i want plot it i don't know which is the way.

Can someone help me?

Était-ce utile?

La solution

Make a symfun of it:

f(x)= l1*Y(1) + l2*Y(2) + l3*Y(3) + l4*Y(4) + l5*Y(5);

Plot it:

ezplot(f)

evaluate it:

f(1)

To increase readability, use:

f(x)=simplify(l1*Y(1) + l2*Y(2) + l3*Y(3) + l4*Y(4) + l5*Y(5))
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top