Recursive MATLAB script for sum of squares 1^2 + 2^2 + 3^2 + ... + n^2

  • Context: MATLAB 
  • Thread starter Thread starter darthxepher
  • Start date Start date
  • Tags Tags
    Matlab
Join the discussion
Registration is free. Start your own thread to ask a follow-up.
1 reply · 2K views
darthxepher
Messages
56
Reaction score
0
Need help creating a recursive script in MATLAB that can do this:

S = 1^2 + 2 ^2 + 3^2 + ... + n^2

: P
Thanks.
 
Physics news on Phys.org


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.