# MATLAB - forming an array of sums?

1. Oct 19, 2013

### peripatein

Hi,
1. The problem statement, all variables and given/known data
I am asked to evaluate the following sum S=Sigma(n=0 to N) x^n/n! (namely, e^x as n->Inf) for N=10:10:100 and x=10, so that every element S(i) is a partial sum which approximates function e^x with different accuracy. Below is my code, which doesn't work.

2. Relevant equations

3. The attempt at a solution

x=10;
N=10;
i=0;
while (N<=100),
S(i)=double(symsum(x^n/sym('n!'), n, 0, N));
i=i+1;
N=N+10;
end

Would anyone please tell me where I am going wrong and how it may be corrected? I'd appreciate some guidance.

2. Oct 19, 2013

### Staff: Mentor

You're trying to do too much all at once. Since you want 10 different partial sums, you could store the 10 partial sums in an array with 10 elements, arranged like this:
S1 = $\sum_{n = 0}^{10}a_nx^n$
S2 = $\sum_{n = 0}^{20}a_nx^n$
.
.
.
S10 = $\sum_{n = 0}^{100}a_nx^n$

You can do this with a for loop nested inside an outer for loop. The outer for loop might look like this:
Code (Text):
for n = 1:10
S(n) = 0.0
// rest of body of for loop
end

The inner for loop would calculate a particular partial sum.
Code (Text):
for j = 0:10*n
S(n) =S(n) + x^j/fact(j)
end

My code uses a function named fact() in a separate M-file named fact.m. It would look like this:
Code (Text):
function r = fact(n)
if (n == 0 || n == 1)
r = 1
else
temp = 1
for j = 1:n
temp = temp * j
end
end
r = temp

I don't have matlab, so can't say that I've tested anything here, but it should give you some ideas about what you need to do.