Printing two variables

Discussion in 'Programmer's Corner' started by monkeyhead, Dec 2, 2008.

  1. monkeyhead

    Thread Starter Active Member

    Mar 5, 2007
    45
    0
    Code ( (Unknown Language)):
    1. clear
    2.  
    3. n = 10;
    4.  
    5. F = Fib(n);
    6.  
    7. % Create, Fn a (10x2) array
    8. Fn = [(1:n)' F];
    9. % Print Fn to command window
    10. fprintf('\n Fib number F_%g = %g \n',Fn')
    11.  
    12. %Part C
    13. F_ratio = F(2:n)./F(1:n-1);
    14. F_r = [(1:n-1)' F_ratio];
    15.  
    16. %Part D
    17.  
    18. phi = (1+(sqrt(5)))/2;
    19.  
    20.  
    21. fprintf('\n Fib number F_ratio_%g = %g \n',F_r')
    22. abs_diff = abs(phi - F_r);
    23. fprintf('\n Absolute difference between F_ratio_%g and golden mean = %g \n', abs_diff')
    24.  
    Basically I'm trying to print the following :

    " Fib number F_ratio 1 = its ratio"
    " Absolute difference between F_ratio and Golden mean = its difference"

    And it should repeat for the remaining 8 times.

    However I'm getting the following displayed:

    " Fib number F_ratio 1 = its ratio"
    upto
    " Fib number F_ratio 9 = its ratio"

    Then

    " Absolute difference between F_ratio (its difference) and Golden mean = its difference here too"

    I'm not quite sure as where too I'm going wrong..

    Any help much appreciated

    Many thanks

    Matt
     
    Last edited: Dec 2, 2008
  2. blazedaces

    Active Member

    Jul 24, 2008
    130
    0
    Well... first of all... I don't see any loop, for or while. Do you know what those are? Otherwise, the program can't tell you want it to print more then exactly what it printed for you.

    It's only printing once for every command of fprint used...

    -blazed
     
  3. monkeyhead

    Thread Starter Active Member

    Mar 5, 2007
    45
    0
    I tried incorporating a for loop but still running into the same issues.

    Code ( (Unknown Language)):
    1. for j=1,
    2.     fprintf('\n Fib number F_ratio_%g = %g \n',F_r')
    3.     fprintf('\n Absoulute difference %g = %g \n',abs_diff')
    4. end
    :(:confused:
     
  4. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
    That should be an IF... Then loop. You either need to read up on how to test for the exit condition, like counting j up or down to match some value, or how set up a conditional loop - WHILE ... LOOP.
     
  5. monkeyhead

    Thread Starter Active Member

    Mar 5, 2007
    45
    0
    Thanks for the reply , butI'm completely lost about what your trying to say
     
  6. blazedaces

    Active Member

    Jul 24, 2008
    130
    0
    Yeah... I don't mean to sound insulting, but I don't think you quite understand what's happening when you're using a for loop.

    The for loop will run for every value of j. So by doing j = 1, it's only going to run once, for the value of j = 1. If you do j = 1: length(F) perhaps? Does that make sense to you?

    -blazed
     
  7. monkeyhead

    Thread Starter Active Member

    Mar 5, 2007
    45
    0
    No problem. I changed it to run 1:9 times which means it will run the loop for nine times right?

    The problem was it just repeated the whole issue 9 times, where as if I left it at 1 it just ran once with the issues ive been having.

    Many thanks
    MAtt
     
  8. blazedaces

    Active Member

    Jul 24, 2008
    130
    0
    I see, I see.

    Alright, this is what I think needs to be done: F_ratio and F_r or whatever second variable you use, are matrices right, containing the different 9 instances you want to show?

    For example, F_ratio(1) is the first one, F_ratio(2) is the second, etc.?

    So instead of
    Code ( (Unknown Language)):
    1. for j=1:9,
    2.     fprintf('\n Fib number F_ratio_%g = %g \n',F_r')
    3.     fprintf('\n Absoulute difference %g = %g \n',abs_diff')
    4. end
    Your code could do something like:
    Code ( (Unknown Language)):
    1. for j=1:9,
    2.     fprintf('\n Fib number F_ratio_%g = %g \n',F_r(j)')
    3.     fprintf('\n Absoulute difference %g = %g \n',abs_diff(j)')
    4. end
    That should fix the problem. Good luck,
    -blazed

    Edit: Remember, you know what your code is better than I, so you should adjust this for your specific case. For example, a quick look at your code indicates F_r is a 9x2 matrix, so instead of F_r(j) you need to do F_r(j,1)...
     
  9. monkeyhead

    Thread Starter Active Member

    Mar 5, 2007
    45
    0
    I made the modfication as suggest and it pretty much works to how I wanted it too. The only problem is that the output prints as follows:

    Code ( (Unknown Language)):
    1.  
    2.  
    3.  Fib number F_ratio_1 =
    4.  Absoulute difference 0.618034 =
    5.  
    6.  Fib number F_ratio_2 =
    7.  Absoulute difference 0.381966 =
    8.  
    9.  Fib number F_ratio_3 =
    10.  Absoulute difference 1.38197 =
    11.  
    12.  Fib number F_ratio_4 =
    13.  Absoulute difference 2.38197 =
    14.  
    15.  Fib number F_ratio_5 =
    16.  Absoulute difference 3.38197 =
    17.  
    18.  Fib number F_ratio_6 =
    19.  Absoulute difference 4.38197 =
    20.  
    21.  Fib number F_ratio_7 =
    22.  Absoulute difference 5.38197 =
    23.  
    24.  Fib number F_ratio_8 =
    25.  Absoulute difference 6.38197 =
    26.  
    27.  Fib number F_ratio_9 =
    28.  Absoulute difference 7.38197
    So the f_ratio is not being displayed? I'm not sure whether it has no value or whether I'm not calling the variable properly?

    Many thanks for the guidance so far,
    Matt
     
  10. blazedaces

    Active Member

    Jul 24, 2008
    130
    0
    Great. Mostly your task is complete. Now your problem is simply debugging your code. I would do it for you, but it's a simple skill that if you ever program is something useful to be able to do.

    You know you have an error with a certain variable (in this case F_r), so start by tracking that variable every step of the way. In your cause, you only really assign it a value once, when you create it, but sometimes that's not always the case.

    In your case, just display it after you made it, either by removing the semicolon or typing disp(F_r). That should show you all the values stored in the matrix. If you see nothing, then something is wrong with your assignment of the variable. Try doing the same thing in a different way or just looking to see if there's something wrong. May I ask why you have an apostrophe after the parenthesis?

    -blazed
     
Loading...