Last active
December 12, 2015 19:39
-
-
Save nikolayemrikh/862457becad85c558583 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| //Точность эпсилон = 0.01 | |
| var epsilon = Math.pow(10, -2); | |
| lambda = Math.pow(10, -3); | |
| var f = function(x) { | |
| var result; | |
| //result = (2-x)*Math.exp(x+1)-1; | |
| result = Math.pow(x,3)-x+Math.exp(-x); | |
| return result; | |
| } | |
| //Выбираем отрезок, на котором функция унимодальна | |
| var a0 = 0, b0 = 1 | |
| //это наши первые точки | |
| a = a0, b = b0; | |
| var alpha, beta, delta; | |
| var i = 0 | |
| console.log("| n | a | b | alpha | beta | f(a) | f(b) | delta |"); | |
| while ((b-a) >= epsilon) { | |
| delta = (b-a); | |
| alpha = a+(2/(3+Math.sqrt(5)))*delta; | |
| beta = a+(2/(1+Math.sqrt(5)))*delta; | |
| console.log("|" + i +"|" + a.toPrecision(4) + "|" + b.toPrecision(4) + "|" | |
| + alpha.toPrecision(4) + "|" + beta.toPrecision(4) + "|" + f(alpha).toFixed(3) | |
| + "|" + f(beta).toFixed(3) + "|" + delta.toFixed(3) + "|"); | |
| if (f(alpha) <= f(beta)) { | |
| b = beta; | |
| } else if (f(alpha) > f(beta)){ | |
| a = alpha; | |
| } else { | |
| throw new BadRangeException("Ты втираешь мне какую-то дичь"); | |
| } | |
| i +=1; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment