% Computation of Taylor approximation of log(1+x) for different x
% and number of terms. The two methods are exactly the same, the only
% difference been that one is implemented with a for loop and the other
% with array syntax.
%
% Fernando Guevara Vasquez 2009

% number of terms in the Taylor approximation
ns=[5,10,20];
% x values to try
xs=[0.1,0.5,1];

% Compute Taylor approximations and store them in a array with rows
% being the x values and columns the number of terms. 
%
% note: It is always a good idea to first do the computations and worry
%       later about how to display the data.
%
% note: I have indices that ix and i that iterate through the array that
%       contains the values of x and n that I want to try, these indices are
%       used to store the result in the arrays lt1 and lt2 for later display
for ix=1:length(xs),
    x=xs(ix);
    for i=1:length(ns),
        n=ns(i);
        lt1(ix,i)=logtaylor1(x,n);
        lt2(ix,i)=logtaylor2(x,n);
    end;
end;

% Display the data as a nice table
% for more info on how to do this
%
%   http://math.utah.edu/~fguevara/math5610_f09/guidelines.html
%
% where there are some examples on how to use fprintf and what the
% formatting string means

% do the column headings
fprintf('   x=');
for ix=1:length(xs),
    fprintf('%10.2f %10.2f ',xs(ix),xs(ix));
end;
fprintf('\n');

% loop for each row
for i=1:length(ns), 
    n=ns(i);
    fprintf('n=%2d ',n);
    for ix=1:length(xs),
        x=xs(ix);
        fprintf('%10.5g %10.5g ',logtaylor1(x,n),...
            logtaylor2(x,n));
    end;
    fprintf('\n');
end;
