Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Jul 30, 2010 #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. Jul 30, 2010 #2

    jamesrc

    User Avatar
    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.
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook




Loading...
Similar Threads for Need help creating Date
MATLAB Help needed to fix a matlab code Nov 8, 2017
Need help with integral Dec 23, 2015
Maple Need help with maple programming Nov 20, 2015
MATLAB 2D diffusion equation, need help for matlab code. Nov 7, 2015
I need help with Excel Dec 12, 2014