-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathNewton_solver.html
229 lines (201 loc) · 5.26 KB
/
Newton_solver.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
<!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8">
<style>
body {font-family: Helvetica, sans-serif;}
table {background-color:#CCDDEE;text-align:left}
</style>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js"],
jax: ["input/TeX", "output/HTML-CSS"],
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
processEscapes: true
},
"HTML-CSS": { fonts: ["TeX"] }
});
</script>
<script type="text/javascript" aync src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js"></script>
<script src="https://cdn.plot.ly/plotly-2.5.1.min.js"></script>
<title>Newton solver</title>
</head>
<body>
<main>
<h1 style="text-align:center">Newton solver</h1>
<table style="align_center;border-radius: 20px;padding: 20px;margin:auto">
<col width="1000">
<tr>
<td>
<div id="plotOutput" style="width: 1000px; height: 600px;border:2px solid #000000;border-radius: 0px;background-color:#EEEEEE"></div>
</td>
</tr>
<tr>
<td><table style="margin:20px">
<col width="200" style="padding-right:10px">
<col width="100">
<tr>
<td><label for="newton_steps">Newton steps</label></td>
<td><input type="text" id="textInput" value="1" readonly></td>
</tr>
<tr>
<td></td>
<td><input onchange="document.getElementById('textInput').value=this.value;plot.reset()" id="newton_steps" value="1" type="range" min="1" max="50" step="1"></td>
</tr>
<tr>
<td><label for="fct">Function</label></td>
<td><select onchange="plot.reset()" id="fct" size="1">
<option selected="selected">Quadratic function</option>
<option>Cubic function</option>
<option>Trigonometric function</option>
</select>
</td>
</tr>
</table></td>
</tr>
<tr><td>
<h2>Newton's method</h2>
<p>Newton's method is an iterative approach to find the roots of a function. The method requires the function $f(x)$, its derivative $f'(x)$ and an initial guess $x_0$.</p>
<p>In each iteration the approximation of the solution is improved by:</p>
$$\begin{equation*}
x_{n+1} = x_{n} - \frac{f(x_n)}{f'(x_n)}
\end{equation*}$$
<p>If the initial guess $x_0$ is close enough to the solution and $f'(x_n) \neq 0$, the method usually converges.</p>
</td></tr>
</table>
</main>
<script id="simulation_code" type="text/javascript">
class Plot
{
constructor()
{
this.reset();
this.num_newton_steps = 1;
}
reset()
{
this.num_newton_steps = parseInt(document.getElementById('newton_steps').value);
this.fct = document.getElementById('fct').value;
this.plotFunctions();
}
quadratic_function(x)
{
return 10*(x*x)+2*x-1;
}
grad_quadratic_function(x, y)
{
return 20*x+2.0;
}
cubic_function(x)
{
return 5*(x*x*x)-3*x*x+2*x+1;
}
grad_cubic_function(x, y)
{
return 15*x*x - 6*x+2.0;
}
trigonometric_function(x, y)
{
return 20*Math.sin(2.0*x) + 3*Math.cos(3.0*x);
}
grad_trigonometric_function(x, y)
{
return 40*Math.cos(2.0*x) - 9.0*Math.sin(3.0*x);
}
newton_step(f, grad_f, x)
{
return x - f(x) / grad_f(x);
}
computeData(fct, grad_fct, x0, data)
{
let xValues = [];
let yValues = [];
let x_newton = []
let y_newton = []
let current_x = x0
let x = -3;
let num_steps = 5000;
for (let i = 0; i <= num_steps; i++)
{
xValues.push(x);
yValues.push(fct(x));
x += 6 / num_steps;
}
x_newton.push(current_x)
y_newton.push(0)
for (let i = 0; i < this.num_newton_steps; i++)
{
x_newton.push(current_x)
y_newton.push(fct(current_x))
current_x = this.newton_step(fct, grad_fct, current_x)
x_newton.push(current_x)
y_newton.push(0)
}
var trace_quadratic = {
x: xValues,
y: yValues,
name: "function",
showlegend: true
};
data.push(trace_quadratic);
for (let i = 0; i < this.num_newton_steps+1; i++)
{
if ( i < this.num_newton_steps)
data.push({
type: 'line',
x: [x_newton[2*i+1], x_newton[2*i+2]],
y: [y_newton[2*i+1], y_newton[2*i+2]],
line: {
color: 'rgb(0, 127, 0)',
width: 2,
},
name: "tangent",
showlegend: i==0
})
data.push({
type: 'line',
x: [x_newton[2*i], x_newton[2*i+1]],
y: [y_newton[2*i], y_newton[2*i+1]],
line: {
color: 'rgb(0, 0, 0)',
width: 2,
dash: "dash",
},
text: ["x_" + i.toString(), ""],
textposition: "bottom center",
mode:'lines+markers+text',
name: "x_n",
showlegend: i==0
})
}
}
plotFunctions()
{
var data = [];
if (this.fct == "Quadratic function")
{
this.computeData(this.quadratic_function, this.grad_quadratic_function, 3.0, data)
}
if (this.fct == "Cubic function")
{
this.computeData(this.cubic_function, this.grad_cubic_function, 3.0, data)
}
if (this.fct == "Trigonometric function")
{
this.computeData(this.trigonometric_function, this.grad_trigonometric_function, 0.465, data)
}
var layout = {
title: 'Functions',
width: 1000,
height: 600
};
Plotly.newPlot('plotOutput', data, layout);
}
}
plot = new Plot();
plot.reset();
</script>
</body>
</html>