MATLAB - forming an array of sums?

  • Thread starter Thread starter peripatein
  • Start date Start date
  • Tags Tags
    Array Matlab Sums
Click For Summary
SUMMARY

The discussion focuses on evaluating the sum S=Σ(n=0 to N) x^n/n! to approximate e^x using MATLAB, specifically for N values ranging from 10 to 100 in increments of 10 and x=10. The original code provided by the user fails to execute correctly due to improper indexing and loop structure. A corrected approach involves using nested for loops to calculate and store the partial sums in an array, with a separate function for factorial calculation defined in a file named fact.m.

PREREQUISITES
  • Understanding of MATLAB programming and syntax
  • Familiarity with symbolic summation using symsum
  • Knowledge of factorial computation in programming
  • Basic concepts of series approximation and convergence
NEXT STEPS
  • Implement nested for loops in MATLAB for calculating partial sums
  • Develop a custom factorial function in MATLAB
  • Explore MATLAB's symbolic math toolbox for advanced summation techniques
  • Research series convergence and approximation methods for exponential functions
USEFUL FOR

Students and educators in mathematics and engineering, MATLAB programmers, and anyone interested in numerical methods for approximating functions.

peripatein
Messages
868
Reaction score
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.
 
Physics news on Phys.org
peripatein said:
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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
Replies
7
Views
3K
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 80 ·
3
Replies
80
Views
10K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K