Hermite interpolation using MATLAB



MATLAB Program:

% Hermite interpolation
 % Find the approximate value of f(1.5) from
 % (x,y,y')= (0,1,1), (1,e,e), (2,e^2,e^2) & (3,e^3,e^3).

 n = input('Enter n for (n+1) nodes, n:  ');
 x = zeros(1,n+1);
 q = zeros(2*n+2,2*n+2);

 for i = 0:n   
   fprintf('Enter x(%d) and f(x(%d)), and f''(x(%d)) on separate lines:  \n', i, i, i);                
   x(i+1) = input(' ');
   q(2*i+1,1) = input(' ');
   q(2*i+2,2) = input(' ');
 end            
 
 z = zeros(1,2*n+2);
 for i = 0:n
    z(2*i+1) = x(i+1);
    z(2*i+2) = x(i+1);
    q(2*i+2,1) = q(2*i+1,1);
    if i ~= 0
       q(2*i+1,2) = (q(2*i+1,1)-q(2*i,1))/(z(2*i+1)-z(2*i));
    end
 end

 k = 2*n+1;
 for i = 2:k
    for j = 2:i
       q(i+1,j+1) = (q(i+1,j)-q(i,j))/(z(i+1)-z(i-j+1));
    end
 end

 fprintf('\nCoefficients of the Hermite polynomial are:\n ');
 for i = 0:k
    fprintf(' %11.8f \n', q(i+1,i+1));
 end

 fprintf('Now enter a point at which to evaluate the polynomial, x = \n');
 xx = input(' ');
 s = q(k+1,k+1)*(xx-z(k));
 for i = 2:k
    j = k-i+1;
    s = (s+q(j+1,j+1))*(xx-z(j));
 end
 s = s + q(1,1);
 fprintf('The interpolated value is:  %11.8f \n\n',s);





OUTPUT:

>> Hermite_interpolation
Enter n for (n+1) nodes, n:  3
Enter x(0) and f(x(0)), and f'(x(0)) on separate lines:
 0
 1
 1
Enter x(1) and f(x(1)), and f'(x(1)) on separate lines:
 1
 2.7
 2.7
Enter x(2) and f(x(2)), and f'(x(2)) on separate lines:
 2
 7.29
 7.29
Enter x(3) and f(x(3)), and f'(x(3)) on separate lines:
 3
 19.68
 19.68

Coefficients of the Hermite polynomial are:
   1.00000000
  1.00000000
  0.70000000
  0.30000000
  0.07250000
  0.05500000
 -0.01694444
  0.02185185
Now enter a point at which to evaluate the polynomial, x =
 1.5
The interpolated value is:   4.43082031

>>




No comments