Почему цикл while не выходит должным образом? - PullRequest
2 голосов
/ 05 июня 2019

enter image description here

G <- function(s)
{
    G <- (1/4) + (1/4)*s + (1/2)*s*s
}

SolveEq <- function(init)
{
    g <- -99

    if(init>0 && init<1)
    {
        g <- init
    }
    else
    {
        stop("The initial value must be between 0 and 1")
    }

    i <- 0
    x <- 0

    while(TRUE)
    {
         x <- G(g)

         if(x == g)
         {
             break
         }

         print(paste(i, ". ", x, ",", g))
         g <- x

         i = i + 1
    }

    print(i)
}

SolveEq(0.1)

выход

[1] "0 .  0.28 , 0.1"
[1] "1 .  0.3592 , 0.28"
[1] "2 .  0.40431232 , 0.3592"
[1] "3 .  0.432812306051891 , 0.40431232"
[1] "4 .  0.451866322647951 , 0.432812306051891"
[1] "5 .  0.465058167433679 , 0.451866322647951"
[1] "6 .  0.474404091406805 , 0.465058167433679"
[1] "7 .  0.48113064382346 , 0.474404091406805"
[1] "8 .  0.486026009168853 , 0.48113064382346"
[1] "9 .  0.489617143086514 , 0.486026009168853"
[1] "10 .  0.492266759173729 , 0.489617143086514"
[1] "11 .  0.494229970887135 , 0.492266759173729"
[1] "12 .  0.495689124783333 , 0.494229970887135"
[1] "13 .  0.496776135410067 , 0.495689124783333"
[1] "14 .  0.497587298208997 , 0.496776135410067"
[1] "15 .  0.498193384221714 , 0.497587298208997"
[1] "16 .  0.498646670096571 , 0.498193384221714"
[1] "17 .  0.498985918323342 , 0.498646670096571"
[1] "18 .  0.49923995292333 , 0.498985918323342"
[1] "19 .  0.499430253528277 , 0.49923995292333"
[1] "20 .  0.499572852451729 , 0.499430253528277"
[1] "21 .  0.49967973056631 , 0.499572852451729"
[1] "22 .  0.499759849210988 , 0.49967973056631"
[1] "23 .  0.499819915744442 , 0.499759849210988"
[1] "24 .  0.499864953023501 , 0.499819915744442"
[1] "25 .  0.499898723886468 , 0.499864953023501"
[1] "26 .  0.499924048043277 , 0.499898723886468"
[1] "27 .  0.499943038916808 , 0.499924048043277"
[1] "28 .  0.499957280809888 , 0.499943038916808"
[1] "29 .  0.499967961519881 , 0.499957280809888"
[1] "30 .  0.499975971653143 , 0.499967961519881"
[1] "31 .  0.499981979028538 , 0.499975971653143"
[1] "32 .  0.499986484433781 , 0.499981979028538"
[1] "33 .  0.499989863416671 , 0.499986484433781"
[1] "34 .  0.499992397613878 , 0.499989863416671"
[1] "35 .  0.499994298239307 , 0.499992397613878"
[1] "36 .  0.499995723695735 , 0.499994298239307"
[1] "37 .  0.499996792780945 , 0.499995723695735"
[1] "38 .  0.499997594590852 , 0.499996792780945"
[1] "39 .  0.499998195946032 , 0.499997594590852"
[1] "40 .  0.499998646961151 , 0.499998195946032"
[1] "41 .  0.499998985221779 , 0.499998646961151"
[1] "42 .  0.499999238916849 , 0.499998985221779"
[1] "43 .  0.499999429187926 , 0.499999238916849"
[1] "44 .  0.499999571891108 , 0.499999429187926"
[1] "45 .  0.499999678918422 , 0.499999571891108"
[1] "46 .  0.499999759188868 , 0.499999678918422"
[1] "47 .  0.49999981939168 , 0.499999759188868"
[1] "48 .  0.499999864543776 , 0.49999981939168"
[1] "49 .  0.499999898407842 , 0.499999864543776"
[1] "50 .  0.499999923805886 , 0.499999898407842"
[1] "51 .  0.499999942854418 , 0.499999923805886"
[1] "52 .  0.499999957140815 , 0.499999942854418"
[1] "53 .  0.499999967855612 , 0.499999957140815"
[1] "54 .  0.49999997589171 , 0.499999967855612"
[1] "55 .  0.499999981918782 , 0.49999997589171"
[1] "56 .  0.499999986439087 , 0.499999981918782"
[1] "57 .  0.499999989829315 , 0.499999986439087"
[1] "58 .  0.499999992371987 , 0.499999989829315"
[1] "59 .  0.49999999427899 , 0.499999992371987"
[1] "60 .  0.499999995709243 , 0.49999999427899"
[1] "61 .  0.499999996781932 , 0.499999995709243"
[1] "62 .  0.499999997586449 , 0.499999996781932"
[1] "63 .  0.499999998189837 , 0.499999997586449"
[1] "64 .  0.499999998642378 , 0.499999998189837"
[1] "65 .  0.499999998981783 , 0.499999998642378"
[1] "66 .  0.499999999236337 , 0.499999998981783"
[1] "67 .  0.499999999427253 , 0.499999999236337"
[1] "68 .  0.49999999957044 , 0.499999999427253"
[1] "69 .  0.49999999967783 , 0.49999999957044"
[1] "70 .  0.499999999758372 , 0.49999999967783"
[1] "71 .  0.499999999818779 , 0.499999999758372"
[1] "72 .  0.499999999864084 , 0.499999999818779"
[1] "73 .  0.499999999898063 , 0.499999999864084"
[1] "74 .  0.499999999923547 , 0.499999999898063"
[1] "75 .  0.499999999942661 , 0.499999999923547"
[1] "76 .  0.499999999956995 , 0.499999999942661"
[1] "77 .  0.499999999967747 , 0.499999999956995"
[1] "78 .  0.49999999997581 , 0.499999999967747"
[1] "79 .  0.499999999981857 , 0.49999999997581"
[1] "80 .  0.499999999986393 , 0.499999999981857"
[1] "81 .  0.499999999989795 , 0.499999999986393"
[1] "82 .  0.499999999992346 , 0.499999999989795"
[1] "83 .  0.49999999999426 , 0.499999999992346"
[1] "84 .  0.499999999995695 , 0.49999999999426"
[1] "85 .  0.499999999996771 , 0.499999999995695"
[1] "86 .  0.499999999997578 , 0.499999999996771"
[1] "87 .  0.499999999998184 , 0.499999999997578"
[1] "88 .  0.499999999998638 , 0.499999999998184"
[1] "89 .  0.499999999998978 , 0.499999999998638"
[1] "90 .  0.499999999999234 , 0.499999999998978"
[1] "91 .  0.499999999999425 , 0.499999999999234"
[1] "92 .  0.499999999999569 , 0.499999999999425"
[1] "93 .  0.499999999999677 , 0.499999999999569"
[1] "94 .  0.499999999999758 , 0.499999999999677"
[1] "95 .  0.499999999999818 , 0.499999999999758"
[1] "96 .  0.499999999999864 , 0.499999999999818"
[1] "97 .  0.499999999999898 , 0.499999999999864"
[1] "98 .  0.499999999999923 , 0.499999999999898"
[1] "99 .  0.499999999999942 , 0.499999999999923"
[1] "100 .  0.499999999999957 , 0.499999999999942"
[1] "101 .  0.499999999999968 , 0.499999999999957"
[1] "102 .  0.499999999999976 , 0.499999999999968"
[1] "103 .  0.499999999999982 , 0.499999999999976"
[1] "104 .  0.499999999999986 , 0.499999999999982"
[1] "105 .  0.49999999999999 , 0.499999999999986"
[1] "106 .  0.499999999999992 , 0.49999999999999"
[1] "107 .  0.499999999999994 , 0.499999999999992"
[1] "108 .  0.499999999999996 , 0.499999999999994"
[1] "109 .  0.499999999999997 , 0.499999999999996"
[1] "110 .  0.499999999999998 , 0.499999999999997"
[1] "111 .  0.499999999999998 , 0.499999999999998"
[1] "112 .  0.499999999999999 , 0.499999999999998"
[1] "113 .  0.499999999999999 , 0.499999999999999"
[1] "114 .  0.499999999999999 , 0.499999999999999"
[1] "115 .  0.499999999999999 , 0.499999999999999"
[1] "116 .  0.5 , 0.499999999999999"
[1] "117 .  0.5 , 0.5"
[1] "118 .  0.5 , 0.5"
[1] "119 .  0.5 , 0.5"
[1] "120 .  0.5 , 0.5"
[1] "121 .  0.5 , 0.5"
[1] "122 .  0.5 , 0.5"
[1] "123 .  0.5 , 0.5"
[1] 124

Цикл while должен был остановиться, когда i==117.

Что здесь происходит?

...