48 views (last 30 days)

Show older comments

Hi,

What is the problem with this code:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [S] = taylorTerm1(x,n)

x=1.1;

n=(1:1:5);

p=0;

for i=1:n

z = (1:2i+1:1);

iTerm = (-1)^(i)*(x)^2i+1/z;

S = p + iTerm;

end

fprintf('The Sum of the Series is %6.4f.\n',S);

I am trying to find the sum of series (-1)^n * (x^2n+1)/(2n+1)! and the factorial is confusing me.

Thanks

Geoff Hayes
on 18 Aug 2014

Jack - part of the problem may have to do with the equation. You state that you are trying to _find the sum of series (-1)^n * (x^2n+1)/(2n+1)! _. Do you really mean this to be

(-1)^n * (x^(2*n+1))/(2*n+1)!

where the exponent of x is 2*n+1? The line of code that is written as

(x)^2i+1/z;

means that the exponent of x is 2*i (I think it more likely that the exponent is considered as 0.0000 + 2.0000i - see below for details), and added to that result is 1/z when what you want instead is x to the power of 2*i+1 and that result divided by z.

The other problem, like you suggest, has to do with the factorial. Your code for this term is z and it is initialized as

z = (1:2i+1:1);

which is a vector of integers from 1 to 1 with a step-size of 2*i+1. So it will always be just one. Use the factorial function instead which will sum the product of the integers from 1,2,...,2*k+1.

This changes your code to

function [S] = taylorTerm1(x,n)

S = 0;

for k=1:n

kTerm = (-1)^(k)*(x^(2*k*+1))/factorial(2*k+1);

S = S + kTerm;

end

Note the differences. I've removed the initialization of

n=(1:1:5);

since this overwrites the input parameter with a vector. This will cause confusion for the line

for i=1:n

and will probably cause the iterations to terminate early.

I've replaced the i with k since MATLAB uses i and j to represent imaginary numbers. In your code, where there was 2i, this was probably being considered as the imaginary number 0.0000 + 2.0000i and not the desired 2*i.

The kterm has been reworked to something more like what you want, and the summation term was changed so that we were always summing S with the values from the previous iterations and the current iteration.

Try the above and see what happens!

Geoff Hayes
on 18 Aug 2014

Jack - factorial is a standard function within MATLAB so you should have this functionality. What is the error message if you try

factorial(4)

Also, in the Command Window, type

ver

which factorial -all

What are the results? (Please paste them in a comment.)

----------------

As for an alternative, yes, you could replace the factorial(2*k+1) with

prod(1:1:(2*k+1))

1:1:(2*k+1) = 1 2 3 4 5 ... 2*k 2*k+1

then

prod(1:1:(2*k+1)) = 1*2*3*4*5 ... *(2*k)*(2*k+1) = factorial(2*k+1)

Try the above and see what happens!

Roger Stafford
on 18 Aug 2014

Here is a method that doesn't require the 'factorial' function. See if you can figure out why this is equivalent to the formula you are using.

x = 1.1;

n = 10; % <-- Choose the number of terms to add

s = 1;

for k = 2*n:-2:2

s = 1 - x^2/k/(k+1)*s;

end

s = s*x;

You can compare your results to sin(x). Setting n = 10 is adequate for x values this size, but larger values of x would require larger values of n to compare successfully with sin(x).

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!