HW 3, problem 1.26

By Nasser Abbasi, PHY 240, feb 4, 2002

 

Part (a)

 

SOURCE code

 

function y=problem_1_26_part_a(x)

% FUNCTION y=problem_1_26_part_a(x)

% Plots the absolute fractional error of the sum of

% taylor series expansion for the exponential vs number

% of terms used in the taylor series.

%

% INPUT

%   x: the value to evaluate exp(x) at using this

%      taylor series expansion.

% OUTPUT

%   y: the value of exp(x) using taylor series expansion

%  

 

% by Nasser Abbasi, PHY 240, San Jose State Univ. Feb 4, 2002.

%

% notes:

% taylor series expansion for exp(x) is

% e(x) = 1 + x + x^2/2! + x^3/3! + ....+ x^n/n! = lim N->Inf S(x,N)

% where S(x,N) is the partial Sum with N+1 terms.

% The absolute fractional error of the sum is |S(x,N)-e^x)|/e^x

 

if nargin ~=1

    error('missing argument x');

end

 

% Max Number of terms to use in the taylor series expansion

maxNumberOfTerms = 60;

 

 

for i=1:maxNumberOfTerms

    sum=1;

    for j=1:i-1

        sum= sum + ( x^j / factorial(j) );

    end

    error(i) = abs(sum - exp(x)) / exp(x);   

end

 

y=sum;

plot(error);

xlabel('N, number of terms in the taylor series');

ylabel(' Absolute fractional error of the sum exp(x)');

title(' Absolute fractional error vs Number of terms for exp() estimation');

legend(sprintf('For x=%g',x));

 

 

OUTPUT

 

» clear all

» help problem_1_26_part_a

 

  FUNCTION y=problem_1_26_part_a(x)

  Plots the absolute fractional error of the sum of

  taylor series expansion for the exponential vs number

  of terms used in the taylor series.

 

  INPUT

    x: the value to evaluate exp(x) at using this

       taylor series expansion.

  OUTPUT

    y: the value of exp(x) using taylor series expansion

   

 

result for x=10

 

» y=problem_1_26_part_a(10)

 

y =

 

          22026.4657948067

 

 

result for x=2

 

» clear all

» y=problem_1_26_part_a(2)

 

y =

 

          7.38905609893065

 

result for x=-2

 

» clear all

» y=problem_1_26_part_a(-2)

 

y =

 

         0.135335283236613

 

»

 

 

 

result for x=-10

 

» y=problem_1_26_part_a(-10)

 

y =

 

     4.53999294340506e-005

 

 

 

 

Analysis

 

When X becomes more and more negative,  this shows that the error increases as we increase the number of terms in the taylor series (instead of decreasing as one might expect), this happens up to some N, but then the errors reverses, and starts to become smaller as the number of terms in the taylor series is increased until the error becomes almost zero for larger N.


 

Part (b)

Modify the program so that it uses the identity exp(x) = 1/exp(x) = 1/S(-x,Inf) to evaluate the exponential when x is negative.

 

Source code

 

function y=problem_1_26_part_c(x)

% FUNCTION y=problem_1_26_part_c(x)

% Plots the absolute fractional error of the sum of

% taylor series expansion for the exponential vs number

% of terms used in the taylor series.

%

% INPUT

%   x: the value to evaluate exp(x) at using this

%      taylor series expansion.

% OUTPUT

%   y: the value of exp(x) using taylor series expansion

%  

 

% by Nasser Abbasi, PHY 240, San Jose State Univ. Feb 4, 2002.

%

% notes:

% taylor series expansion for exp(x) is

% e(x) = 1 + x + x^2/2! + x^3/3! + ....+ x^n/n! = lim N->Inf S(x,N)

% where S(x,N) is the partial Sum with N+1 terms.

% The absolute fractional error of the sum is |S(x,N)-e^x)|/e^x

 

if nargin ~=1

    error('missing argument x');

end

 

% Max Number of terms to use in the taylor series expansion

maxNumberOfTerms = 60;

 

% use the identity exp(-x) = 1/exp(x) to evaluate exp when x is negative.

% so, when x is negative, evalute exp(x), then when done, find exp(-x) as

% 1/exp(x). This way we avoid having to use taylor series when x is

% negative, which introduces errors for low N.

 

SIGN=1;

 

if x<0

   SIGN=-1;

end

 

n_x = SIGN * x;

  

for i=1:maxNumberOfTerms

    sum=1;

    for j=1:i-1

        sum= sum + ( n_x^j / factorial(j) );

    end

   

    if SIGN == -1

       sum = 1/sum;

    end

   

    error(i) = abs(sum - exp(x)) / exp(x);   

end

 

y=sum;

plot(error);

xlabel('N, number of terms in the taylor series');

ylabel('Absolute fractional error of the sum exp(x), using identity.');

title(' Absolute fractional error vs Number of terms for exp() estimation');

legend(sprintf('For x=%g',x));

 

 

 

 

OUTPUT

 

 

» help problem_1_26_part_c

 

  FUNCTION y=problem_1_26_part_c(x)

  Plots the absolute fractional error of the sum of

  taylor series expansion for the exponential vs number

  of terms used in the taylor series.

 

  INPUT

    x: the value to evaluate exp(x) at using this

       taylor series expansion.

  OUTPUT

    y: the value of exp(x) using taylor series expansion

   

 

result for x=10

 

» clear all

» y=problem_1_26_part_c(10)

 

y =

 

          22026.4657948067

 

 

 

result for x=2

 

» clear all

» y=problem_1_26_part_c(2)

 

y =

 

          7.38905609893065

 

 

 

» clear all

» y=problem_1_26_part_c(-2)

 

y =

 

         0.135335283236613

 

 

 

» clear all

» y=problem_1_26_part_c(-10)

 

y =

 

     4.53999297624849e-005

 

 

 

 

Analysis

 

Using the identity exp(-x) = 1/exp(x) has eliminated the need to evaluate the taylor series expansion for x when x is negative. Since we only use X as positive, then we find exp(-x) using division as shown above.

 

This means the error between the real exp(x) and the above approximation at low number of terms has been reduced when x was negative compared to part(a) method.