var points function clone obj if obj null typeof obj object return obj

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var points = [];
function clone(obj){
if(obj == null || typeof(obj) != 'object')
return obj;
var temp = new obj.constructor();
for(var key in obj)
temp[key] = clone(obj[key]);
return temp;
}
var curPoint = {
x1: 0.0,
x2: 0.0
};
var nextPoint = {
x1: 0.0,
x2: 0.0
};
var lambda = 0.03;
var curIter = 1;
var neededPenaltyFunc = false;
var a = 6;
var fPrev = 0;
do {
curPoint = clone(nextPoint);
nextPoint.x1 = Math.max(0,
curPoint.x1 + lambda * (1 + 4 * curPoint.x2)
+ lambda * (neededPenaltyFunc ? (a * (4 - 2 * curPoint.x1)) : 0)
);
nextPoint.x2 = Math.max(0,
curPoint.x2 + lambda * (2 + 4 * curPoint.x1)
+ lambda * (neededPenaltyFunc ? (a * (2 - 2 * curPoint.x2)) : 0)
);
points.push(clone(nextPoint));
var g = 9 - (nextPoint.x1 - 2) * (nextPoint.x1 - 2)
- (nextPoint.x2 - 1) * (nextPoint.x2 - 1);
var f = -nextPoint.x1 - 2 * nextPoint.x2 - 4 * nextPoint.x1 * nextPoint.x2;
neededPenaltyFunc = g < 0;
if (!(curIter % 20)) {
a /= 1.1;
lambda /= 3;
}
if (Math.abs(fPrev - f) <= 0.012) {
break;
}
document.body.innerHTML += '<b>Итерация №' + (curIter++) + '.</b><br>'
+ 'x1 = ' + nextPoint.x1.toFixed(3)
+ ' (Δ1 = ' + (nextPoint.x1 - curPoint.x1).toFixed(3) + ');<br>'
+ 'x2 = ' + nextPoint.x2.toFixed(3)
+ ' (Δ2 = ' + (nextPoint.x2 - curPoint.x2).toFixed(3) + ');<br>'
+ 'f(x) = ' + f.toFixed(3) + '; '
+ ' (Δf = ' + (f - fPrev).toFixed(3) + ');<br>'
+ 'g(x) = ' + g.toFixed(3) + ';<br><br>';
fPrev = f;
} while (!console.log('while_condition') && curIter < 100)