Search code examples
mathinterpolationscilab

Where is the problem in my Forward Difference Table using Scilab?


So, i have to interpolate function f(x) like this. x = 0:0.1:2.8; y = [0 0.717 0.999 0.675 0.0583 0.7568 0.9961 0.6312];

Here's the code i got at the moment.

clc
clear

x = 0:0.1:2.8;
y = [0 0.717 0.999 0.675 0.0583 0.7568 0.9961 0.6312];

n = length(x);
del = %nan * ones (n ,7) ;
    del (:,1) = y';
    for j = 2:7
        for i = 1: n - j +1
            del (i,j) = del(i+1,j-1) - del(i,j-1);
        end
    end
del = [x'del];
del = round ( del *10^3) /10^3;
mprintf ("%5s,%7s,%8s,%9s,%8s,%8s,%8s",'x','y','dy','d2y','d3y','d4y','d5y')
disp ( del )

and it's giving me Submatrix incorrectly defined error. Where could be the problem?


Solution

  • x and y should have the same length, but it is not the case with you data. For example, you can set

    y = [0 0.717 0.999 0.675 0.0583 0.7568 0.9961 0.6312];
    x = linspace(0,2.8,length(y));
    

    The line del = [x'del]; fails, it should be written as (a space is missing)

    del = [x' del];
    

    Then you script outputs the result:

         x,      y,      dy,      d2y,     d3y,     d4y,     d5y
       0.    0.      0.717  -0.435  -0.171   0.484   0.81   -5.487
       0.4   0.717   0.282  -0.606   0.313   1.295  -4.677   9.689
       0.8   0.999  -0.324  -0.293   1.608  -3.382   5.012   Nan  
       1.2   0.675  -0.617   1.315  -1.774   1.629   Nan     Nan  
       1.6   0.058   0.699  -0.459  -0.145   Nan     Nan     Nan  
       2.    0.757   0.239  -0.604   Nan     Nan     Nan     Nan  
       2.4   0.996  -0.365   Nan     Nan     Nan     Nan     Nan  
       2.8   0.631   Nan     Nan     Nan     Nan     Nan     Nan