Path: news.mathworks.com!not-for-mail From: <HIDDEN> Newsgroups: comp.soft-sys.matlab Subject: Re: Summing numbers in an M file in MATLAB Date: Fri, 13 May 2011 02:42:05 +0000 (UTC) Organization: The MathWorks, Inc. Lines: 20 Message-ID: <iqi5pt$f47$1@newscl01ah.mathworks.com> References: <iqh4s8$em7$1@newscl01ah.mathworks.com> Reply-To: <HIDDEN> NNTP-Posting-Host: www-02-blr.mathworks.com Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: newscl01ah.mathworks.com 1305254525 15495 172.30.248.47 (13 May 2011 02:42:05 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Fri, 13 May 2011 02:42:05 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 1187260 Xref: news.mathworks.com comp.soft-sys.matlab:726516 "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