Interpolation of $\frac{1}{1+x^2}$ on Octave

206 Views Asked by At

I have used Chebyshev nodes to avoid Runge's phenomenon

x = −5 : 0.001 : 5; 
i = 0 : 10; 
xc = −1/2 ∗ 10∗cos((2 ∗ i + 1)/(2 ∗ 10 + 2) ∗ pi);
yc = 1./(1 + xc.ˆ2);
pc = polyfit(xc, yc, length(xc) − 1);
pyc = polyval(pc, x);
plot(x, pyc,'k')

This code works fine:output 1

But:

x = −5 : 0.001 : 5;
i = 0 : 50;
xc = −1/2 ∗ 10∗cos((2 ∗ i + 1)/(2 ∗ 10 + 2) ∗ pi);
yc = 1./(1 + xc.ˆ2);
pc = polyfit(xc, yc, length(xc) − 1);
pyc = polyval(pc, x);
plot(x, pyc,'k')

output 2

What is the problem in this case?


Thanks!

1

There are 1 best solutions below

0
On BEST ANSWER

Try this:

    N = 50;
    x = -5 : 0.001 : 5;
    y = 1./(1 + x.^2);
    p = polyfit(x, y, N-1 );
    yp = polyval(p, x); 
    i = 0 : N;
    xc = -1/2 * 10*cos((2 * i + 1)/(2 * N + 2) * pi); % changed
    yc = 1./(1 + xc.^2);
    pc = polyfit(xc, yc, N-1);
    pyc = polyval(pc, x);
    figure
        plot(x, pyc,'b', x, yp, 'r', xc, yc, '.k' )
        grid

Note the change in the line that starts with xc =. With MATLAB, I get this warning when N>14:

Warning: Polynomial is badly conditioned. Add points with distinct X values, reduce the
degree of the polynomial, or try centering and scaling as described in HELP POLYFIT.