MATLAB question-sum of sequence

In summary, the problem is that the author wants to plot a sum of two infinite geometric series, but does not know how to do it. The solution is to use a for loop to sum the series, and to find the nearest n using logs.
  • #1
Bassalisk
947
2
So the problem is here:

https://www.physicsforums.com/showthread.php?p=3532161&posted=1#post3532161

And I understand the answer and all, but I want to go further. MATLAB will be main tool for upcoming years so I have to learn.

I want to plot this:

[itex]\sum 2^{i}= 500 000[/itex]

where sum goes from i=0 to n. n are number of iterations i am looking for. First how can make a combination of functions, that plots a sum. Next how can I plot that so its discreet, and only exists for 1 2 3 4...

So my graph contain x-axis which will be representing "i".

And my y-axis should represent the value of the sum. Ergo 1 3 7 15 etc. All up to something close to 500 000(I think this will come close to like 19 iterations)

EDIT: Can anybody show me purely mathematical way to solve the problem stated in the provided post.

[itex]\sum_{i=0}^n 2^{i}= 500 000[/itex]
 
Last edited:
Physics news on Phys.org
  • #2
The sum (geometric series) is 2n+1 - 1. You can find the nearest n using logs.
 
  • #3
mathman said:
The sum (geometric series) is 2n+1 - 1. You can find the nearest n using logs.

Thank you.

Anybody on the MATLAB thing yet?
 
  • #4
Bassalisk said:
Thank you.

Anybody on the MATLAB thing yet?

Here you go:
http://www.mathworks.com/help/techdoc/learn_matlab/f3-27853.html

While you could plot discrete points (see above), if you're going to span a large number of iterations (say, between 1 and 10,000), you should probably just plot a continuous (log) graph.

Now, as for a way to implement the summation, you should probably just a for loop. I'd start with your n_vector, make a twotothen_vector, and then use the for loop to sum. HINT: you do not need to sum from the first entry every single time. If none of these concepts makes sense, I'd start with an introductory programming book.

Implementation is left as an exercise to the reader / poster, following completion of the MATLAB getting started tutorial:
http://www.mathworks.com/help/techdoc/learn_matlab/bqr_2pl.html
 
Last edited by a moderator:
  • #5
MATLABdude said:
Here you go:
http://www.mathworks.com/help/techdoc/learn_matlab/f3-27853.html

While you could plot discrete points (see above), if you're going to span a large number of iterations (say, between 1 and 10,000), you should probably just plot a continuous (log) graph.

Now, as for a way to implement the summation, you should probably just a for loop. I'd start with your n_vector, make a twotothen_vector, and then use the for loop to sum. HINT: you do not need to sum from the first entry every single time. If none of these concepts makes sense, I'd start with an introductory programming book.

Implementation is left as an exercise to the reader / poster, following completion of the MATLAB getting started tutorial:
http://www.mathworks.com/help/techdoc/learn_matlab/bqr_2pl.html

Thank you! I will try this out. My next few year will be, literally LIVING on MATLAB. Expect more inquires from me. I am determined to get as best as i can in MATLAB.
 
Last edited by a moderator:

What is MATLAB and how is it used for calculating the sum of a sequence?

MATLAB is a programming language and software environment commonly used in scientific and engineering fields. It is used for a variety of tasks, including data analysis, visualization, and mathematical calculations. To calculate the sum of a sequence in MATLAB, you can use the built-in function "sum()" which takes an array as input and returns the sum of all elements in the array.

How do I input a sequence of numbers into MATLAB for sum calculation?

To input a sequence of numbers into MATLAB, you can use the square brackets notation, also known as the "concatenation operator". For example, if you want to input the sequence [1, 2, 3, 4], you can type [1, 2, 3, 4] in the command window and hit enter. This will create an array of numbers that can be used for sum calculation.

Can I use MATLAB to calculate the sum of a sequence with a specific pattern?

Yes, MATLAB has a powerful indexing feature that allows you to specify a pattern for selecting elements from an array. You can use this feature to select specific elements from a sequence and then calculate their sum using the "sum()" function. For example, if you want to calculate the sum of every other element in a sequence, you can use the indexing notation [1:2:end] to select every other element and then pass it to the "sum()" function.

Is there a way to store the sum of a sequence in a variable in MATLAB?

Yes, you can use the assignment operator "=" to store the sum of a sequence in a variable. For example, if you want to store the sum of [1, 2, 3, 4] in a variable "result", you can type "result = sum([1, 2, 3, 4])" in the command window. This will assign the value of 10 to the variable "result".

Are there any other functions in MATLAB that can be used for calculating the sum of a sequence?

Yes, besides the "sum()" function, MATLAB also has other functions that can be used for calculating the sum of a sequence, such as "cumsum()" which returns the cumulative sum of elements in an array and "trapz()" which calculates the integral of a sequence using the trapezoidal rule. These functions may be useful for more complex sequences or in specific applications.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
997
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
569
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
745
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top