Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Summing numbers in an M file in MATLAB

Subject: Summing numbers in an M file in MATLAB

From: J.C. Martinez

Date: 12 May, 2011 17:20:09

Message: 1 of 5

here's my code. I'm wondering what else I need to add to sum up all the numbers at the end.

a = input('Enter number of days you would like to know the total of');
f(1) = .01;
for i = 2:a
    f(i) = (2*f(i-1));
end

Would sum(sum(2:a)) work right before the "end"?

Subject: Summing numbers in an M file in MATLAB

From: Greg Heath

Date: 12 May, 2011 23:16:14

Message: 2 of 5

On May 12, 1:20 pm, "J.C. Martinez" <beatbox...@gmail.com> wrote:
> here's my code. I'm wondering what else I need to add to sum up all the numbers at the end.
>
> a = input('Enter number of days you would like to know the total of');
> f(1) = .01;
> for i = 2:a
>     f(i) = (2*f(i-1));
> end
>
> Would sum(sum(2:a)) work right before the "end"?

I think you want sum(f) after the end

Hope this helps.

Greg

Subject: Summing numbers in an M file in MATLAB

From: ImageAnalyst

Date: 13 May, 2011 02:20:22

Message: 3 of 5

How about this:
(You can do it a lot of ways but I'll introduce you to cumsum() which
sometimes comes in useful. For example you could compute the
cumulative sum just once and then ask lots and lots of times without
ever having to perform the sum anymore.)

% Ask user how many terms they want to sum up.
termsToSum = str2double(cell2mat(inputdlg('Enter number of days you
would like to know the total of')));

% Generate the array that we're going to sum up later.
f = zeros(1, termsToSum);
f(1) = .01;
% Note: loop use k instead of i (the imaginary variable).
for k = 2 : termsToSum
    f(k) = (2*f(k-1));
end
% Display it in the command window.
f

% Now calculate the sum from 1 through the ath term.
f_cdf = cumsum(f)
theSum = f_cdf(termsToSum);
fprintf('The sum of terms 1 through %d is %.2f.\n', ...
    termsToSum, theSum);

Subject: Summing numbers in an M file in MATLAB

From: Roger Stafford

Date: 13 May, 2011 02:42:05

Message: 4 of 5

"J.C. Martinez" wrote in message <iqh4s8$em7$1@newscl01ah.mathworks.com>...
> here's my code. I'm wondering what else I need to add to sum up all the numbers at the end.
>
> a = input('Enter number of days you would like to know the total of');
> f(1) = .01;
> for i = 2:a
> f(i) = (2*f(i-1));
> end
>
> Would sum(sum(2:a)) work right before the "end"?
- - - - - - - - - -
  In this particular problem there is no need to do summation of any kind. The sum of all the terms can be expressed as

 .01*(2^a-1)

which is a special case of taking sums of geometric series.

  Your problem sounds like the famous tale of a king trying to award a subject for supposedly devising the game of chess. The subject made what seemed like a modest request. One penny on the first square, two on the next, four on the third, and doubling on each successive square. Presumably the king would have had to file for bankruptcy if he had attempted to pay for all sixty-four squares.

Roger Stafford

Subject: Summing numbers in an M file in MATLAB

From: Steven_Lord

Date: 13 May, 2011 14:05:39

Message: 5 of 5



"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in
message news:iqi5pt$f47$1@newscl01ah.mathworks.com...
> "J.C. Martinez" wrote in message
> <iqh4s8$em7$1@newscl01ah.mathworks.com>...
>> here's my code. I'm wondering what else I need to add to sum up all the
>> numbers at the end. a = input('Enter number of days you would like to
>> know the total of');
>> f(1) = .01;
>> for i = 2:a
>> f(i) = (2*f(i-1));
>> end
>>
>> Would sum(sum(2:a)) work right before the "end"?
> - - - - - - - - - -
> In this particular problem there is no need to do summation of any kind.
> The sum of all the terms can be expressed as
>
> .01*(2^a-1)
>
> which is a special case of taking sums of geometric series.
>
> Your problem sounds like the famous tale of a king trying to award a
> subject for supposedly devising the game of chess. The subject made what
> seemed like a modest request. One penny on the first square, two on the
> next, four on the third, and doubling on each successive square.
> Presumably the king would have had to file for bankruptcy if he had
> attempted to pay for all sixty-four squares.

I agree; when I first read the OP's message I assumed this was for homework
for a combinatorics or discrete mathematics class. Although the original
version I remember heading dealt not with coins but with grains of rice, the
Wikipedia entry puts how much rice would be on the chessboard into
perspective.

http://en.wikipedia.org/wiki/Wheat_and_chessboard_problem

"On the entire chessboard there would be 2^64 1 =
18,446,744,073,709,551,615 grains of rice, weighing 461,168,602,000 metric
tons, which would be a heap of rice larger than Mount Everest."

Hope the inventor was hungry :)

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us