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

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

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

    : P
    Thanks.
     
  2. jcsd
  3. jamesrc

    jamesrc 477
    Science Advisor
    Gold Member

    Re: Recursion

    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.
     
Know someone interested in this topic? Share this thead via email, Google+, Twitter, or Facebook

Have something to add?
Similar discussions for: Need help creating a recursive script in matlab that can do this:
Loading...