%% ME 2016 %% Fall 2007 %% %% AUTHOR: Chris Paredis %% %% PURPOSE: plot the function Et(x,n) as defined in Task2 of HW1 for values of %% x=50 and n ranging from 0 through 200. %% %% ASSUMPTIONS: %% %% %% Problem formulation: %% % it is sometimes a good idea to clear the workspace to make sure you are % not inadvertently using old results clear all; % deletes all the variables in the workspace close all; % closes all the open figures % define the problem and initialize the memory n = 0:200; x = 50; % create a vector of the same size as x Et = zeros(size(n)); %% %% Problem solution: %% % Since the function taylor_error_cjp only works for scalars, we have to evaluate % it inside a loop for i=1:length(n) Et(i)=taylor_error_cjp(x,n(i)); end %% %% Problem interpretation: %% % plot the results in a graph semilogy(n,abs(Et)); grid on; title('ME2016: Function from Task 2, HW1 -- Chris Paredis'); xlabel('n'); ylabel('|Et|'); % display the results in a table % you could use a simple display command: format long e disp([n', Et']); % or you could create a more elegantly formatted table with headings: fprintf('+-----------+-------------------------+\n'); fprintf('| n (order) | Et |\n'); fprintf('+-----------+-------------------------+\n'); for i=1:length(Et) fprintf('| %3d | %23.15e |\n',n(i),Et(i)); end fprintf('+-----------+-------------------------+\n'); %% %% solution to questions in Task 4 %% %% Question 4.1: Why does the graph stop around n=180? % at n=182 to be exact, the computation of the power term becomes infinite: % 50^182 is larger than realmax and therefore results in Inf % However, notice that the result is not Inf but NaN. That is because the % denominator, factorial(n), is also larger than realmax, so that % Inf/Inf = NaN. % (note: factorial(n) already overflows (>realmax) at n=171, so that the % last 10 terms in the summation are zero: power_term/Inf = 0 %% Question 4.2: Why does the graph become flat at n=120? % We compute the absolute error, Et, by taking the difference between two % large numbers exp(50) and the Taylor series summation. Once we reach % n=115, the difference between exp(50) and the Taylor series approximation % becomes so small that there are no significant digits left after % computing the difference. All that is left is some round-off error. % Notice that the result is negative! This is surprising because the % truncated Taylor series is an underestimate of exp(x), so that Et should % always be positive. The computation of some of the the factorials must % have been underestimated (chopped off) so that some terms of the % summation were too large. % The curve remains flat after n=118 because the terms n=119 and above are % completely chopped off and do not contribute at all to the result. %% Question 4.3: Why are the values for n=0 through 4 exactly equal? % For very small values of n, the Taylor series summation result is such a % small value that when subtracted from exp(50) it is completely chopped % off. This is an inherent limitation of the floating point % representation. Unlike the two previous artifacts, it cannot be avoided.