MATLAB - forming an array of sums?

  • Thread starter peripatein
  • Start date
  • #1
880
0
Hi,

Homework Statement


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.

Homework Equations





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.
 

Answers and Replies

  • #2
35,019
6,770
Hi,

Homework Statement


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.

Homework Equations





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.

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:
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:
  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:
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.
 

Related Threads on MATLAB - forming an array of sums?

  • Last Post
Replies
6
Views
4K
  • Last Post
Replies
3
Views
1K
Replies
2
Views
1K
  • Last Post
Replies
1
Views
1K
Replies
4
Views
2K
Replies
2
Views
6K
Replies
13
Views
2K
  • Last Post
Replies
5
Views
890
Top