can anyone help me in writing Program in Matlab for Lagrange interpolation . which should take data from user in the form of vectors (abscissa and ordinate) and return the coefficients of Lagrange interpolating Polynomials.
lagran.m 1. MATLAB stores polynomials by storing their coefficients in a row vector ( 3 x 1 matrix) Hence the polynomial x2 + 2x +3 would be stored as the vector [1 2 3] » P = [1 2 3] P = 1 2 3 2. MATLAB has a several routines that allows one to manipulate polynomials. We can evaluate polynomials at a particular point with the command polyval The following evaluates the polynomial with coefficients stored in P above at the value x = 2: » polyval(P,2) ans = 11 3. The conv command can be used to multiply two polynomial. For example, to multiply the above polynomial by (x + 1) -- i.e. to compute (x2 + 2x + 3) (x + 1): » Q = [1 1] Q = 1 1 » conv(P,Q) ans = 1 3 5 3 ( The result is thus x3+ 3x2 + 5x + 3. ) 4. The MATLAB Symbolic Toolbox also has some commands for manipulating polynomials symbolically. The poly2sym command converts a vector of coefficients to a symbolic polynomial. » syms x » poly2sym(P,x) ans = x^2+2*x+3 5. To find the polynomial of degree n that passes through n + 1 data points we can use polyfit. » help polyfit To find the polynomial passing through the points in Example 4.4, page 200: » X = [1 2 3 5] » Y = [1.06 1.12 1.34 1.78] » P = polyfit(X,Y,3) P = -0.0200 0.2000 -0.4000 1.2800 Thus the polynomial fitting these points is -0.02x3 +0.2x2-0.4x +1.28. You can see that MATLAB produces the correct coefficients agreeing with your text.
thanks :)
you welcome
Join our real-time social learning platform and learn together with your friends!