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

  • Context: MATLAB 
  • Thread starter Thread starter darthxepher
  • Start date Start date
  • Tags Tags
    Matlab
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
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.