This function takes input values for x and y, uses Lagrange interpolation to calculate the coefficients C of a polynomial that passes through the (x,y) points, and returns C. It constructs a Lagrange basis polynomial for each x value, calculates the coefficients by multiplying the y values by the basis polynomials, and returns both C and an inline function f that defines the Lagrange interpolating polynomial for plotting.
This function takes input values for x and y, uses Lagrange interpolation to calculate the coefficients C of a polynomial that passes through the (x,y) points, and returns C. It constructs a Lagrange basis polynomial for each x value, calculates the coefficients by multiplying the y values by the basis polynomials, and returns both C and an inline function f that defines the Lagrange interpolating polynomial for plotting.
This function takes input values for x and y, uses Lagrange interpolation to calculate the coefficients C of a polynomial that passes through the (x,y) points, and returns C. It constructs a Lagrange basis polynomial for each x value, calculates the coefficients by multiplying the y values by the basis polynomials, and returns both C and an inline function f that defines the Lagrange interpolating polynomial for plotting.
x=input('Ingrese Los valores de x [x0 ...xf] : ');
y=input('Ingrese Los valores de y [y0 ...yf] : '); n1=length(x); n=n1-1; L=zeros(n1,n1); for k=1:n+1 V=1; for j=1:n+1 if k~=j V=conv(V,poly(x(j)))/(x(k)-x(j)); end end L(k,:)=V end C=y*L; text = ''; exp = length(C); for r=1: length(C) exp = exp - 1; text = strcat(text, '+', num2str(C(r)), '*x.^', num2str(exp)); end f = inline(text);