0

I need to interpolate a function using polynomial interpolation, but I'm not sure how to code the part to get the polynomial and its coefficients:

enter image description here

My function:

fx = @(x) sin(3 .*x) ./ (0.4 .+ (x .-2) .^2);

My 9 sample values:

xs = 0:0.5:4;
ys = fx(xs);

My attempt:

n = 2;
x(1) = xs; # not sure if this is correct

for k=1:n-1

    for j=k+1:n

        p = ((x(1) - x(j)) / x(1) - x(j))* fx(x(j));
    endfor

endfor

But I feel really lost and I don't quite understand. Could someone help me out?

jadhachem
  • 1,123
  • 2
  • 11
  • 19
TheMortiestMorty
  • 421
  • 2
  • 4
  • 12
  • Is there a reason to try and perform the interpolation by hand instead of calling `interp1` with the method that you want? If the final argument is `'pp'` the function returns the piecewise polynomial, which you can evaluate with `ppval` or examine at will. – Javier Martín Mar 21 '16 at 00:14
  • I guess I'm not really sure. The instruction set reads: *Polynomial interpolation for n = 2, 4, 6, 8 (sampled accordingly at 3, 5, 7 and 9 points); see the formula at the end * [given in my question] * for computing the coefficients of the interpolating polynomial P(x) of degree n – you may find the function polyval useful for this.* I just kind of assumed that our professor wanted this done by hand. I might be wrong though. – TheMortiestMorty Mar 21 '16 at 00:22
  • Oh, well, if this is part of class work it's not illogical to have a requirement to do it manually. – Javier Martín Mar 21 '16 at 00:32
  • I agree. Any tips? – TheMortiestMorty Mar 21 '16 at 01:45
  • I guess you could try and think vectorially; for example the values of (Xk - Xj) can be obtained by matrix multiplication and then you can take the product of a whole row of that matrix (excluding the diagonal) to get each denominator. – Javier Martín Mar 21 '16 at 02:26

0 Answers0