1
$\begingroup$

I am intending to use NestWhile in finding a solution with random real values as parameter.

NestWhile[{b2, b3, k1, d2, d3, k2, g2, k3} = RandomReal[2, 8], 
 RR1 = Solve[{F11 == 0 && F12 == 0 && F13 == 0 && F21 == 0 && 
     F22 == 0 && F23 == 0 && F31 == 0 && F32 == 0 && F33 == 0}, {a0, 
    a1, b1, c0, c1, d1, f0, f1, g1}], 
 Element[{a0 /. RR1[[1]]}, Reals] == False &, 1, 5]

However, when I test my code (at most 5 iterations), I found that each iteration has the same random real values.

I was wondering that how can I modify the code such that I can get the different random real values for different iteration within the NestWhile loop.

Thanks for your help in advance!

$\endgroup$
3
  • $\begingroup$ The code you posted does not evaluate without giving a message. $\endgroup$
    – Jason B.
    Commented Sep 10, 2020 at 15:43
  • $\begingroup$ Maybe you should use a While loop. Doing assignments to global state inside functional constructs is dodgy. Try this pastebin.com/UGSCdUzY - it should be close to what you need. $\endgroup$
    – flinty
    Commented Sep 10, 2020 at 16:16
  • $\begingroup$ @mikado What's wrong with pastebin? We often advise people to post code that's too long to that site. $\endgroup$
    – Michael E2
    Commented Sep 10, 2020 at 17:07

1 Answer 1

1
$\begingroup$

Here is a simple example, showing how you can get a different random number on each iteration of a NestWhile. Note that the assignment to a uses delayed evaluation (:= rather than =)

a := RandomReal[]

NestWhile[Append[#, a] &, {}, Length[#] < 10 &]
(* {0.358648, 0.262051, 0.597893, 0.654873, 0.20104, 0.389616, \
0.764424, 0.0774252, 0.63444, 0.58011} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.