Results 1 to 7 of 7

Math Help - Matlab while statement problem

  1. #1
    Junior Member
    Joined
    May 2008
    Posts
    50

    Matlab while statement problem

    I'm trying to write a simple program that computes the sum of the series 1^2 + 2^2 + 3^2... such that the sum is as large as possible without exceeding 1000. I need to use a while loop in this program. Here's my code so far:

    clear;
    number=0;
    sum_vector=[];
    End_flag=0

    while End_flag==0;
    number=number+1
    square=number.^2
    sum_vector(number)=square
    sum=sum(sum_vector)
    if sum>=1000
    End_flag=1;
    end
    end


    So my basic idea here was to place each number squared into an array, then sum the array and check to see if it makes the sum condition (<= 1000) and if not go up and plug the next number into the array etc. But this doesn't work and I'm not sure why. Any pointers?

    Thanks,
    Kim
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by Kim Nu View Post
    I'm trying to write a simple program that computes the sum of the series 1^2 + 2^2 + 3^2... such that the sum is as large as possible without exceeding 1000. I need to use a while loop in this program. Here's my code so far:

    clear;
    number=0;
    sum_vector=[];
    End_flag=0

    while End_flag==0;
    number=number+1
    square=number.^2
    sum_vector(number)=square
    sum=sum(sum_vector)
    if sum>=1000
    End_flag=1;
    end
    end


    So my basic idea here was to place each number squared into an array, then sum the array and check to see if it makes the sum condition (<= 1000) and if not go up and plug the next number into the array etc. But this doesn't work and I'm not sure why. Any pointers?

    Thanks,
    Kim
    Well for a start do not use the name of a function for a variable also, it may be legal but it is bad practice.

    Also you do not need an array with the squares, you just need a running total of the sum of squares, try something like:

    Code:
     
    SumOfSquares=0
    Num=0;
     
    while Num<1000
    
      work=SumOfSquares+(Num+1)*(Num+1);
      if work>1000
        break
      end
      SumOfSquares=work;
      Num=Num+1;
    
    end
    CB
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Junior Member
    Joined
    May 2008
    Posts
    50
    Hey Captain Black,

    Thanks alot for the help. I've just been introduced to while statements in Matlab and the only instruction I've received so far has been to start and stop the while statements using flags. But your solution looks alot better. I'm also not familiar with the "break" statement you put in your if statement. Is this condition what stopped the while statement?

    Kim
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by Kim Nu View Post
    Hey Captain Black,

    Thanks alot for the help. I've just been introduced to while statements in Matlab and the only instruction I've received so far has been to start and stop the while statements using flags. But your solution looks alot better. I'm also not familiar with the "break" statement you put in your if statement. Is this condition what stopped the while statement?

    Kim
    Yes, a break statement imediatly terminates a while or for loop.

    type "help break" at the command prompt or look break up in the online documentation.



    CB
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Or using flags:

    Code:
     
    SumOfSquares=0;
    Num=0;
    flg=0;
     
    while flg==0
     
      Num=Num+1;
      SumOfSquares=SumOfSquares+Num*Num;
      if SumOfSquares>1000
        flg=1
      end
    
    end
     
    SumOfSquares=SumOfSquares-Num*Num;
    Num-Num-1;
    Not as neat since the real test is now done at the top of the loop abd so we have to remove the last term from the answer after we exit

    CB
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Junior Member
    Joined
    May 2008
    Posts
    50
    CaptainBlack,

    You're the best. Just out curiosity, could the program be written with my original idea of summing an array and then proceeding or stopping the while loop?

    Kim
    Follow Math Help Forum on Facebook and Google+

  7. #7
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by Kim Nu View Post
    CaptainBlack,

    You're the best. Just out curiosity, could the program be written with my original idea of summing an array and then proceeding or stopping the while loop?

    Kim
    Yes, but there is no point all you ever look at is the sum so far. Also continually extending the matrix is a very slow process, it would be better if you cound pre declare it (1000 elements will allways be adequate).

    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Matlab LSQ problem
    Posted in the Math Software Forum
    Replies: 2
    Last Post: June 22nd 2010, 02:52 AM
  2. matlab problem
    Posted in the Math Software Forum
    Replies: 7
    Last Post: October 28th 2009, 08:09 AM
  3. MatLab IVP Problem
    Posted in the Math Software Forum
    Replies: 5
    Last Post: August 22nd 2009, 05:29 AM
  4. Matlab Problem For Help!
    Posted in the Math Software Forum
    Replies: 0
    Last Post: June 16th 2009, 05:15 PM
  5. MATLAB Problem
    Posted in the Math Software Forum
    Replies: 2
    Last Post: January 20th 2009, 06:42 AM

Search Tags


/mathhelpforum @mathhelpforum