Printing two variables

Thread Starter

monkeyhead

Joined Mar 5, 2007
45
Rich (BB code):
clear

n = 10;

F = Fib(n);

% Create, Fn a (10x2) array
Fn = [(1:n)' F];
% Print Fn to command window
fprintf('\n Fib number F_%g = %g \n',Fn')

%Part C
F_ratio = F(2:n)./F(1:n-1);
F_r = [(1:n-1)' F_ratio];

%Part D

phi = (1+(sqrt(5)))/2;


fprintf('\n Fib number F_ratio_%g = %g \n',F_r')
abs_diff = abs(phi - F_r);
fprintf('\n Absolute difference between F_ratio_%g and golden mean = %g \n', abs_diff')
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:

blazedaces

Joined Jul 24, 2008
130
Rich (BB code):
clear

n = 10;

F = Fib(n);

% Create, Fn a (10x2) array
Fn = [(1:n)' F];
% Print Fn to command window
fprintf('\n Fib number F_%g = %g \n',Fn')

%Part C
F_ratio = F(2:n)./F(1:n-1);
F_r = [(1:n-1)' F_ratio];

%Part D

phi = (1+(sqrt(5)))/2;


fprintf('\n Fib number F_ratio_%g = %g \n',F_r')
abs_diff = abs(phi - F_r);
fprintf('\n Absolute difference between F_ratio_%g and golden mean = %g \n', abs_diff')
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
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
 

Thread Starter

monkeyhead

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

Rich (BB code):
for j=1,
    fprintf('\n Fib number F_ratio_%g = %g \n',F_r')
    fprintf('\n Absoulute difference %g = %g \n',abs_diff')
end
:(:confused:
 

beenthere

Joined Apr 20, 2004
15,819
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.
 

blazedaces

Joined Jul 24, 2008
130
I tried incorporating a for loop but still running into the same issues.

Rich (BB code):
for j=1,
    fprintf('\n Fib number F_ratio_%g = %g \n',F_r')
    fprintf('\n Absoulute difference %g = %g \n',abs_diff')
end
:(:confused:
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
 

Thread Starter

monkeyhead

Joined Mar 5, 2007
45
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
 

blazedaces

Joined Jul 24, 2008
130
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
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
Rich (BB code):
for j=1:9,
    fprintf('\n Fib number F_ratio_%g = %g \n',F_r')
    fprintf('\n Absoulute difference %g = %g \n',abs_diff')
end
Your code could do something like:
Rich (BB code):
for j=1:9,
    fprintf('\n Fib number F_ratio_%g = %g \n',F_r(j)')
    fprintf('\n Absoulute difference %g = %g \n',abs_diff(j)')
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)...
 

Thread Starter

monkeyhead

Joined Mar 5, 2007
45
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:

Rich (BB code):
 Fib number F_ratio_1 = 
 Absoulute difference 0.618034 = 

 Fib number F_ratio_2 = 
 Absoulute difference 0.381966 = 

 Fib number F_ratio_3 = 
 Absoulute difference 1.38197 = 

 Fib number F_ratio_4 = 
 Absoulute difference 2.38197 = 

 Fib number F_ratio_5 = 
 Absoulute difference 3.38197 = 

 Fib number F_ratio_6 = 
 Absoulute difference 4.38197 = 

 Fib number F_ratio_7 = 
 Absoulute difference 5.38197 = 

 Fib number F_ratio_8 = 
 Absoulute difference 6.38197 = 

 Fib number F_ratio_9 = 
 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
 

blazedaces

Joined Jul 24, 2008
130
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
 
Top