MATLAB Need help creating a recursive script in matlab that can do this:

  • Thread starter Thread starter darthxepher
  • Start date Start date
  • Tags Tags
    Matlab
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.
 

Similar threads

Replies
4
Views
1K
Replies
1
Views
2K
Replies
4
Views
4K
Replies
2
Views
2K
Replies
1
Views
1K
Replies
5
Views
3K
Replies
5
Views
2K
Back
Top