From: Mario Fatafehi on
My nested for loop can carry out the geometric sum okay but what I want is only the total final answer of the whole summation. How can I add to my code to add up and give me just the final answer (total summation) rather than intermediate values?
My code is shown below.

for k = 2:1:7
for t = 0:1:5
Y = k*t^2;
end
end
From: Steve Amphlett on
"Mario Fatafehi" <coruba9(a)hotmail.com> wrote in message <hof97h$qqk$1(a)fred.mathworks.com>...
> My nested for loop can carry out the geometric sum okay but what I want is only the total final answer of the whole summation. How can I add to my code to add up and give me just the final answer (total summation) rather than intermediate values?
> My code is shown below.
>
> for k = 2:1:7
> for t = 0:1:5
> Y = k*t^2;
> end
> end

Your code doesn't make a lot of sense, there is no summation. What answer were you expecting?
From: Mario Fatafehi on
"Steve Amphlett" <Firstname.Lastname(a)Where-I-Work.com> wrote in message <hofepn$lvq$1(a)fred.mathworks.com>...
> "Mario Fatafehi" <coruba9(a)hotmail.com> wrote in message <hof97h$qqk$1(a)fred.mathworks.com>...
> > My nested for loop can carry out the geometric sum okay but what I want is only the total final answer of the whole summation. How can I add to my code to add up and give me just the final answer (total summation) rather than intermediate values?
> > My code is shown below.
> >
> > for k = 2:1:7
> > for t = 0:1:5
> > Y = k*t^2;
> > end
> > end
>
> Your code doesn't make a lot of sense, there is no summation. What answer were you expecting?


I am expecting about 1485 as the final answer (total summation). I am not sure how to code in the adding up so I can get the total summation rather than just the individual sum.
From: Jan Simon on
Dear Mario!

> > > for k = 2:1:7
> > > for t = 0:1:5
> > > Y = k*t^2;
> > > end
> > > end
> >
> I am expecting about 1485 as the final answer (total summation). I am not sure how to code in the adding up so I can get the total summation rather than just the individual sum.

To get the total, you have to add the individual numbers.

S = 0;
for k = 2:7
for t = 1:5
S = S + k*t^2;
end
end

In addition I've simplified the code a little bit:
2:1:7 ==> 2:7
and t = 0 can be omitted.

Of course this can be vectorized also, but perhaps speed is not your problem.
Good luck, Jan
From: James Allison on
How about:

sum(sum((2:7)'*(1:5).^2))

-James

Jan Simon wrote:
> Dear Mario!
>
>> > > for k = 2:1:7
>> > > for t = 0:1:5
>> > > Y = k*t^2;
>> > > end
>> > > end
>> > I am expecting about 1485 as the final answer (total summation). I
>> am not sure how to code in the adding up so I can get the total
>> summation rather than just the individual sum.
>
> To get the total, you have to add the individual numbers.
>
> S = 0;
> for k = 2:7
> for t = 1:5
> S = S + k*t^2;
> end
> end
>
> In addition I've simplified the code a little bit:
> 2:1:7 ==> 2:7
> and t = 0 can be omitted.
>
> Of course this can be vectorized also, but perhaps speed is not your
> problem.
> Good luck, Jan