MATLAB - forming an array of sums?

In summary, the person posting is asking for help with evaluating a sum that approximates the function e^x with different accuracy. They have provided code that does not work and are asking for guidance on how to correct it. The suggested solution involves storing the 10 partial sums in an array and using nested for loops to calculate each partial sum. A separate function, fact(), is also used in the solution.
  • #1
peripatein
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.
 
Physics news on Phys.org
  • #2
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.
 

1. What is an array of sums in MATLAB?

An array of sums in MATLAB is a matrix or vector that contains the cumulative sums of the elements in a given array. This can be achieved using the cumsum function in MATLAB.

2. How do I form an array of sums in MATLAB?

To form an array of sums in MATLAB, you can use the cumsum function. This function takes in an array as its input and returns a new array with the cumulative sums of the elements in the original array.

3. Can I specify the direction of the cumulative sum in MATLAB?

Yes, you can specify the direction of the cumulative sum in MATLAB by using the cumsum function with the optional argument dim. This argument allows you to specify the dimension along which you want the cumulative sum to be calculated.

4. How can I visualize an array of sums in MATLAB?

You can visualize an array of sums in MATLAB by using the plotting function plot or stem. These functions can be used to plot the cumulative sums over time or against a specific variable in the array.

5. Can I use the array of sums in further calculations?

Yes, you can use the array of sums in further calculations in MATLAB. The array of sums can be treated as any other array and can be used in mathematical operations or passed as an argument to other functions.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
735
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
696
  • Engineering and Comp Sci Homework Help
Replies
2
Views
742
  • Engineering and Comp Sci Homework Help
Replies
1
Views
902
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
898
  • Engineering and Comp Sci Homework Help
Replies
1
Views
896
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
3
Replies
80
Views
8K
Back
Top