-
-
Save EM5813/783d6d7c9d903a07daa73c77977f3b16 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"cur_x = 3 # The algorithm starts at x=3\n", | |
"rate = 0.01 # Learning rate\n", | |
"precision = 0.000001 #This tells us when to stop the algorithm\n", | |
"previous_step_size = 1 #\n", | |
"max_iters = 10000 # maximum number of iterations\n", | |
"iters = 0 #iteration counter\n", | |
"df = lambda x: 2*(x+5) #Gradient of our function" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Iteration 1 \n", | |
"X value is 2.84\n", | |
"Iteration 2 \n", | |
"X value is 2.6832\n", | |
"Iteration 3 \n", | |
"X value is 2.529536\n", | |
"Iteration 4 \n", | |
"X value is 2.37894528\n", | |
"Iteration 5 \n", | |
"X value is 2.2313663744\n", | |
"Iteration 6 \n", | |
"X value is 2.0867390469119997\n", | |
"Iteration 7 \n", | |
"X value is 1.9450042659737599\n", | |
"Iteration 8 \n", | |
"X value is 1.8061041806542846\n", | |
"Iteration 9 \n", | |
"X value is 1.669982097041199\n", | |
"Iteration 10 \n", | |
"X value is 1.5365824551003748\n", | |
"Iteration 11 \n", | |
"X value is 1.4058508059983674\n", | |
"Iteration 12 \n", | |
"X value is 1.2777337898784\n", | |
"Iteration 13 \n", | |
"X value is 1.152179114080832\n", | |
"Iteration 14 \n", | |
"X value is 1.0291355317992152\n", | |
"Iteration 15 \n", | |
"X value is 0.9085528211632309\n", | |
"Iteration 16 \n", | |
"X value is 0.7903817647399662\n", | |
"Iteration 17 \n", | |
"X value is 0.6745741294451669\n", | |
"Iteration 18 \n", | |
"X value is 0.5610826468562635\n", | |
"Iteration 19 \n", | |
"X value is 0.44986099391913825\n", | |
"Iteration 20 \n", | |
"X value is 0.3408637740407555\n", | |
"Iteration 21 \n", | |
"X value is 0.23404649855994042\n", | |
"Iteration 22 \n", | |
"X value is 0.1293655685887416\n", | |
"Iteration 23 \n", | |
"X value is 0.026778257216966764\n", | |
"Iteration 24 \n", | |
"X value is -0.07375730792737258\n", | |
"Iteration 25 \n", | |
"X value is -0.1722821617688251\n", | |
"Iteration 26 \n", | |
"X value is -0.2688365185334486\n", | |
"Iteration 27 \n", | |
"X value is -0.36345978816277963\n", | |
"Iteration 28 \n", | |
"X value is -0.45619059239952403\n", | |
"Iteration 29 \n", | |
"X value is -0.5470667805515336\n", | |
"Iteration 30 \n", | |
"X value is -0.6361254449405029\n", | |
"Iteration 31 \n", | |
"X value is -0.7234029360416929\n", | |
"Iteration 32 \n", | |
"X value is -0.8089348773208591\n", | |
"Iteration 33 \n", | |
"X value is -0.8927561797744419\n", | |
"Iteration 34 \n", | |
"X value is -0.9749010561789531\n", | |
"Iteration 35 \n", | |
"X value is -1.055403035055374\n", | |
"Iteration 36 \n", | |
"X value is -1.1342949743542665\n", | |
"Iteration 37 \n", | |
"X value is -1.2116090748671813\n", | |
"Iteration 38 \n", | |
"X value is -1.2873768933698377\n", | |
"Iteration 39 \n", | |
"X value is -1.361629355502441\n", | |
"Iteration 40 \n", | |
"X value is -1.4343967683923922\n", | |
"Iteration 41 \n", | |
"X value is -1.5057088330245443\n", | |
"Iteration 42 \n", | |
"X value is -1.5755946563640535\n", | |
"Iteration 43 \n", | |
"X value is -1.6440827632367725\n", | |
"Iteration 44 \n", | |
"X value is -1.711201107972037\n", | |
"Iteration 45 \n", | |
"X value is -1.7769770858125964\n", | |
"Iteration 46 \n", | |
"X value is -1.8414375440963444\n", | |
"Iteration 47 \n", | |
"X value is -1.9046087932144176\n", | |
"Iteration 48 \n", | |
"X value is -1.9665166173501292\n", | |
"Iteration 49 \n", | |
"X value is -2.0271862850031264\n", | |
"Iteration 50 \n", | |
"X value is -2.0866425593030637\n", | |
"Iteration 51 \n", | |
"X value is -2.1449097081170025\n", | |
"Iteration 52 \n", | |
"X value is -2.2020115139546625\n", | |
"Iteration 53 \n", | |
"X value is -2.257971283675569\n", | |
"Iteration 54 \n", | |
"X value is -2.312811858002058\n", | |
"Iteration 55 \n", | |
"X value is -2.3665556208420164\n", | |
"Iteration 56 \n", | |
"X value is -2.419224508425176\n", | |
"Iteration 57 \n", | |
"X value is -2.4708400182566725\n", | |
"Iteration 58 \n", | |
"X value is -2.521423217891539\n", | |
"Iteration 59 \n", | |
"X value is -2.570994753533708\n", | |
"Iteration 60 \n", | |
"X value is -2.619574858463034\n", | |
"Iteration 61 \n", | |
"X value is -2.667183361293773\n", | |
"Iteration 62 \n", | |
"X value is -2.713839694067898\n", | |
"Iteration 63 \n", | |
"X value is -2.75956290018654\n", | |
"Iteration 64 \n", | |
"X value is -2.804371642182809\n", | |
"Iteration 65 \n", | |
"X value is -2.8482842093391527\n", | |
"Iteration 66 \n", | |
"X value is -2.8913185251523696\n", | |
"Iteration 67 \n", | |
"X value is -2.9334921546493224\n", | |
"Iteration 68 \n", | |
"X value is -2.974822311556336\n", | |
"Iteration 69 \n", | |
"X value is -3.015325865325209\n", | |
"Iteration 70 \n", | |
"X value is -3.055019348018705\n", | |
"Iteration 71 \n", | |
"X value is -3.093918961058331\n", | |
"Iteration 72 \n", | |
"X value is -3.1320405818371646\n", | |
"Iteration 73 \n", | |
"X value is -3.1693997702004215\n", | |
"Iteration 74 \n", | |
"X value is -3.206011774796413\n", | |
"Iteration 75 \n", | |
"X value is -3.2418915393004846\n", | |
"Iteration 76 \n", | |
"X value is -3.277053708514475\n", | |
"Iteration 77 \n", | |
"X value is -3.3115126343441856\n", | |
"Iteration 78 \n", | |
"X value is -3.345282381657302\n", | |
"Iteration 79 \n", | |
"X value is -3.378376734024156\n", | |
"Iteration 80 \n", | |
"X value is -3.4108091993436727\n", | |
"Iteration 81 \n", | |
"X value is -3.4425930153567994\n", | |
"Iteration 82 \n", | |
"X value is -3.4737411550496633\n", | |
"Iteration 83 \n", | |
"X value is -3.50426633194867\n", | |
"Iteration 84 \n", | |
"X value is -3.534181005309697\n", | |
"Iteration 85 \n", | |
"X value is -3.563497385203503\n", | |
"Iteration 86 \n", | |
"X value is -3.5922274374994325\n", | |
"Iteration 87 \n", | |
"X value is -3.620382888749444\n", | |
"Iteration 88 \n", | |
"X value is -3.6479752309744553\n", | |
"Iteration 89 \n", | |
"X value is -3.675015726354966\n", | |
"Iteration 90 \n", | |
"X value is -3.7015154118278666\n", | |
"Iteration 91 \n", | |
"X value is -3.7274851035913095\n", | |
"Iteration 92 \n", | |
"X value is -3.7529354015194833\n", | |
"Iteration 93 \n", | |
"X value is -3.7778766934890937\n", | |
"Iteration 94 \n", | |
"X value is -3.8023191596193118\n", | |
"Iteration 95 \n", | |
"X value is -3.8262727764269258\n", | |
"Iteration 96 \n", | |
"X value is -3.8497473208983872\n", | |
"Iteration 97 \n", | |
"X value is -3.8727523744804193\n", | |
"Iteration 98 \n", | |
"X value is -3.895297326990811\n", | |
"Iteration 99 \n", | |
"X value is -3.917391380450995\n", | |
"Iteration 100 \n", | |
"X value is -3.939043552841975\n", | |
"Iteration 101 \n", | |
"X value is -3.9602626817851356\n", | |
"Iteration 102 \n", | |
"X value is -3.981057428149433\n", | |
"Iteration 103 \n", | |
"X value is -4.001436279586445\n", | |
"Iteration 104 \n", | |
"X value is -4.021407553994716\n", | |
"Iteration 105 \n", | |
"X value is -4.040979402914822\n", | |
"Iteration 106 \n", | |
"X value is -4.060159814856525\n", | |
"Iteration 107 \n", | |
"X value is -4.078956618559395\n", | |
"Iteration 108 \n", | |
"X value is -4.097377486188207\n", | |
"Iteration 109 \n", | |
"X value is -4.115429936464443\n", | |
"Iteration 110 \n", | |
"X value is -4.133121337735154\n", | |
"Iteration 111 \n", | |
"X value is -4.150458910980451\n", | |
"Iteration 112 \n", | |
"X value is -4.167449732760842\n", | |
"Iteration 113 \n", | |
"X value is -4.1841007381056246\n", | |
"Iteration 114 \n", | |
"X value is -4.200418723343512\n", | |
"Iteration 115 \n", | |
"X value is -4.216410348876642\n", | |
"Iteration 116 \n", | |
"X value is -4.2320821418991095\n", | |
"Iteration 117 \n", | |
"X value is -4.247440499061128\n", | |
"Iteration 118 \n", | |
"X value is -4.262491689079905\n", | |
"Iteration 119 \n", | |
"X value is -4.277241855298307\n", | |
"Iteration 120 \n", | |
"X value is -4.291697018192341\n", | |
"Iteration 121 \n", | |
"X value is -4.305863077828494\n", | |
"Iteration 122 \n", | |
"X value is -4.319745816271924\n", | |
"Iteration 123 \n", | |
"X value is -4.333350899946486\n", | |
"Iteration 124 \n", | |
"X value is -4.3466838819475555\n", | |
"Iteration 125 \n", | |
"X value is -4.359750204308605\n", | |
"Iteration 126 \n", | |
"X value is -4.372555200222433\n", | |
"Iteration 127 \n", | |
"X value is -4.385104096217984\n", | |
"Iteration 128 \n", | |
"X value is -4.3974020142936245\n", | |
"Iteration 129 \n", | |
"X value is -4.409453974007752\n", | |
"Iteration 130 \n", | |
"X value is -4.421264894527597\n", | |
"Iteration 131 \n", | |
"X value is -4.432839596637045\n", | |
"Iteration 132 \n", | |
"X value is -4.444182804704305\n", | |
"Iteration 133 \n", | |
"X value is -4.4552991486102185\n", | |
"Iteration 134 \n", | |
"X value is -4.466193165638014\n", | |
"Iteration 135 \n", | |
"X value is -4.4768693023252535\n", | |
"Iteration 136 \n", | |
"X value is -4.487331916278748\n", | |
"Iteration 137 \n", | |
"X value is -4.497585277953173\n", | |
"Iteration 138 \n", | |
"X value is -4.50763357239411\n", | |
"Iteration 139 \n", | |
"X value is -4.517480900946228\n", | |
"Iteration 140 \n", | |
"X value is -4.527131282927304\n", | |
"Iteration 141 \n", | |
"X value is -4.536588657268758\n", | |
"Iteration 142 \n", | |
"X value is -4.545856884123382\n", | |
"Iteration 143 \n", | |
"X value is -4.5549397464409145\n", | |
"Iteration 144 \n", | |
"X value is -4.563840951512097\n", | |
"Iteration 145 \n", | |
"X value is -4.572564132481855\n", | |
"Iteration 146 \n", | |
"X value is -4.581112849832218\n", | |
"Iteration 147 \n", | |
"X value is -4.589490592835574\n", | |
"Iteration 148 \n", | |
"X value is -4.597700780978863\n", | |
"Iteration 149 \n", | |
"X value is -4.605746765359285\n", | |
"Iteration 150 \n", | |
"X value is -4.6136318300521\n", | |
"Iteration 151 \n", | |
"X value is -4.621359193451058\n", | |
"Iteration 152 \n", | |
"X value is -4.628932009582036\n", | |
"Iteration 153 \n", | |
"X value is -4.636353369390395\n", | |
"Iteration 154 \n", | |
"X value is -4.643626302002588\n", | |
"Iteration 155 \n", | |
"X value is -4.650753775962536\n", | |
"Iteration 156 \n", | |
"X value is -4.657738700443285\n", | |
"Iteration 157 \n", | |
"X value is -4.664583926434419\n", | |
"Iteration 158 \n", | |
"X value is -4.671292247905731\n", | |
"Iteration 159 \n", | |
"X value is -4.6778664029476165\n", | |
"Iteration 160 \n", | |
"X value is -4.684309074888664\n", | |
"Iteration 161 \n", | |
"X value is -4.6906228933908904\n", | |
"Iteration 162 \n", | |
"X value is -4.696810435523073\n", | |
"Iteration 163 \n", | |
"X value is -4.702874226812612\n", | |
"Iteration 164 \n", | |
"X value is -4.708816742276359\n", | |
"Iteration 165 \n", | |
"X value is -4.714640407430832\n", | |
"Iteration 166 \n", | |
"X value is -4.720347599282215\n", | |
"Iteration 167 \n", | |
"X value is -4.725940647296571\n", | |
"Iteration 168 \n", | |
"X value is -4.731421834350639\n", | |
"Iteration 169 \n", | |
"X value is -4.736793397663627\n", | |
"Iteration 170 \n", | |
"X value is -4.742057529710355\n", | |
"Iteration 171 \n", | |
"X value is -4.747216379116147\n", | |
"Iteration 172 \n", | |
"X value is -4.752272051533824\n", | |
"Iteration 173 \n", | |
"X value is -4.757226610503148\n", | |
"Iteration 174 \n", | |
"X value is -4.762082078293084\n", | |
"Iteration 175 \n", | |
"X value is -4.766840436727223\n", | |
"Iteration 176 \n", | |
"X value is -4.771503627992678\n", | |
"Iteration 177 \n", | |
"X value is -4.776073555432824\n", | |
"Iteration 178 \n", | |
"X value is -4.780552084324168\n", | |
"Iteration 179 \n", | |
"X value is -4.784941042637685\n", | |
"Iteration 180 \n", | |
"X value is -4.7892422217849315\n", | |
"Iteration 181 \n", | |
"X value is -4.793457377349233\n", | |
"Iteration 182 \n", | |
"X value is -4.7975882298022485\n", | |
"Iteration 183 \n", | |
"X value is -4.801636465206204\n", | |
"Iteration 184 \n", | |
"X value is -4.805603735902079\n", | |
"Iteration 185 \n", | |
"X value is -4.809491661184038\n", | |
"Iteration 186 \n", | |
"X value is -4.813301827960357\n", | |
"Iteration 187 \n", | |
"X value is -4.81703579140115\n", | |
"Iteration 188 \n", | |
"X value is -4.820695075573127\n", | |
"Iteration 189 \n", | |
"X value is -4.824281174061665\n", | |
"Iteration 190 \n", | |
"X value is -4.827795550580431\n", | |
"Iteration 191 \n", | |
"X value is -4.831239639568823\n", | |
"Iteration 192 \n", | |
"X value is -4.834614846777447\n", | |
"Iteration 193 \n", | |
"X value is -4.837922549841898\n", | |
"Iteration 194 \n", | |
"X value is -4.84116409884506\n", | |
"Iteration 195 \n", | |
"X value is -4.844340816868159\n", | |
"Iteration 196 \n", | |
"X value is -4.847454000530796\n", | |
"Iteration 197 \n", | |
"X value is -4.85050492052018\n", | |
"Iteration 198 \n", | |
"X value is -4.853494822109776\n", | |
"Iteration 199 \n", | |
"X value is -4.85642492566758\n", | |
"Iteration 200 \n", | |
"X value is -4.859296427154229\n", | |
"Iteration 201 \n", | |
"X value is -4.862110498611145\n", | |
"Iteration 202 \n", | |
"X value is -4.864868288638922\n", | |
"Iteration 203 \n", | |
"X value is -4.867570922866143\n", | |
"Iteration 204 \n", | |
"X value is -4.87021950440882\n", | |
"Iteration 205 \n", | |
"X value is -4.872815114320644\n", | |
"Iteration 206 \n", | |
"X value is -4.875358812034231\n", | |
"Iteration 207 \n", | |
"X value is -4.877851635793546\n", | |
"Iteration 208 \n", | |
"X value is -4.880294603077676\n", | |
"Iteration 209 \n", | |
"X value is -4.882688711016122\n", | |
"Iteration 210 \n", | |
"X value is -4.8850349367958\n", | |
"Iteration 211 \n", | |
"X value is -4.887334238059884\n", | |
"Iteration 212 \n", | |
"X value is -4.8895875532986866\n", | |
"Iteration 213 \n", | |
"X value is -4.891795802232712\n", | |
"Iteration 214 \n", | |
"X value is -4.893959886188058\n", | |
"Iteration 215 \n", | |
"X value is -4.896080688464297\n", | |
"Iteration 216 \n", | |
"X value is -4.898159074695011\n", | |
"Iteration 217 \n", | |
"X value is -4.9001958932011105\n" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Iteration 218 \n", | |
"X value is -4.902191975337089\n", | |
"Iteration 219 \n", | |
"X value is -4.904148135830347\n", | |
"Iteration 220 \n", | |
"X value is -4.90606517311374\n", | |
"Iteration 221 \n", | |
"X value is -4.907943869651465\n", | |
"Iteration 222 \n", | |
"X value is -4.909784992258436\n", | |
"Iteration 223 \n", | |
"X value is -4.911589292413267\n", | |
"Iteration 224 \n", | |
"X value is -4.913357506565002\n", | |
"Iteration 225 \n", | |
"X value is -4.915090356433702\n", | |
"Iteration 226 \n", | |
"X value is -4.9167885493050285\n", | |
"Iteration 227 \n", | |
"X value is -4.918452778318928\n", | |
"Iteration 228 \n", | |
"X value is -4.920083722752549\n", | |
"Iteration 229 \n", | |
"X value is -4.921682048297498\n", | |
"Iteration 230 \n", | |
"X value is -4.923248407331548\n", | |
"Iteration 231 \n", | |
"X value is -4.9247834391849175\n", | |
"Iteration 232 \n", | |
"X value is -4.926287770401219\n", | |
"Iteration 233 \n", | |
"X value is -4.927762014993195\n", | |
"Iteration 234 \n", | |
"X value is -4.929206774693331\n", | |
"Iteration 235 \n", | |
"X value is -4.930622639199464\n", | |
"Iteration 236 \n", | |
"X value is -4.932010186415474\n", | |
"Iteration 237 \n", | |
"X value is -4.933369982687164\n", | |
"Iteration 238 \n", | |
"X value is -4.934702583033421\n", | |
"Iteration 239 \n", | |
"X value is -4.936008531372753\n", | |
"Iteration 240 \n", | |
"X value is -4.937288360745298\n", | |
"Iteration 241 \n", | |
"X value is -4.938542593530392\n", | |
"Iteration 242 \n", | |
"X value is -4.939771741659784\n", | |
"Iteration 243 \n", | |
"X value is -4.940976306826588\n", | |
"Iteration 244 \n", | |
"X value is -4.942156780690056\n", | |
"Iteration 245 \n", | |
"X value is -4.943313645076255\n", | |
"Iteration 246 \n", | |
"X value is -4.94444737217473\n", | |
"Iteration 247 \n", | |
"X value is -4.945558424731236\n", | |
"Iteration 248 \n", | |
"X value is -4.946647256236611\n", | |
"Iteration 249 \n", | |
"X value is -4.947714311111879\n", | |
"Iteration 250 \n", | |
"X value is -4.9487600248896415\n", | |
"Iteration 251 \n", | |
"X value is -4.949784824391848\n", | |
"Iteration 252 \n", | |
"X value is -4.950789127904011\n", | |
"Iteration 253 \n", | |
"X value is -4.951773345345931\n", | |
"Iteration 254 \n", | |
"X value is -4.952737878439012\n", | |
"Iteration 255 \n", | |
"X value is -4.953683120870232\n", | |
"Iteration 256 \n", | |
"X value is -4.954609458452827\n", | |
"Iteration 257 \n", | |
"X value is -4.955517269283771\n", | |
"Iteration 258 \n", | |
"X value is -4.956406923898095\n", | |
"Iteration 259 \n", | |
"X value is -4.957278785420133\n", | |
"Iteration 260 \n", | |
"X value is -4.958133209711731\n", | |
"Iteration 261 \n", | |
"X value is -4.958970545517496\n", | |
"Iteration 262 \n", | |
"X value is -4.959791134607146\n", | |
"Iteration 263 \n", | |
"X value is -4.960595311915003\n", | |
"Iteration 264 \n", | |
"X value is -4.9613834056767026\n", | |
"Iteration 265 \n", | |
"X value is -4.962155737563169\n", | |
"Iteration 266 \n", | |
"X value is -4.962912622811905\n", | |
"Iteration 267 \n", | |
"X value is -4.963654370355667\n", | |
"Iteration 268 \n", | |
"X value is -4.964381282948554\n", | |
"Iteration 269 \n", | |
"X value is -4.965093657289583\n", | |
"Iteration 270 \n", | |
"X value is -4.965791784143791\n", | |
"Iteration 271 \n", | |
"X value is -4.966475948460915\n", | |
"Iteration 272 \n", | |
"X value is -4.967146429491697\n", | |
"Iteration 273 \n", | |
"X value is -4.967803500901863\n", | |
"Iteration 274 \n", | |
"X value is -4.968447430883826\n", | |
"Iteration 275 \n", | |
"X value is -4.969078482266149\n", | |
"Iteration 276 \n", | |
"X value is -4.969696912620826\n", | |
"Iteration 277 \n", | |
"X value is -4.970302974368409\n", | |
"Iteration 278 \n", | |
"X value is -4.970896914881041\n", | |
"Iteration 279 \n", | |
"X value is -4.97147897658342\n", | |
"Iteration 280 \n", | |
"X value is -4.972049397051752\n", | |
"Iteration 281 \n", | |
"X value is -4.972608409110717\n", | |
"Iteration 282 \n", | |
"X value is -4.973156240928502\n", | |
"Iteration 283 \n", | |
"X value is -4.973693116109932\n", | |
"Iteration 284 \n", | |
"X value is -4.974219253787734\n", | |
"Iteration 285 \n", | |
"X value is -4.974734868711979\n", | |
"Iteration 286 \n", | |
"X value is -4.975240171337739\n", | |
"Iteration 287 \n", | |
"X value is -4.975735367910985\n", | |
"Iteration 288 \n", | |
"X value is -4.976220660552765\n", | |
"Iteration 289 \n", | |
"X value is -4.976696247341709\n", | |
"Iteration 290 \n", | |
"X value is -4.977162322394875\n", | |
"Iteration 291 \n", | |
"X value is -4.977619075946977\n", | |
"Iteration 292 \n", | |
"X value is -4.978066694428038\n", | |
"Iteration 293 \n", | |
"X value is -4.978505360539477\n", | |
"Iteration 294 \n", | |
"X value is -4.978935253328687\n", | |
"Iteration 295 \n", | |
"X value is -4.979356548262113\n", | |
"Iteration 296 \n", | |
"X value is -4.979769417296871\n", | |
"Iteration 297 \n", | |
"X value is -4.980174028950934\n", | |
"Iteration 298 \n", | |
"X value is -4.980570548371915\n", | |
"Iteration 299 \n", | |
"X value is -4.980959137404477\n", | |
"Iteration 300 \n", | |
"X value is -4.981339954656387\n", | |
"Iteration 301 \n", | |
"X value is -4.981713155563259\n", | |
"Iteration 302 \n", | |
"X value is -4.982078892451994\n", | |
"Iteration 303 \n", | |
"X value is -4.9824373146029535\n", | |
"Iteration 304 \n", | |
"X value is -4.982788568310895\n", | |
"Iteration 305 \n", | |
"X value is -4.983132796944677\n", | |
"Iteration 306 \n", | |
"X value is -4.983470141005784\n", | |
"Iteration 307 \n", | |
"X value is -4.983800738185668\n", | |
"Iteration 308 \n", | |
"X value is -4.984124723421955\n", | |
"Iteration 309 \n", | |
"X value is -4.984442228953515\n", | |
"Iteration 310 \n", | |
"X value is -4.984753384374445\n", | |
"Iteration 311 \n", | |
"X value is -4.985058316686956\n", | |
"Iteration 312 \n", | |
"X value is -4.9853571503532175\n", | |
"Iteration 313 \n", | |
"X value is -4.985650007346153\n", | |
"Iteration 314 \n", | |
"X value is -4.9859370071992295\n", | |
"Iteration 315 \n", | |
"X value is -4.986218267055245\n", | |
"Iteration 316 \n", | |
"X value is -4.98649390171414\n", | |
"Iteration 317 \n", | |
"X value is -4.986764023679857\n", | |
"Iteration 318 \n", | |
"X value is -4.98702874320626\n", | |
"Iteration 319 \n", | |
"X value is -4.987288168342134\n", | |
"Iteration 320 \n", | |
"X value is -4.987542404975292\n", | |
"Iteration 321 \n", | |
"X value is -4.987791556875786\n", | |
"Iteration 322 \n", | |
"X value is -4.98803572573827\n", | |
"Iteration 323 \n", | |
"X value is -4.988275011223505\n", | |
"Iteration 324 \n", | |
"X value is -4.988509510999035\n", | |
"Iteration 325 \n", | |
"X value is -4.988739320779054\n", | |
"Iteration 326 \n", | |
"X value is -4.988964534363473\n", | |
"Iteration 327 \n", | |
"X value is -4.989185243676204\n", | |
"Iteration 328 \n", | |
"X value is -4.98940153880268\n", | |
"Iteration 329 \n", | |
"X value is -4.989613508026626\n", | |
"Iteration 330 \n", | |
"X value is -4.989821237866094\n", | |
"Iteration 331 \n", | |
"X value is -4.990024813108772\n", | |
"Iteration 332 \n", | |
"X value is -4.9902243168465965\n", | |
"Iteration 333 \n", | |
"X value is -4.990419830509665\n", | |
"Iteration 334 \n", | |
"X value is -4.990611433899471\n", | |
"Iteration 335 \n", | |
"X value is -4.990799205221482\n", | |
"Iteration 336 \n", | |
"X value is -4.990983221117052\n", | |
"Iteration 337 \n", | |
"X value is -4.991163556694711\n", | |
"Iteration 338 \n", | |
"X value is -4.991340285560817\n", | |
"Iteration 339 \n", | |
"X value is -4.9915134798496\n", | |
"Iteration 340 \n", | |
"X value is -4.991683210252608\n", | |
"Iteration 341 \n", | |
"X value is -4.991849546047556\n", | |
"Iteration 342 \n", | |
"X value is -4.992012555126605\n", | |
"Iteration 343 \n", | |
"X value is -4.992172304024073\n", | |
"Iteration 344 \n", | |
"X value is -4.992328857943591\n", | |
"Iteration 345 \n", | |
"X value is -4.99248228078472\n", | |
"Iteration 346 \n", | |
"X value is -4.992632635169025\n", | |
"Iteration 347 \n", | |
"X value is -4.9927799824656445\n", | |
"Iteration 348 \n", | |
"X value is -4.992924382816332\n", | |
"Iteration 349 \n", | |
"X value is -4.993065895160005\n", | |
"Iteration 350 \n", | |
"X value is -4.993204577256805\n", | |
"Iteration 351 \n", | |
"X value is -4.993340485711669\n", | |
"Iteration 352 \n", | |
"X value is -4.993473675997436\n", | |
"Iteration 353 \n", | |
"X value is -4.993604202477487\n", | |
"Iteration 354 \n", | |
"X value is -4.993732118427937\n", | |
"Iteration 355 \n", | |
"X value is -4.993857476059379\n", | |
"Iteration 356 \n", | |
"X value is -4.993980326538191\n", | |
"Iteration 357 \n", | |
"X value is -4.9941007200074266\n", | |
"Iteration 358 \n", | |
"X value is -4.994218705607278\n", | |
"Iteration 359 \n", | |
"X value is -4.994334331495133\n", | |
"Iteration 360 \n", | |
"X value is -4.994447644865231\n", | |
"Iteration 361 \n", | |
"X value is -4.994558691967926\n", | |
"Iteration 362 \n", | |
"X value is -4.994667518128567\n", | |
"Iteration 363 \n", | |
"X value is -4.994774167765996\n", | |
"Iteration 364 \n", | |
"X value is -4.9948786844106765\n", | |
"Iteration 365 \n", | |
"X value is -4.994981110722463\n", | |
"Iteration 366 \n", | |
"X value is -4.995081488508014\n", | |
"Iteration 367 \n", | |
"X value is -4.995179858737854\n", | |
"Iteration 368 \n", | |
"X value is -4.995276261563097\n", | |
"Iteration 369 \n", | |
"X value is -4.995370736331835\n", | |
"Iteration 370 \n", | |
"X value is -4.9954633216051985\n", | |
"Iteration 371 \n", | |
"X value is -4.995554055173095\n", | |
"Iteration 372 \n", | |
"X value is -4.995642974069633\n", | |
"Iteration 373 \n", | |
"X value is -4.99573011458824\n", | |
"Iteration 374 \n", | |
"X value is -4.995815512296476\n", | |
"Iteration 375 \n", | |
"X value is -4.995899202050547\n", | |
"Iteration 376 \n", | |
"X value is -4.995981218009535\n", | |
"Iteration 377 \n", | |
"X value is -4.996061593649345\n", | |
"Iteration 378 \n", | |
"X value is -4.996140361776358\n", | |
"Iteration 379 \n", | |
"X value is -4.996217554540831\n", | |
"Iteration 380 \n", | |
"X value is -4.996293203450014\n", | |
"Iteration 381 \n", | |
"X value is -4.996367339381013\n", | |
"Iteration 382 \n", | |
"X value is -4.996439992593393\n", | |
"Iteration 383 \n", | |
"X value is -4.996511192741525\n", | |
"Iteration 384 \n", | |
"X value is -4.996580968886694\n", | |
"Iteration 385 \n", | |
"X value is -4.99664934950896\n", | |
"Iteration 386 \n", | |
"X value is -4.9967163625187805\n", | |
"Iteration 387 \n", | |
"X value is -4.996782035268405\n", | |
"Iteration 388 \n", | |
"X value is -4.996846394563037\n", | |
"Iteration 389 \n", | |
"X value is -4.996909466671776\n", | |
"Iteration 390 \n", | |
"X value is -4.996971277338341\n", | |
"Iteration 391 \n", | |
"X value is -4.997031851791574\n", | |
"Iteration 392 \n", | |
"X value is -4.997091214755742\n", | |
"Iteration 393 \n", | |
"X value is -4.997149390460628\n", | |
"Iteration 394 \n", | |
"X value is -4.997206402651415\n", | |
"Iteration 395 \n", | |
"X value is -4.997262274598387\n", | |
"Iteration 396 \n", | |
"X value is -4.997317029106419\n", | |
"Iteration 397 \n", | |
"X value is -4.997370688524291\n", | |
"Iteration 398 \n", | |
"X value is -4.997423274753805\n", | |
"Iteration 399 \n", | |
"X value is -4.997474809258729\n", | |
"Iteration 400 \n", | |
"X value is -4.997525313073554\n", | |
"Iteration 401 \n", | |
"X value is -4.997574806812083\n", | |
"Iteration 402 \n", | |
"X value is -4.997623310675841\n", | |
"Iteration 403 \n", | |
"X value is -4.997670844462324\n", | |
"Iteration 404 \n", | |
"X value is -4.997717427573078\n", | |
"Iteration 405 \n", | |
"X value is -4.997763079021617\n", | |
"Iteration 406 \n", | |
"X value is -4.997807817441185\n", | |
"Iteration 407 \n", | |
"X value is -4.997851661092361\n", | |
"Iteration 408 \n", | |
"X value is -4.997894627870514\n", | |
"Iteration 409 \n", | |
"X value is -4.997936735313104\n", | |
"Iteration 410 \n", | |
"X value is -4.9979780006068415\n", | |
"Iteration 411 \n", | |
"X value is -4.998018440594705\n", | |
"Iteration 412 \n", | |
"X value is -4.998058071782811\n", | |
"Iteration 413 \n", | |
"X value is -4.998096910347155\n", | |
"Iteration 414 \n", | |
"X value is -4.998134972140212\n", | |
"Iteration 415 \n", | |
"X value is -4.998172272697408\n", | |
"Iteration 416 \n", | |
"X value is -4.9982088272434595\n", | |
"Iteration 417 \n", | |
"X value is -4.998244650698591\n", | |
"Iteration 418 \n", | |
"X value is -4.998279757684619\n", | |
"Iteration 419 \n", | |
"X value is -4.998314162530927\n", | |
"Iteration 420 \n", | |
"X value is -4.998347879280309\n", | |
"Iteration 421 \n", | |
"X value is -4.998380921694703\n", | |
"Iteration 422 \n", | |
"X value is -4.998413303260809\n", | |
"Iteration 423 \n", | |
"X value is -4.998445037195593\n", | |
"Iteration 424 \n", | |
"X value is -4.998476136451681\n", | |
"Iteration 425 \n", | |
"X value is -4.998506613722648\n", | |
"Iteration 426 \n", | |
"X value is -4.998536481448195\n", | |
"Iteration 427 \n", | |
"X value is -4.998565751819231\n", | |
"Iteration 428 \n", | |
"X value is -4.998594436782846\n", | |
"Iteration 429 \n", | |
"X value is -4.998622548047189\n", | |
"Iteration 430 \n", | |
"X value is -4.998650097086245\n", | |
"Iteration 431 \n", | |
"X value is -4.9986770951445205\n", | |
"Iteration 432 \n", | |
"X value is -4.99870355324163\n", | |
"Iteration 433 \n", | |
"X value is -4.998729482176797\n", | |
"Iteration 434 \n", | |
"X value is -4.998754892533261\n", | |
"Iteration 435 \n", | |
"X value is -4.998779794682596\n", | |
"Iteration 436 \n", | |
"X value is -4.998804198788944\n", | |
"Iteration 437 \n", | |
"X value is -4.998828114813166\n", | |
"Iteration 438 \n", | |
"X value is -4.998851552516903\n", | |
"Iteration 439 \n", | |
"X value is -4.998874521466565\n", | |
"Iteration 440 \n", | |
"X value is -4.998897031037234\n", | |
"Iteration 441 \n", | |
"X value is -4.998919090416489\n", | |
"Iteration 442 \n", | |
"X value is -4.99894070860816\n", | |
"Iteration 443 \n", | |
"X value is -4.998961894435997\n", | |
"Iteration 444 \n", | |
"X value is -4.998982656547277\n", | |
"Iteration 445 \n", | |
"X value is -4.999003003416331\n", | |
"Iteration 446 \n", | |
"X value is -4.999022943348004\n", | |
"Iteration 447 \n", | |
"X value is -4.999042484481044\n", | |
"Iteration 448 \n", | |
"X value is -4.999061634791423\n", | |
"Iteration 449 \n", | |
"X value is -4.999080402095594\n", | |
"Iteration 450 \n", | |
"X value is -4.999098794053682\n", | |
"Iteration 451 \n", | |
"X value is -4.999116818172609\n", | |
"Iteration 452 \n", | |
"X value is -4.999134481809157\n", | |
"Iteration 453 \n", | |
"X value is -4.999151792172974\n", | |
"Iteration 454 \n", | |
"X value is -4.999168756329515\n", | |
"Iteration 455 \n", | |
"X value is -4.999185381202924\n", | |
"Iteration 456 \n", | |
"X value is -4.999201673578866\n", | |
"Iteration 457 \n", | |
"X value is -4.999217640107289\n", | |
"Iteration 458 \n", | |
"X value is -4.999233287305143\n", | |
"Iteration 459 \n", | |
"X value is -4.9992486215590395\n", | |
"Iteration 460 \n", | |
"X value is -4.999263649127859\n", | |
"Iteration 461 \n", | |
"X value is -4.999278376145302\n", | |
"Iteration 462 \n", | |
"X value is -4.999292808622396\n", | |
"Iteration 463 \n", | |
"X value is -4.999306952449948\n", | |
"Iteration 464 \n", | |
"X value is -4.999320813400949\n", | |
"Iteration 465 \n", | |
"X value is -4.99933439713293\n", | |
"Iteration 466 \n", | |
"X value is -4.999347709190272\n", | |
"Iteration 467 \n", | |
"X value is -4.9993607550064665\n", | |
"Iteration 468 \n", | |
"X value is -4.999373539906337\n", | |
"Iteration 469 \n", | |
"X value is -4.99938606910821\n", | |
"Iteration 470 \n", | |
"X value is -4.9993983477260455\n", | |
"Iteration 471 \n", | |
"X value is -4.999410380771525\n", | |
"Iteration 472 \n", | |
"X value is -4.999422173156094\n", | |
"Iteration 473 \n", | |
"X value is -4.9994337296929725\n", | |
"Iteration 474 \n", | |
"X value is -4.999445055099113\n", | |
"Iteration 475 \n", | |
"X value is -4.999456153997131\n", | |
"Iteration 476 \n", | |
"X value is -4.999467030917188\n", | |
"Iteration 477 \n", | |
"X value is -4.9994776902988445\n", | |
"Iteration 478 \n", | |
"X value is -4.999488136492867\n", | |
"Iteration 479 \n", | |
"X value is -4.99949837376301\n", | |
"Iteration 480 \n", | |
"X value is -4.99950840628775\n", | |
"Iteration 481 \n", | |
"X value is -4.999518238161995\n", | |
"Iteration 482 \n", | |
"X value is -4.999527873398756\n", | |
"Iteration 483 \n", | |
"X value is -4.99953731593078\n", | |
"Iteration 484 \n", | |
"X value is -4.999546569612165\n", | |
"Iteration 485 \n", | |
"X value is -4.999555638219921\n", | |
"Iteration 486 \n", | |
"X value is -4.999564525455523\n", | |
"Iteration 487 \n", | |
"X value is -4.999573234946412\n", | |
"Iteration 488 \n", | |
"X value is -4.9995817702474845\n", | |
"Iteration 489 \n", | |
"X value is -4.999590134842535\n", | |
"Iteration 490 \n", | |
"X value is -4.999598332145684\n", | |
"Iteration 491 \n", | |
"X value is -4.99960636550277\n", | |
"Iteration 492 \n", | |
"X value is -4.999614238192715\n", | |
"Iteration 493 \n", | |
"X value is -4.999621953428861\n", | |
"Iteration 494 \n", | |
"X value is -4.999629514360284\n", | |
"Iteration 495 \n", | |
"X value is -4.999636924073078\n", | |
"Iteration 496 \n", | |
"X value is -4.999644185591617\n", | |
"Iteration 497 \n", | |
"X value is -4.999651301879784\n", | |
"Iteration 498 \n", | |
"X value is -4.999658275842188\n", | |
"Iteration 499 \n", | |
"X value is -4.999665110325345\n", | |
"Iteration 500 \n", | |
"X value is -4.999671808118838\n", | |
"Iteration 501 \n", | |
"X value is -4.9996783719564615\n", | |
"Iteration 502 \n", | |
"X value is -4.999684804517332\n", | |
"Iteration 503 \n", | |
"X value is -4.999691108426985\n", | |
"Iteration 504 \n", | |
"X value is -4.999697286258446\n", | |
"Iteration 505 \n", | |
"X value is -4.9997033405332765\n", | |
"Iteration 506 \n", | |
"X value is -4.999709273722611\n", | |
"Iteration 507 \n", | |
"X value is -4.999715088248159\n", | |
"Iteration 508 \n", | |
"X value is -4.999720786483196\n", | |
"Iteration 509 \n", | |
"X value is -4.999726370753532\n", | |
"Iteration 510 \n", | |
"X value is -4.999731843338461\n", | |
"Iteration 511 \n", | |
"X value is -4.999737206471692\n", | |
"Iteration 512 \n", | |
"X value is -4.999742462342258\n", | |
"Iteration 513 \n", | |
"X value is -4.999747613095413\n", | |
"Iteration 514 \n", | |
"X value is -4.999752660833504\n", | |
"Iteration 515 \n", | |
"X value is -4.999757607616834\n", | |
"Iteration 516 \n", | |
"X value is -4.999762455464498\n", | |
"Iteration 517 \n", | |
"X value is -4.999767206355208\n", | |
"Iteration 518 \n", | |
"X value is -4.999771862228104\n", | |
"Iteration 519 \n", | |
"X value is -4.999776424983542\n", | |
"Iteration 520 \n", | |
"X value is -4.9997808964838715\n", | |
"Iteration 521 \n", | |
"X value is -4.999785278554194\n", | |
"Iteration 522 \n", | |
"X value is -4.9997895729831106\n", | |
"Iteration 523 \n", | |
"X value is -4.999793781523448\n", | |
"Iteration 524 \n", | |
"X value is -4.999797905892979\n", | |
"Iteration 525 \n", | |
"X value is -4.999801947775119\n", | |
"Iteration 526 \n", | |
"X value is -4.999805908819617\n", | |
"Iteration 527 \n", | |
"X value is -4.999809790643225\n", | |
"Iteration 528 \n", | |
"X value is -4.99981359483036\n", | |
"Iteration 529 \n", | |
"X value is -4.999817322933753\n", | |
"Iteration 530 \n", | |
"X value is -4.999820976475077\n", | |
"Iteration 531 \n", | |
"X value is -4.999824556945576\n", | |
"Iteration 532 \n", | |
"X value is -4.999828065806665\n", | |
"Iteration 533 \n", | |
"X value is -4.9998315044905315\n", | |
"Iteration 534 \n", | |
"X value is -4.999834874400721\n", | |
"Iteration 535 \n", | |
"X value is -4.999838176912706\n", | |
"Iteration 536 \n", | |
"X value is -4.999841413374452\n", | |
"Iteration 537 \n", | |
"X value is -4.999844585106963\n", | |
"Iteration 538 \n", | |
"X value is -4.999847693404824\n", | |
"Iteration 539 \n", | |
"X value is -4.999850739536727\n", | |
"Iteration 540 \n", | |
"X value is -4.999853724745993\n", | |
"Iteration 541 \n", | |
"X value is -4.999856650251073\n", | |
"Iteration 542 \n", | |
"X value is -4.999859517246051\n", | |
"Iteration 543 \n", | |
"X value is -4.99986232690113\n", | |
"Iteration 544 \n", | |
"X value is -4.999865080363108\n", | |
"Iteration 545 \n", | |
"X value is -4.999867778755846\n", | |
"Iteration 546 \n", | |
"X value is -4.999870423180729\n", | |
"Iteration 547 \n", | |
"X value is -4.999873014717115\n", | |
"Iteration 548 \n", | |
"X value is -4.999875554422772\n", | |
"Iteration 549 \n", | |
"X value is -4.999878043334316\n", | |
"Iteration 550 \n", | |
"X value is -4.99988048246763\n", | |
"Iteration 551 \n", | |
"X value is -4.999882872818278\n", | |
"Iteration 552 \n", | |
"X value is -4.999885215361912\n", | |
"Iteration 553 \n", | |
"X value is -4.999887511054674\n", | |
"Iteration 554 \n", | |
"X value is -4.999889760833581\n", | |
"Iteration 555 \n", | |
"X value is -4.999891965616909\n", | |
"Iteration 556 \n", | |
"X value is -4.999894126304571\n", | |
"Iteration 557 \n", | |
"X value is -4.999896243778479\n", | |
"Iteration 558 \n", | |
"X value is -4.999898318902909\n", | |
"Iteration 559 \n", | |
"X value is -4.999900352524851\n", | |
"Iteration 560 \n", | |
"X value is -4.9999023454743545\n", | |
"Iteration 561 \n", | |
"X value is -4.999904298564868\n", | |
"Iteration 562 \n", | |
"X value is -4.9999062125935705\n", | |
"Iteration 563 \n", | |
"X value is -4.999908088341699\n", | |
"Iteration 564 \n", | |
"X value is -4.9999099265748645\n", | |
"Iteration 565 \n", | |
"X value is -4.999911728043367\n", | |
"Iteration 566 \n", | |
"X value is -4.9999134934825\n", | |
"Iteration 567 \n", | |
"X value is -4.99991522361285\n", | |
"Iteration 568 \n", | |
"X value is -4.999916919140593\n", | |
"Iteration 569 \n", | |
"X value is -4.999918580757781\n", | |
"Iteration 570 \n", | |
"X value is -4.999920209142625\n", | |
"Iteration 571 \n", | |
"X value is -4.999921804959773\n", | |
"Iteration 572 \n", | |
"X value is -4.9999233688605775\n", | |
"Iteration 573 \n", | |
"X value is -4.999924901483366\n", | |
"Iteration 574 \n", | |
"X value is -4.999926403453699\n", | |
"Iteration 575 \n", | |
"X value is -4.999927875384625\n", | |
"Iteration 576 \n", | |
"X value is -4.999929317876933\n", | |
"Iteration 577 \n", | |
"X value is -4.999930731519394\n", | |
"Iteration 578 \n", | |
"X value is -4.999932116889006\n", | |
"Iteration 579 \n", | |
"X value is -4.999933474551226\n", | |
"Iteration 580 \n", | |
"X value is -4.999934805060202\n", | |
"Iteration 581 \n", | |
"X value is -4.999936108958998\n", | |
"Iteration 582 \n", | |
"X value is -4.999937386779818\n", | |
"Iteration 583 \n", | |
"X value is -4.999938639044221\n", | |
"Iteration 584 \n", | |
"X value is -4.999939866263337\n", | |
"Iteration 585 \n", | |
"X value is -4.99994106893807\n", | |
"Iteration 586 \n", | |
"X value is -4.999942247559309\n", | |
"Iteration 587 \n", | |
"X value is -4.999943402608123\n", | |
"Iteration 588 \n", | |
"X value is -4.9999445345559606\n", | |
"Iteration 589 \n", | |
"X value is -4.999945643864842\n", | |
"Iteration 590 \n", | |
"X value is -4.999946730987545\n", | |
"Iteration 591 \n", | |
"X value is -4.999947796367794\n", | |
"Iteration 592 \n", | |
"X value is -4.999948840440438\n", | |
"Iteration 593 \n", | |
"X value is -4.999949863631629\n", | |
"Iteration 594 \n", | |
"X value is -4.999950866358997\n", | |
"Iteration 595 \n", | |
"X value is -4.9999518490318176\n", | |
"The local minimum occurs at -4.9999518490318176\n" | |
] | |
} | |
], | |
"source": [ | |
"while previous_step_size > precision and iters < max_iters:\n", | |
" prev_x = cur_x #Store current x value in prev_x\n", | |
" cur_x = cur_x - rate * df(prev_x) #Grad descent\n", | |
" previous_step_size = abs(cur_x - prev_x) #Change in x\n", | |
" iters = iters+1 #iteration count\n", | |
" print(\"Iteration\",iters,\"\\nX value is\",cur_x) #Print iterations\n", | |
" \n", | |
"print(\"The local minimum occurs at\", cur_x)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment