1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: MATLAB - forming an array of sums?

  1. Oct 19, 2013 #1
    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

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

    Would anyone please tell me where I am going wrong and how it may be corrected? I'd appreciate some guidance.
  2. jcsd
  3. Oct 19, 2013 #2


    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
    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)

    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
        temp = 1
        for j = 1:n
          temp = temp * j
     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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted