-
Notifications
You must be signed in to change notification settings - Fork 0
/
lagrange.html
278 lines (194 loc) · 7.38 KB
/
lagrange.html
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>Lagrange Multiplier</title>
<script src="http://s.codepen.io/assets/libs/modernizr.js" type="text/javascript"></script>
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/style.css">
<script src="js/numeric-1.2.6.js"></script>
<script src="js/numeric-1.2.6.min.js"></script>
<script type="text/javascript" language="javascript" src="js/vis.js"></script>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<script src="js/algebra-0.2.5.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.6.0/katex.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.6.0/katex.min.js"></script>
</head>
<body>
<nav class="nav" id="navi1">
<div class="padded">
<ul>
<li class="active"><a id="link1" class="nav-section1" href="index.html">Home</a></li>
<li class="scrollTop"><a href="#"><span class="entypo-up-open"></span></a></li>
</ul>
</div>
</nav>
<article>
<h1 id="section1"> Lagrange Multipliers</h1>
<h2>Optimised Extremum</h2>
<div id="main">
The method of Lagrange multipliers allows us to maximize or minimize functions with the constraint
that we only consider points on a certain surface.
<h2>Key Point:</h2> The gradient of a function is in the direction of <strong>steepest</strong> increase of the function <span id="fun"></span> and is orthogonal to the contour lines.
<br>
Using the above key point, we can conclude that the gradient of <span id="fun1"></span> and <span id="gun"></span> should be parallel at the point of extremum. Since if they are not parallel then there will be a component of <span id="fun2"></span> on <span id="gun1"></span> and the gradient will increase in that direction in order to maximise <span id="fun3"></span>.
</article>
<script >
var Fraction = algebra.Fraction;
var Expression = algebra.Expression;
var Equation = algebra.Equation;
var expr = new Expression("x").multiply(new Fraction(-9,10));
katex.render("f(x)",fun);
katex.render("f(x)",fun1);
katex.render("f(x)",fun2);
katex.render("f(x)",fun3);
katex.render("g(x)",gun);
katex.render("g(x)",gun1);
</script>
<!-- --------------------------------Sript starts here ------------------------------------------- -->
<script type="text/javascript">
var lolz;
function onload() {
a1 = document.getElementById('a1');
a2 = document.getElementById('a2');
b1 = document.getElementById('b1');
b2 = document.getElementById('b2');
b3 = document.getElementById('b3');
b4 = document.getElementById('b4');
b5 = document.getElementById('b5');
b6 = document.getElementById('b6');
}
function kk(){
a1= parseInt(a1.value, 10);
a2= parseInt(a2.value, 10);
b1 = parseInt(b1.value, 10);
b2 = parseInt(b2.value, 10);
b3 = parseInt(b3.value, 10);
b4 = parseInt(b4.value, 10);
b5 = parseInt(b5.value, 10);
b6 = parseInt(b6.value, 10);
// x = (x1/lambda +x2) , y = (y1/lambda +y2)
x1 = ((a2*b3-2*a1*b2)/(4*b1*b2 - b3*b3));
x2 = ((2*b4*b2 - b3*b5)/(4*b1*b2 - b3*b3));
y1 = ((a1*b3 - 2*a2*b1)/(4*b1*b2 - b3*b3));
y2 = ((2*b1*b5 - b3*b4)/(4*b1*b2 - b3*b3));
//Quadratic form of lambda (lm) : (Coefficients)
c1 = b1*x2*x2 + b2*y2*y2 + b3*x2*y2 + b4*x2 + b5*y2 - b6;
c2 = 2*x1*x2*b1 + 2*y1*y2*b2 + (x1*y2 + x2*y1)*b3 + b4*x1 + b5*y1;
c3 = b1*x1*x1 + b2*y1*y1 + b3*x1*y1;
// alert(c1 + " " + c2 +" " +c3)
// Lagrange values
lm1 = (-c2 + Math.sqrt(c2*c2 - 4*c1*c3))/(2*c1);
lm2 = (-c2 - Math.sqrt(c2*c2 - 4*c1*c3))/(2*c1);
xOptimised = (x1/lm1) + x2;
yOptimised = (y1/lm1) + y2;
// alert(xOptimised + " " +yOptimised );
// Plotting the gradient at (xOptimised, yOptimised, zOptimised).
//grad[2] = g3*zOptimised;
//Tangent equation
// Gradient:
gradFx = a1;
gradFy = a2;
gradGx = 2*b1*xOptimised + b3*yOptimised + b4;
gradGy = b3*xOptimised + 2*b2*yOptimised + b5;
var xF = [xOptimised, gradFx];
var yF = [yOptimised, gradFy];
var xG = [xOptimised, gradGx];
var yG = [yOptimised, gradGy];
//------------------------PLOT STARTS----------------------------------
//---------------------------------------------------------------------
var size = 50, x1 = new Array(size), y1 = new Array(size), z1 = new Array(size), z11 = new Array(size), i, j;
for(var i = 0; i < size; i=i+1) {
x1[i] = i-25;
y1[i]=i-25;
z1[i] = new Array(size);
z11[i] = new Array(size);
}
for(var i = 0; i < size; i++) {
for( var j = 0; j < size; j++) {
z1[i][j] = b1*x1[i]*x1[i] + b2*y1[j]*y1[j] + b3*x1[i]*y1[j] + b4*x1[i] + b5*y1[j] -b6;
z11[i][j] = -1 * z1[i][j];
}
}
var trace1 = {
x: x1,
y: y1,
z: z1,
name: 'yaxis data',
type: 'contour',
colorscale: 'Jet',
contours:{
coloring: 'lines'
}
};
var trace2 = {
x: x1,
y: y1,
z: z11,
name: 'yaxis data',
type: 'contour',
colorscale: 'Jet',
contours:{
coloring: 'lines'
}
};
var trace3 = {
x: xF,
y: yF,
type: 'scatter'
};
var trace4 = {
x: xG,
y: yG,
type: 'scatter'
};
var data = [trace1, trace2, trace3,trace4];
var layout = {
title: 'Function g',
yaxis: {title: 'yaxis title', range:[-20,20]},
yaxis2: {
title: 'yaxis2 title',
titlefont: {color: 'rgb(148, 103, 189)'},
tickfont: {color: 'rgb(148, 103, 189)'},
overlaying: 'y',
side: 'right',
range:[-20,20]
},
xaxis:{range:[-20,20]}
};
Plotly.newPlot('myDiv', data, layout);
}
</script>
<h2>Enter the function f </h2>
<input type="text" name="enter" class="enter" value="" id="a1" size="10"/> <span id="x"></span> +
<input type="text" name="enter" class="enter" value="" id="a2" size="10" /> <span id="y"></span>
<h2> Enter the constraint g </h2>
<input type="text" name="enter" class="enter" value="" id="b1" size="10"/> <span id="x2"></span> +
<input type="text" name="enter" class="enter" value="" id="b2" size="10"/> <span id="y2"></span> +
<input type="text" name="enter" class="enter" value="" id="b3" size="10"/> <span id="xy"></span> +
<input type="text" name="enter" class="enter" value="" id="b4" size="10"/> <span id="x1"></span> +
<input type="text" name="enter" class="enter" value="" id="b5" size="10"/> <span id="y1"></span> =
<input type="text" name="enter" class="enter" value="" id="b6" size="10"/>
<br>
<br>
<input type="button" value="Submit" onclick="kk()"/>
<script >
var Fraction = algebra.Fraction;
var Expression = algebra.Expression;
var Equation = algebra.Equation;
var a = new Expression("x").pow(2);
var b = new Expression("y").pow(2);
katex.render("x",x);
katex.render("y",y);
katex.render(algebra.toTex(a),x2);
katex.render(algebra.toTex(b),y2);
katex.render("xy",xy);
katex.render("x",x1);
katex.render("y",y1);
</script>
<br>
<div id = "myDiv"></div>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src="js/index.js"></script>
</body>
</html>