matlab help
%% Problem 9.8 %{ One interesting preoperty of a Fibonacci sequence is that the ratio of the values of adjacent members of the sequence aproaches a number valled "the golden ratio" or (phi). Create a program that accepts the first two additonal values in the sequence as user input and then calculates additional values in the sequence until the ratio of adjacent values converges to within 0.001. You can do this in a while loop by comparing the ratio of element k to element k - 1 and the ratio of element k - 1 to element k - 2. If you call your sequence x, then the code for the while statement is while abs(x(k)/x(k-1)-x(k-1)/x(k-2))>0.001 %}
@jim_thompson5910
how much do you have written?
nothing. I didn't really know where to start. Let me work on it for a bit. I made a mini program before on the Fib sequence.
`Let me work on it for a bit` alright, take your time
@jim_thompson5910 I don't undertand what it is asking
ok so the first thing I'd do is write the code where you get your inputs these inputs will help set up the fibonacci sequence
`Create a program that accepts the first two additonal values in the sequence as user input`
N = input('Enter first value: '); fib = zeros(1, N); fib(1) = 1; fib(2) = 2; k = 3; while k <= N fib(k) = fib(k-1) + fib(k-2); k = k + 1; end fprintf('The Fibonacci sequence to %d terms is \n', N); fprintf('%0.f', fib); fprintf('\n');
ok that code looks like it will work
But this is for a single input
btw you don't have to have `fib = zeros(1, N);` at least I think you don't
am I heading in the right direction here?
% asking user to input any number N1 = input('Enter first value: '); N2 = input('Enter second value: '); fib(1) = N1; fib(2) = N2; k = 3; fib(k) = fib(k-1) + fib(k-2); while abs(fib(k)/fib(k-1)-fib(k-1)/fib(k-2))>0.001 fib(k) = fib(k-1) + fib(k-2); k = k + 1; end disp(fib)
% ask the user for the starting 2 values % the user should type in 1, then 2. fib(1) = input('Enter first value: '); fib(2) = input('Enter second value: '); % in all honesty, this shouldn't be an input section since the fib sequence doesn't change. % The starting values are always the same. % as long as the difference between the adjacent ratios is greater % than 0.001, keep generating terms k = 3; while diff > 0.001 fib(k) = fib(k-1) + fib(k-2); diff = fib(k)/fib(k-1) - fib(k-1)/fib(k-2); k = k + 1; end fprintf('The Fibonacci sequence to %d terms is \n', k); fprintf('%0.f', fib); fprintf('\n');
Reading and studying what you displayed
I like what you posted better.
You should find that `fib(k)/fib(k-1)` will approach the golden ratio phi (as k approaches infinity) https://www.mathsisfun.com/numbers/golden-ratio.html http://prntscr.com/b6xji4
actually I see why the teacher wants an input section. The only problem is that the right inputs must be typed in or else it won't work. For instance, input1 = 1 input2 = 2 or input1 = 5 input2 = 8 are valid input combos but input1 = 5 input2 = 10 is invalid
ignore the comment where I wrote `% the user should type in 1, then 2. ` you can start at any point in the fib sequence as long as you type in numbers that belong to the sequence and those numbers are adjacent (second input being larger)
so fib(1) and fib(2) won't work since I need to assign an input() func to them because fib(1) = 1 and fib(2) = 2 won't work
^That means I would have to enter value 1 or 2
when you type in `fib(1) = 1` it says there's an error?
I have not tested. I was fixing my code
but you said `because fib(1) = 1 and fib(2) = 2 won't work`
It was more of a question. if you assign fib(1) and fib(2) I thought this doesn't allow the user to input values beyond 2
you can add another input line if you need more inputs
true or I could just do this % asking user to input any number N1 = input('Enter first value: '); N2 = input('Enter second value: '); fib(1) = N1; fib(2) = N2; k = 3;
yes that works too. It seems a bit wasteful though because N1 isn't used for anything else. So why not just take out the middleman and connect the two sections directly?
so go like this fib(1) = input('... fib(2) = input('....
your way isn't wrong, it just takes up more lines than needed
yes you can write it like that
so what does the 1 and 2 represent? Induces or the program knowing you put a value in for the first prompt and second prompt?
you mean fib(1) what the 1 represents?
yeah
fib(1) represents the first value in the fib vector fib(2) represents the second value in the fib vector you do not need to create a blank fib vector because you can add on values by simply writing the next bit of code this bit of code fib(k) = fib(k-1)+fib(k-2) will sets up the kth term based on the previous two terms. Again we don't need to set up a blank vector. This allows the fib vector to get as big as we need (as long as the memory can hold it of course)
Oh okay
% asking user to input any number fib(1) = input('Enter first value: '); fib(2) = input('Enter second value: '); k = 3; while diff > 0.001 fib(k) = fib(k-1) + fib(k-2); diff = fib(k)/fib(k-1) - fib(k-1)/fib(k-2); k = k + 1; end fprintf('The Fibonacci sequence to %d terms is \n', k); fprintf('%0.f', fib); fprintf('\n');
it gives me an error Enter first value: 1 Enter second value: 2 Error using diff Not enough input arguments.
hmm let me think
this is what my matlab says Error: "diff" previously appeared to be used as a function or command, conflicting with its use here as the name of a variable. A possible cause of this error is that you forgot to initialize the variable, or you have initialized it implicitly using load or eval.
why did we remove the abs() function? Don't we need it?
hmm I messed up and forgot about the abs
yes we need it
okay
Putting the abs() I still get an error Enter first value: 1 Enter second value: 2 Error using diff Not enough input arguments.
ok I got it to work
here is the m file and the command output as two separate files
I changed each instance of `diff` to `difference`. Apparently Matlab has a function named `diff` for differentiation I also changed the line `fprintf('%0.f', fib);` to `fprintf('%0.f, ', fib);` I added in a comma and a space to make the formatting much cleaner
why did we have to assign diff = 10?
The fib sequence Matlab spits out is 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 Take the ratios of the terms 89/55 = 1.61818181818181 55/34 = 1.61764705882352 then subtract 1.61818181818181 - 1.61764705882352 = 0.0005347593583 So we're within range
`why did we have to assign diff = 10?` when Matlab first sees the "difference" variable in the line while difference > 0.001 it has no idea what the value of "difference" is. So I just made up some number larger than 0.001 This is to set up the variable and define it to be some value. The value of "difference" is then recomputed to be some smaller decimal number
Okay makes sense. Thank you very much.
No problem. I'm not sure if it's me, but I have a feeling your copy of Matlab can handle "diff" just fine? and you don't have to change it to "difference" ?
I was actually going to just leave it or at least try to run it with diff instead of difference.
see if matlab throws an error if you have "diff"
Oh that is neat when you change %0.f to %0.f, it puts commas between each number
Okay it runs just fine now. I made diff = 5;
ok great
the only down side of putting comma after the f is that a commas appears after 89
it shows ....89,
It doesn't matter I will just leave it. It bothers me though.
yeah true. I'm not sure how to get rid of that pesky comma
No worries, I will just leave it.
Join our real-time social learning platform and learn together with your friends!