-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
341 lines (299 loc) · 11.5 KB
/
index.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<html>
<head>
<title>fdm.js</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/a11y-light.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js"></script>
<script>hljs.highlightAll();</script>
<script src="fdm.js"></script>
<script src="simple-plot.js"></script>
</head>
<body>
<div class="container mt-4">
<div class="row justify-content-md-center">
<div class="col-md-7">
<div class="row">
<h1 class="display-6">fdm.js</h1>
</div>
<div class="row">
<p class="lead">
A Javascript Library for Finite Difference Method (FDM)
</p>
</div>
<div class="row">
<canvas id="canvas" style="border: 1px solid rgb(168, 168, 168);">Canvas is not supported in your browser.</canvas>
</div>
<div class="row">
<p>
Fig. 1. Comparison of analytical and numerical solution of the user-defined function f(x) using fdm.js.
</p>
</div>
<div class="row">
<div class="mx-auto">
<pre><code class="javascript">// define f(x)
function f(x) { return Math.exp(0.11 * x) * Math.sin(x); }
// solve 1st derivative of f(x)
const fdm = new FDM();
fdm.dh = 0.1;
const g = fdm.grad(f);</code></pre>
</div>
</div>
<hr>
<h2>Introduction</h2>
<div class="row">
<p>
The fdm.js is a simple JavaScript library that enables you to calculate the 1st and 2nd derivatives of user-defined functions.
It utilizes the <span class="fw-bold">Finite Difference Method</span> (FDM), which is a commonly employed differentiation technique in computer simulation.
</p>
<p>
This library focuses on being "easy to use" and "lightweight."
Developers now have enhanced capabilities for conducting numerical simulations quickly and effortlessly.
</p>
</div>
<h2>Tutorial</h2>
<div class="row">
<p>
First, link fdm.js to your page by adding the following line to HTML header.
</p>
<div class="mx-auto">
<pre><code class="html"><script src="fdm.js"></script></code></pre>
</div>
<p>
Next, define a function you'd like to differentiate.
</p>
<div class="mx-auto">
<pre><code class="javascript">const f = x => x * x;</code></pre>
</div>
<p>
Create an instance of fdm.js and set the step size.
</p>
<div class="mx-auto">
<pre><code class="javascript">const fdm = new FDM();
fdm.dh = 0.1;</code></pre>
</div>
<p>
You can also specify `fdm.accuracy` to increase the accuracy.
e.g. <code>fdm.accuracy = 1, 2, 3, ...</code>
<br>
The algorithm is derived from <a href="https://doi.org/10.1090%2FS0025-5718-1988-0935077-0">Bengt(1988)</a>.
</p>
<div class="mx-auto">
<pre><code class="javascript">fdm.accuracy = 2;</code></pre>
</div>
</div>
<p>
Finally, wrap the function with <code>grad</code> or <code>lap</code> to create derivative functions.
</p>
<div class="mx-auto">
<pre><code class="javascript">const g = fdm.grad(f); // gradient
console.log(g(1) - 2*1);
console.log(g(2) - 2*2);
const h = fdm.lap(f); // laplacian
console.log(h(1) - 2);
console.log(h(2) - 2);</code></pre>
</div>
<p>
The above example illustrates a one-dimensional case, but this library can support up to three dimensions.
</p>
<h4>RESULT CODE</h4>
<div class="mx-auto">
<pre><code class="html"><html>
<head>
<script src="fdm.js"></script>
</head>
<body>
<script>
// define f(x)
const f = x => x * x;
// setup fdm.js
const fdm = new FDM();
fdm.dh = 0.1;
fdm.accuracy = 2;
// gradient
const g = fdm.grad(f);
console.log(g(1) - 2*1);
console.log(g(2) - 2*2);
// laplacian
const h = fdm.lap(f);
console.log(h(1) - 2);
console.log(h(2) - 2);
</script>
</body>
</html></code></pre>
</div>
<h2>Examples</h2>
<div class="row">
<p>The following examples are calculated by fdm.js.</p>
</div>
<div class="row">
<h4>Polynomial Function</h4>
<canvas id="ex_polynomial" style="border: 1px solid rgb(168, 168, 168);">Canvas is not supported in your browser.</canvas>
</div>
<div class="row">
<p>
Fig. 2. Comparison of analytical and numerical solution of f(x) = x**3.
</p>
</div>
<div class="row">
<h4>Square Root</h4>
<canvas id="ex_sqrt_x" style="border: 1px solid rgb(168, 168, 168);">Canvas is not supported in your browser.</canvas>
</div>
<div class="row">
<p>
Fig. 3. Comparison of analytical and numerical solution of f(x) = sqrt(x).
</p>
</div>
<div class="row">
<h4>Exponential Function</h4>
<canvas id="ex_exponential" style="border: 1px solid rgb(168, 168, 168);">Canvas is not supported in your browser.</canvas>
</div>
<div class="row">
<p>
Fig. 4. Comparison of analytical and numerical solution of f(x) = exp(x).
</p>
</div>
<div class="row">
<h4>Sinusoidal Function</h4>
<canvas id="ex_sinusoidal" style="border: 1px solid rgb(168, 168, 168);">Canvas is not supported in your browser.</canvas>
</div>
<div class="row">
<p>
Fig. 4. Comparison of analytical and numerical solution of f(x) = sin(x).
</p>
</div>
<h2>License</h2>
<div class="row">
<p>MIT License.</p>
</div>
<hr>
</div>
</div>
</div>
<script>
function main() {
draw_fig1();
draw_fig2();
draw_fig3();
draw_fig4();
draw_fig5();
}
function draw_fig1() {
const f = x => Math.exp(0.11 * x) * Math.sin(x);
const df = x => Math.exp(0.11 * x) * (0.11 * Math.sin(x) + Math.cos(x));
const fdm = new FDM(0.01);
fdm.accuracy = 2;
const dh = fdm.dh;
const g = fdm.grad(f);
const xmin = -10; const xmax = +10;
const ymin = -3; const ymax = +3;
const a = []; const b = [];
for (let i = 0; xmin + i * dh <= xmax; i++) {
a[i] = xmin + i * dh;
}
for (let i = 0; ymin + i * dh <= ymax; i++) {
b[i] = ymin + i * dh;
}
const sp = new SimplePlot("canvas");
const plots = [];
plots[0] = { func: f, color: '#64DB8F', legend: 'f(x) = exp(0.11x) sin(x)' };
plots[1] = { func: g, color: '#F9F790', legend: 'FDM.grad(f)' };
plots[2] = { func: df, color: '#DB7307', style: [10, 5], legend: 'Theoretical df/dx' };
sp.draw(plots, a, b);
}
function draw_fig2() {
const f = x => x * x * x;
const df = x => 3 * x * x;
const fdm = new FDM(0.1);
fdm.accuracy = 1;
const dh = fdm.dh;
const g = fdm.grad(f);
const xmin = -4; const xmax = +4;
const ymin = -3; const ymax = +3;
const a = []; const b = [];
for (let i = 0; xmin + i * dh <= xmax; i++) {
a[i] = xmin + i * dh;
}
for (let i = 0; ymin + i * dh <= ymax; i++) {
b[i] = ymin + i * dh;
}
const sp = new SimplePlot("ex_polynomial");
const plots = [];
plots[0] = { func: f, color: '#64DB8F', legend: 'f(x) = x**3' };
plots[1] = { func: g, color: '#F9F790', legend: 'FDM.grad(f)' };
plots[2] = { func: df, color: '#DB7307', style: [10, 5], legend: 'Theoretical df/dx' };
sp.draw(plots, a, b);
}
function draw_fig3() {
const f = x => Math.sqrt(x);
const df = x => 0.5 / Math.sqrt(x);
const fdm = new FDM(0.01);
fdm.accuracy = 1;
const dh = fdm.dh;
const g = fdm.grad(f);
const xmin = 0; const xmax = +1;
const ymin = 0; const ymax = +1;
const a = []; const b = [];
for (let i = 0; xmin + i * dh <= xmax; i++) {
a[i] = xmin + i * dh;
}
for (let i = 0; ymin + i * dh <= ymax; i++) {
b[i] = ymin + i * dh;
}
const sp = new SimplePlot("ex_sqrt_x");
const plots = [];
plots[0] = { func: f, color: '#64DB8F', legend: 'f(x) = sqrt(x)' };
plots[1] = { func: g, color: '#F9F790', legend: 'FDM.grad(f)' };
plots[2] = { func: df, color: '#DB7307', style: [10, 5], legend: 'Theoretical df/dx' };
sp.draw(plots, a, b);
}
function draw_fig4() {
const f = x => Math.exp(x);
const df = x => Math.exp(x);
const fdm = new FDM(0.01);
fdm.accuracy = 1;
const dh = fdm.dh;
const g = fdm.grad(f);
const xmin = -3; const xmax = +5;
const ymin = -1; const ymax = +40;
const a = []; const b = [];
for (let i = 0; xmin + i * dh <= xmax; i++) {
a[i] = xmin + i * dh;
}
for (let i = 0; ymin + i * dh <= ymax; i++) {
b[i] = ymin + i * dh;
}
const sp = new SimplePlot("ex_exponential");
const plots = [];
plots[0] = { func: f, color: '#64DB8F', legend: 'f(x) = exp(x)' };
plots[1] = { func: g, color: '#F9F790', legend: 'FDM.grad(f)' };
plots[2] = { func: df, color: '#DB7307', style: [10, 5], legend: 'Theoretical df/dx' };
sp.draw(plots, a, b);
}
function draw_fig5() {
const f = x => Math.sin(x);
const df = x => Math.cos(x);
const fdm = new FDM(0.01);
fdm.accuracy = 1;
const dh = fdm.dh;
const g = fdm.grad(f);
const xmin = -3 * 3.14; const xmax = +3 * 3.14;
const ymin = -2; const ymax = +2;
const a = []; const b = [];
for (let i = 0; xmin + i * dh <= xmax; i++) {
a[i] = xmin + i * dh;
}
for (let i = 0; ymin + i * dh <= ymax; i++) {
b[i] = ymin + i * dh;
}
const sp = new SimplePlot("ex_sinusoidal");
const plots = [];
plots[0] = { func: f, color: '#64DB8F', legend: 'f(x) = sin(x)' };
plots[1] = { func: g, color: '#F9F790', legend: 'FDM.grad(f)' };
plots[2] = { func: df, color: '#DB7307', style: [10, 5], legend: 'Theoretical df/dx' };
sp.draw(plots, a, b);
}
window.onload = main;
</script>
</body>
</html>