Results 1 to 6 of 6

Math Help - Basic Matlab help. Can't seem to grasp basic concept

  1. #1
    Newbie
    Joined
    Feb 2010
    Posts
    5

    Basic Matlab help. Can't seem to grasp basic concept

    I need help understanding these two questions. I have managed to do all of my assignments in Matlab without using/understanding these type of problems but they are on my midterm review. I have the answers but can't seem to grasp how to arrive at them. I understand how to use while and for loops in the codes that I have written, but can't figure out the answers to these questions. Help with either one would be greatly appreciated!
    Question #1
    The output of the following code is? Answer is sum=18

    n=0;
    sum=0;
    while n<10
    sum=sum+n;
    n= n+3;
    end
    fprintf('sum= %d \n', sum)


    Question #2
    What is the final value for y in the code below? Answer is -49
    Also, how many times will the 'for' loop run? Answer is 7

    Here is the code:
    y=0;
    for x= 1:2:13
    y=y-x;
    end



    Any help will be greatly appreciated. My textbook is pretty useless and I cannot figure these out.
    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 roryman987 View Post
    I need help understanding these two questions. I have managed to do all of my assignments in Matlab without using/understanding these type of problems but they are on my midterm review. I have the answers but can't seem to grasp how to arrive at them. I understand how to use while and for loops in the codes that I have written, but can't figure out the answers to these questions. Help with either one would be greatly appreciated!
    Question #1
    The output of the following code is? Answer is sum=18

    n=0;
    sum=0;
    while n<10
    sum=sum+n;
    n= n+3;
    end
    fprintf('sum= %d \n', sum)
    Look at the following trace table for the while loop:

    Code:
    n_ on_entry       sum    n_on_exit
          0            0          3
          3            3          6
          6            9          9
          9           12         12
         12 - end loop
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Feb 2010
    Posts
    5
    So the n=n+3 means that n is always (0+3) within the range given for n? Shouldn't this command come before the sum command for the sum to be properly calculated? Either way, I still don't see how the sum would ever equal 18??? It seems that the sum portion of the equation would always be 0 so the highest value would be the highest value of n, which can't be 18.
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Newbie
    Joined
    Feb 2010
    Posts
    5
    Wow I'm stupid. I have #1 figured out for the most part. I wasn't following the loop the way I should have been, but now it makes perfect sense. Thanks for the chart captain! That got my mind heading in the right direction.

    I do have one concern. How can you assume that the first value of n is 3 when calculating the first sum if the n=n+3 is AFTER the sum equation.



    Any advice on tackling #2? Does the 1:2:13 mean to go from 1-13 in intervals of 2? If so, I understand it perfectly.
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by roryman987 View Post
    Wow I'm stupid. I have #1 figured out for the most part. I wasn't following the loop the way I should have been, but now it makes perfect sense. Thanks for the chart captain! That got my mind heading in the right direction.

    I do have one concern. How can you assume that the first value of n is 3 when calculating the first sum if the n=n+3 is AFTER the sum equation.
    Look at the trace table, The first value of n used in the sum is 0 not 3, it is reassigned the value 3 after being used in sum.

    CB
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by roryman987 View Post
    Wow I'm stupid. I have #1 figured out for the most part. I wasn't following the loop the way I should have been, but now it makes perfect sense. Thanks for the chart captain! That got my mind heading in the right direction.

    I do have one concern. How can you assume that the first value of n is 3 when calculating the first sum if the n=n+3 is AFTER the sum equation.



    Any advice on tackling #2? Does the 1:2:13 mean to go from 1-13 in intervals of 2? If so, I understand it perfectly.
    The for loop assigns x the value 1 on the first pass, then 1+2=3 on the second pass, 3+2=5 on the third pass, then 7, 9, 11, 13.

    That is the control variable for the loop "x", loops over the elements of the vector [1:2:13]=[1,3,5,7,9,11,13]

    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Replies: 3
    Last Post: May 3rd 2011, 12:01 AM
  2. Basic Concept in Analysis
    Posted in the Differential Geometry Forum
    Replies: 1
    Last Post: January 13th 2011, 06:29 PM
  3. question about basic concept of hausdorff measure.
    Posted in the Differential Geometry Forum
    Replies: 0
    Last Post: April 5th 2010, 12:49 AM
  4. Basic Probability concept
    Posted in the Statistics Forum
    Replies: 1
    Last Post: September 8th 2009, 12:21 AM
  5. Request feedback on basic integration concept
    Posted in the Calculus Forum
    Replies: 4
    Last Post: May 24th 2006, 05:20 AM

Search Tags


/mathhelpforum @mathhelpforum