Physics Forums
>
Other Sciences
>
Computing & Technology
>
Math & Science Software
> Recursion
PDA
View Full Version :
Recursion
darthxepher
Jul30-10, 02:35 AM
Need help creating a recursive script in matlab that can do this:
S = 1^2 + 2 ^2 + 3^2 + ... + n^2
: P
Thanks.
jamesrc
Jul30-10, 05:54 AM
This should do it:
S = sum((1:n).^2);
(1:n) creates an array of integers from 1 to n
the .^2 squares each element
and the sum() function does what it says.
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.