function [ y ] = Tanh_2nd_Antiderivative( x )
% TANH_2ND_ANTIDERIVATIVE 
% Computes second antiderivative of tanh(x) from table.
% Function not optimized for real-time implementation.
% 
% Written by F. Esqueda June 41, 2016

load('f2_table.mat')
OS = 100;

% Find interpolation points
rd_in = floor(OS*abs(x));

if rd_in <= 1

    y = 0;

else

    % Sample points
    x1 = rd_in - 1;
    x2 = rd_in;
    x3 = rd_in + 1;
    x4 = rd_in + 2;

    % Fit cubic polynomial to those points
    A = vander([x1 x2 x3 x4]/OS);
    b = [f2_table(x1+1); f2_table(x2+1); f2_table(x3+1); f2_table(x4+1)];
    coeff = A\b;

    % Compute value of polynomial at 'xn'
    y = sign(x) * (coeff(1)*abs(x)^3 + coeff(2)*abs(x)^2 + coeff(3)*abs(x) + coeff(4));

end
    
end

