%% Need not ship with the product but has to live in the CVS as reference file
<%- % build a largrange formula template using the generic formula for a particular degree polynomials. %>
<%- % develop a function template header %>
<%- 
    Ndegree = Ndegree + 1; %3 degree polynomial interp requires 4 points
    Xs = arrayfun( @(x) ['x',num2str(x)], 1:Ndegree, 'UniformOutput',false );
    Ys = arrayfun( @(x) ['y',num2str(x)], 1:Ndegree, 'UniformOutput',false );
    function_signature = cell(1,2*Ndegree+1);
    function_signature{1} = 'x';
    function_signature(2:2:end) = Xs;
    function_signature(3:2:end) = Ys;
    function_signature = strjoin(function_signature,', ');
%>
function Y = lagrange_interpolate_<% Ndegree %>(<% function_signature %>)
 Y = ...
 <%- for itr = 1:Ndegree %>
   <%- tmp_str = ''; %>
   <%- for itj = 1:Ndegree 
         if ( itj ~= itr )
             tmp_str = [tmp_str, '*', '(x - x',num2str(itj),')/(x',num2str(itr),' - x',num2str(itj),')'];
         end
       end
       tmp_str = tmp_str(2:end); %drop starting '*'
       %ends the inner for-loop %>

    <% %format the code %>
    <%- if ( itr == 1 )  %>
           y<% itr %> *<% tmp_str %> ...
    <%- elseif ( itr ~= Ndegree ) %>
           + y<% itr %> *<% tmp_str %> ...
    <%- else %when itr == Ndegree %>
           + y<% itr %> *<% tmp_str %>;
    <%- end %>
 <%- end %>
 return
end

