Reiteration sequence without Fib function.

7 次查看(过去 30 天)
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
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
emehmetcik 2015-9-18
It is a valuable number though, (sqrt(5)+1)/2, some say it's golden.

请先登录,再进行评论。

采纳的回答

Kirby Fears
Kirby Fears 2015-9-18
编辑:Kirby Fears 2015-9-18
Hi Ann,
Sounds like your function should take the following inputs: the first value of your sequence, the second value of your sequence, and the length of the sequence. The function should also return a vector "x" of iterative pair sums.
Be sure that this code is saved in a file name HW215_Ann.m. Then from the Matlab command window, you can type
x=HW215_Ann(1,2,30);
to call your function (use any inputs you like, i used 1, 2, and 30). The output "x" should then be in your workspace with the sequence you wanted.
Function below:
function x = HW215_Ann(f1,f2,n)
% f1 and f2 are the first two numbers of the sequence
% n is the length of the sequence
% Checking that function inputs are valid
if ~isnumeric(f1)||~isscalar(f1),
error('First input "f1" must be numeric scalar.');
end
if ~isnumeric(f2)||~isscalar(f2),
error('Second input "f3" must be numeric scalar.');
end
if ~isnumeric(n)||~isscalar(n),
error('Third input "n" must be numeric scalar.');
elseif (n~=round(n))||n<3,
error('Third input "n" must be an integer 3 or greater.')
end
% pre-allocate memory for x
x=NaN(n,1);
% start off with f1 and f2
x(1)=f1;
x(2)=f2;
% loop to calculate x values
for iter=3:n
x(iter)=x(iter-1)+x(iter-2);
end
Functions in Matlab must be saved as .m files with the same name as the as the function's name. Also, you won't need to use return statements in Matlab. The objects you want to return should only be stated in the function declaration. Give this link a read-through.
  2 个评论
Ann Gabele
Ann Gabele 2015-9-18
Ah, so I see what you did there. My loop was not working properly because of my x. So was it trying to store a vector the way I did it and it had no space to put it? This piece plus the part two is supposed to generate my final sequence and I want to make sure I am understanding my mistakes. Thanks.
Kirby Fears
Kirby Fears 2015-9-18
Your function declaration was not formed properly before. You were probably trying to run the function as if it is a script, which doesn't work as intended in this case.
Just be sure to follow the documentation linked above next time you want to create a function. To use your function, call it by name from another script or function, or in the command window as indicated in my answer.

请先登录,再进行评论。

更多回答(2 个)

the cyclist
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 个评论
the cyclist
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
Ann Gabele 2015-9-18
编辑:Ann Gabele 2015-9-18
I am using a recursion relation to define the Fibonacci sequence. X(0)=1, X(n)=c
x(n+1)=X(n)+X(n-1) for n>=1 when c is set to (1+sqrt(5))/2, it is supposed to give the closed form formula X(n)=((1+sqrt(5))/2)^n Since Matlab has to start from 1, that means x(1)=1;X(2)=c; and X(3)=2.6180 Maybe I am mis-interpreting the problem? There are three parts, and the second uses the negative and the third joins them together somehow.

请先登录,再进行评论。


John D'Errico
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
Ann Gabele 2015-9-18
I am not looking for Fibonacci NUMBERS, I am looking to build a SEQUENCE for the given iteration and value c which is what I have to work with. (see my last comment on the first answer). If my code is working as it should be, then I will have to figure it out on my own which I am okay with, I just thought I was coding wrong as I have never worked with this program and we have to teach it to ourselves.
John D'Errico
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Entering Commands 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by