Hi! I have a for loop that fills my vector with fibonacci numbers if random number from 0 to 1 is greater than 0,1. In cases when random number is less than 0,1, instead of adding the leading digits, it subtracts. So normally result looks like this:
1
1
2
3
5
8
13
5
18
23
But in some cases random number from 0 to 1 gives value less than 0,1 twice in a row and i get a table with fibonacci number <= 0 that looks like this:
1
1
2
3
1
-2
-1
-3
-4
-7
So i have two questions that i have not managed to solve.
  1. How to stop the loop when i get first negative value (in this case -2) and write a message about that?
  2. How to make this for loop automaticly go over and over again (implement it in while loop?) until i get a negative value and than stop it and then write this message? Is it possible to count how many times (while?) loop had to start before negative value was found?
n = 10;
limitf = 0.1;
F = zeros(n,1);
F(1) = 1;
F(2) = 1;
for k = 3:n
r = rand(1);
if r > limitf
F(k) = F(k-1) + F(k-2);
else
F(k) = F(k-1) - F(k-2);
end
end
disp(F)

 Accepted Answer

hello
I modified a bit your code so the condition were the two successive identical random r :
n = 10;
limitf = 0.1;
F = zeros(n,1);
F(1) = 1;
F(2) = 1;
r_old = -1;
for k = 3:n
r_old = r;
r = rand(1);
if r > limitf
F(k) = F(k-1) + F(k-2);
else
if abs(r-r_old)>eps
F(k) = F(k-1) - F(k-2);
end
end
end
disp(F)

6 Comments

Hi! Thank you for the answer. I don’t quite get it. Could you explain what are you using abs and eps for?
hello
so I modified the code so that in case r would be identical twice in a row , this case would be rejected
this portion of the code ensure that F(k) = F(k-1) - F(k-2); is executred only if r and r-old are differnt by an amount greater than eps
if abs(r-r_old)>eps
F(k) = F(k-1) - F(k-2);
end
r_old is the previous r value and we compare if to the current r value
instead of making a direct comparison like
if r == r_old
which can be sensitive to round off errors , it's much preferable to check that the difference (absolute value) is very low ; eps = 2.2204e-016
is a small number defined bu default in matlab , but of course you can define your own tolerance value
Oh, i see now :) I don´t really need r to be identical, but r to be bellow 0,1 twice in the row.
But still the best criteria is when we get first negative value written in the vector
1
1
2
3
1
-2
-1
-3
-4
-7
So when in this case -2 is written in the table, the loop should stop, write content of the vector, in this case it will be:
1
1
2
3
1
-2
0
0
0
0
and write a message
hello
I modified the code so it implements the requirement : the best criteria is when we get first negative value written in the vector
n = 10;
limitf = 0.1;
F = zeros(n,1);
F(1) = 1;
F(2) = 1;
for k = 3:n
r = rand(1);
if r > limitf
F(k) = F(k-1) + F(k-2);
else
F(k) = F(k-1) - F(k-2);
end
if F(k)<0
disp(F)
disp(['number of iterations ' num2str(k)]);
break
end
end
Thank you :)
My pleasure !

Sign in to comment.

More Answers (1)

Something like this perhaps:
fib = [1; 1];
its = 0;
keepgoing = true;
while keepgoing
its = its+1;
r = rand;
if r>0.1
fib(its+2) = fib(its+1) + fib(its);
else
fib(its+2) = fib(its+1) - fib(its);
end
if fib(its+2)<0
keepgoing = false;
end
end
disp(['number of iterations ' num2str(its)])

4 Comments

Hi, Alan! Thank you for your answer! What vould you do on question 1 (edit for loop so it stops when we get first negative value and write a message about that)?
The problem with your answer is that it expands matrice until it find this value, but original matrice was limited to 10 values in length. So the loop have to start again when desired negative value is not found.
Probably it will be easiest to find solution on question number 1 and stop at this point :) thank you for trying
The following restircts your vetor to 10 elements. Not clear to me if you want to start from 1 1 each time the 10 values are exceeded, or if you just want to carry on with the larger Fibonacci numbers. The following does the latter. It answers the first question automatically (the message I've printed just indicates the number of iterations. Replace with whatever message you want.)
fib = [1; 1];
its = 0;
keepgoing = true;
p1 = 1; p2 = 2; p3 = 3;
while keepgoing
its = its+1;
if p1>10
p1 = 1;
end
if p2 > 10
p2 = 1;
end
if p3 > 10
p3 = 1;
end
r = rand;
if r>0.1
fib(p3) = fib(p2) + fib(p1);
else
fib(p3) = fib(p2) - fib(p1);
end
if fib(p3)<0
keepgoing = false;
end
p1 = p1+1;
p2 = p2+1;
p3 = p3+1;
end
disp(['number of iterations ' num2str(its)])
The next loop have to start from 1 and 1 again :)

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2021a

Community Treasure Hunt

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

Start Hunting!