Reiteration sequence without Fib function.
显示 更早的评论
I am working on a recursive relation for the Fibonacci sequence without using the fib code in Matlab. My code works, but no matter what I do, I cannot get it to print more than the first three numbers.Any advice is appreciated.
function [x,n] = HW215(~)
x(1)=1;
x(2)=(1+sqrt(5))/2;
for n=3:31
x(n)=x(n-1)+x(n-2);
end
2 个评论
John D'Errico
2015-9-18
编辑:John D'Errico
2015-9-18
Hmm. Its been a long time, but I could swear that
x(2)=(1+sqrt(5))/2;
is NOT a Fibonacci number. Maybe Fibonacci was wrong. He has been dead for a few years. Must be that new math.
emehmetcik
2015-9-18
It is a valuable number though, (sqrt(5)+1)/2, some say it's golden.
采纳的回答
更多回答(2 个)
the cyclist
2015-9-18
Your code lists all 31 numbers for me.
Do you mean you want it to display more digits for each number? You could try typing
format long
before you run your code. Type
doc format
for the documentation.
3 个评论
Ann Gabele
2015-9-18
the cyclist
2015-9-18
Things I don't understand about your problem:
- Why you define x(2)=(1+sqrt(5))/2, which is not a Fibonacci number, as John D'Errico has deftly pointed out already
- What you mean by "displays wrong numbers". What specifically is different about what you see, compared to what you expect?
- What you mean by "proper numbers"
- What you mean by "the rest"
So, again, when I run your code, I see all the output, and the sequence is sensible (given your initial inputs).
Please realize the only insight we have into your issues is what you tell us. Perhaps you could go beyond tweet-length phrases to describe what you need.
I have one guess: Are you actually hoping to input n, to know what the nth Fibonacci number is? Because you have n as an output, not an input.
Ann Gabele
2015-9-18
编辑:Ann Gabele
2015-9-18
John D'Errico
2015-9-18
编辑:John D'Errico
2015-9-18
You have a MAJOR misunderstanding of the algorithm for fibonacci numbers.
First of all, F(2) is NOT (1+sqrt(5))/2. In fact, you claim that F(n) is in general
((1+sqrt(5))/2)^n
FLAT OUT WRONG. You seem to be wanting to use Binet's formula for Fibonacci numbers. In fact, F(2) would be 2 (Assuming that you start out at F(0) as 1.) The Binet formula has a second term in it though, that you seem to have missed. For larger values of n, that second term starts to get small, and if you round the result, your formula will actually work.
Regardless, WERE you to fix your code, so that
x(2) = 2;
then it should work, returning the first 30 or so Fibonacci numbers. For a fair amount of information on various ways to compute the Fibonacci numbers, you can look here.
By the way, your approximate version of the Binet formula is not actually correct anyway. Here is the correct Binet formula.
F(n) = ((1 + sqrt(5))^n - (1-sqrt(5))^n) / (2^n*sqrt(5))
That second term in there is quite important, at least for small values of n.
binet = @(n) ((1 + sqrt(5)).^n - (1-sqrt(5)).^n) ./ (2.^n*sqrt(5));
binet(1:10)
ans =
1 1 2 3 5 8 13 21 34 55
Next, I should point out that what you call the second Fibonacci number is actually binet(3) here. Be careful about the indexing. The Fibinacci sequence is usually described as being generated with the first two terms as [1 1]. So depending on your indexing scheme, you can get a different result.
That second term in the Binet formula is small. It is small for all non-negative values of n in fact.
n = 0:10; (1-sqrt(5)).^n./2.^n/sqrt(5)
ans =
0.44721 -0.27639 0.17082 -0.10557 0.065248 -0.040325 0.024922 -0.015403 0.0095195 -0.0058834 0.0036361
So, were you to round the result of an approximate Binet formula, it would in fact yield the desired sequence.
binet_approx = @(n) ((1 + sqrt(5))/2).^n/sqrt(5);
binet_approx(1:10)
ans =
0.72361 1.1708 1.8944 3.0652 4.9597 8.0249 12.985 21.01 33.994 55.004
round(binet_approx(1:10))
ans =
1 1 2 3 5 8 13 21 34 55
As long as you make sure you have the index correct.
2 个评论
Ann Gabele
2015-9-18
John D'Errico
2015-9-18
编辑:John D'Errico
2015-9-18
You are trying to compute a sequence of FIBONACCI numbers, something which you appear not to fully understand.
To quote your own words, you are "working on a recursive relation for the Fibonacci sequence". The use of c as you have done it will never generate the sequence of Fibonacci numbers, although that constant is closely related to that sequence.
The clue for you in all of this is the fact that your code as you wrote it fails to generate the sequence you claim to expect. As it turns out, if you start off the generating relation
x(n) = x(n-1) + x(n-2)
with some other set of values besides [1 1], then you will get some other sequence that is NOT the fibonacci sequence. [1 2] also works for the Fibonacci sequence, it just starts off one term ahead. However, suppose you start off the sequence with an initial pair as [1 3]?
Now we will generate the sequence [1 3 4 7 11 18 29 47...]. Just sum the previous two elements, and you get the next one. This is a related set of numbers: Lucas numbers, named after a mathematician named Lucas, so the Lucas numbers. It is not the Fibonacci sequence, but the Lucas sequence, closely related, but not the same. The same is true for what you have done.
If you start the recurrence off with the pair [1, (1+sqrt(5))/2] you will get some other sequence of numbers, which will not be an integer sequence. We could in fact derive a relation for that sequence as a function of n, using standard methods for linear difference equations. We can easily enough derive the general term as
x(n) = a*((1+sqrt(5))/2)^n + b*((1-sqrt(5))/2)^n
where a and b are unknown coefficients. In the case of the sequence you tried to generate, we can solve it using the symbolic toolbox, or paper and pencil would suffice.
syms a b
phi = sym('(1 + sqrt(5))/2')
pretty(phi)
sqrt(5) 1
------- + -
2 2
pretty(simplify(1/phi))
sqrt(5) 1
------- - -
2 2
ab = solve(a*phi + b/phi == 1,a*phi^2 + b/phi^2 == phi);
ab.a
ans =
2/(5^(1/2) + 1)
ab.b
ans =
0
So the general term for the sequence you apparently tried to generate is
x(n) = phi^n-1
We can call that the Ann sequence, for lack of a more creative name on my part.
phi = (1 + sqrt(5))/2;
Ann = @(n) phi.^(n-1);
Ann(1:10)
ans =
1 1.618 2.618 4.2361 6.8541 11.09 17.944 29.034 46.979 76.013
In fact, this is the sequence you managed to generate, though not the sequence you claim to have wanted to generate.
类别
在 帮助中心 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!