Численные методы интегрирования
Материал из Department of Theoretical and Applied Mechanics
Версия от 11:35, 17 мая 2019; 195.209.230.53 (обсуждение)
Описание
Визуализация различных численных методов
Исполнитель: Дурнев Андрей
Группа 13632/1 Кафедра Теоретической механики
Код программы
Текст программы на языке JavaScript:
1 //Монте-Карло
2 window.addEventListener("load", program_code, false);
3 function program_code() {
4
5 var ctx = canvas_example.getContext("2d");
6 var w = canvas_example.width;
7 var h = canvas_example.height;
8
9 ctx.translate(w/2, h/2);
10
11 var x = [];
12 var y = [];
13 var min = 1000;
14 var max = -1000;
15 var n = 0;
16 var points;
17 var cur_points;
18 var ok = 0;
19 var a;
20 var b ;
21 var scalex, scaley;
22 var dx;
23 var dy;
24 var func;
25 var time = 5000;
26 var dt;
27 var flag = false;
28
29
30 var result;
31
32
33
34 //функция
35 function f(x){
36 return (eval(func));
37 }
38
39 function search(){
40
41 func = document.getElementById("fun").value;
42
43 for (var i = a; i < b; i+=0.001){
44
45 //максимум
46 if (f(i) > max){
47 max = f(i);
48 }
49
50 //минимум
51 if (f(i) < min){
52 min = f(i);
53 }
54 }
55
56 //масштаб
57 scaley = 100/(Math.abs(max) + Math.abs(min));
58
59 if (Math.abs(b) >= Math.abs(a)){
60 scalex = 250/b * 0.9;
61 }
62
63 if (Math.abs(b) < Math.abs(a)){
64 scalex = 250/Math.abs(a) * 0.9;
65 }
66
67 //сдвиг
68 dx = (b + a)/2;
69 dy = (max + min)/2;
70
71 //точки
72 //points = 100;
73 }
74
75 function calculation(){
76
77 cur_points++;
78
79 //положительная функция
80 if (min * max >= 0 && min >= 0 && n < points){
81 x[n] = Math.random() * (b - a) + a;
82 y[n] = Math.random() * (max - 0) + 0;
83
84 if (y[n] < f(x[n])){
85 ok++;
86 }
87
88
89 result = (ok / cur_points) * (b - a) * max;
90 n++;
91
92 }
93
94 //отрицательная функция
95 if (min * max >= 0 && max <= 0 && n < points){
96 x[n] = Math.random() * (b - a) + a;
97 y[n] = Math.random() * (min - 0) + 0;
98
99 if (y[n] > f(x[n])){
100 ok++;
101 }
102
103 result = -(ok/cur_points) * (b - a) * (-min);
104 n++;
105 }
106
107 //знакопеременные функции
108 if (min * max < 0 && n < points){
109
110 x[n] = Math.random() * (b - a) + a;
111 y[n] = Math.random() * (max - min) + min;
112
113
114 if (y[n] > 0 && f(x[n]) > 0 && y[n] < f(x[n])){
115 ok++;
116 }
117
118 if (y[n] < 0 && f(x[n]) < 0 && y[n] > f(x[n])){
119 ok--;
120 }
121
122 result = (ok/cur_points) * (b - a) * (max - min);
123 n++;
124 }
125 }
126
127 function draw(){
128 //оси
129 ctx.beginPath();
130 ctx.moveTo(0 - dx * scalex, -h/2);
131 ctx.lineTo(0 - dx * scalex, h/2);
132 ctx.moveTo(w/2 , dy * scaley);
133 ctx.lineTo(-w/2 , dy * scaley);
134 ctx.stroke();
135
136 ctx.font = '10px Arial';
137 ctx.strokeText('1', (1 - dx) * scalex, (-0.1 + dy) * scaley);
138 ctx.strokeText('1', (0.1 -dx) * scalex, (-1 + dy) * scaley);
139
140
141 //кривая
142 ctx.beginPath();
143 for (var i = a; i < b; i+=0.01){
144 ctx.moveTo((i - dx) * scalex, (-f(i) + dy) * scaley);
145 ctx.lineTo((i+0.01 - dx) * scalex, (-f(i+0.01) + dy) * scaley);
146 }
147
148 ctx.stroke();
149
150
151 //точки
152 ctx.fillStyle = 'red';
153 ctx.beginPath();
154 ctx.arc((x[n-1] - dx) * scalex, (-y[n-1] + dy) * scaley, 1, 0, 2 * Math.PI);
155 ctx.fill();
156
157 }
158
159 function control(){
160 calculation();
161 draw();
162 result1.innerHTML = Math.round(result * 100)/100;
163 }
164
165 knopka.onclick = function(){
166
167 if (flag){
168 location.reload();
169 }
170
171 flag = true;
172 knopka.value = 'Очистить';
173 cur_points = 0;
174 points = parseFloat(document.getElementById("qual").value);
175 dt = time/points;
176 console.log(dt)
177 a = parseFloat(document.getElementById("a1").value);
178 b = parseFloat(document.getElementById("b1").value);
179 search();
180 var name = setInterval(control, dt);
181 }
182
183
184 if (n == points){
185 clearInterval(name);
186 console.log('a');
187 }
188 }
189
190 //Метод трапеций
191 window.addEventListener("load", program_code, false);
192 function program_code() {
193
194 var ctx = canvas_example.getContext("2d");
195 var w = canvas_example.width;
196 var h = canvas_example.height;
197
198 var flag = false;
199 var a;
200 var b;
201 var func;
202 var max = -100000;
203 var min = 100000;
204 var scale, scale1, scale2;
205 var x = [];
206 var y = [];
207 var result = 0;
208
209 ctx.translate(w/2, h/2);
210
211 //функция
212 function f(x){
213 return (eval(func));
214 }
215
216 function input(){
217
218 a = parseFloat(document.getElementById("a1").value);
219 b = parseFloat(document.getElementById("b1").value);
220 func = document.getElementById("fun").value;
221 number = parseFloat(document.getElementById('qual').value);
222
223 //минимум и максимум
224 for (var i = a; i < b; i+=0.001){
225
226 //максимум
227 if (f(i) > max){
228 max = f(i);
229 }
230
231 //минимум
232 if (f(i) < min){
233 min = f(i);
234 }
235 }
236
237 //масштаб
238 scaley = 100/(Math.abs(max) + Math.abs(min));
239
240 if (Math.abs(b) >= Math.abs(a)){
241 scalex = 250/b * 0.9;
242 }
243
244 if (Math.abs(b) < Math.abs(a)){
245 scalex = 250/Math.abs(a) * 0.9;
246 }
247
248 //сдвиг
249 dx = (b + a)/2;
250 dy = (max + min)/2;
251 dt = (b - a)/number;
252
253 //разбиение
254 for (var i = a; i < b + 1/2 * dt; i += dt){
255 x.push(i);
256 y.push(f(i));
257 }
258 }
259
260 function calculation(){
261 for (var i = 0; i < number; i++){
262
263 if (f(x[i]) >= 0 && f(x[i+1]) >= 0){
264 result += (x[i + 1] - x[i]) * (Math.abs(y[i]) + Math.abs(y[i + 1])) * 0.5;
265 }
266
267 if (f(x[i]) < 0 && f(x[i+1]) < 0){
268 result -= (x[i + 1] - x[i]) * (Math.abs(y[i]) + Math.abs(y[i + 1])) * 0.5;
269 }
270
271 else{
272 result += 0;
273 }
274
275 }
276 }
277
278 function draw(){
279
280 //оси
281 ctx.beginPath();
282 ctx.moveTo(0 - dx * scalex, -h/2);
283 ctx.lineTo(0 - dx * scalex, h/2);
284 ctx.moveTo(w/2 , 0 + dy * scaley);
285 ctx.lineTo(-w/2 , 0 + dy * scaley);
286 ctx.stroke();
287
288 ctx.font = '10px Arial';
289 ctx.strokeText('1', (1 - dx) * scalex, (-0.1 + dy) * scaley);
290 ctx.strokeText('1', (0.1 -dx) * scalex, (-1 + dy) * scaley);
291
292 //кривая
293 ctx.beginPath();
294 for (var i = a; i < b; i+=0.01){
295 ctx.moveTo((i - dx) * scalex, (-f(i) + dy) * scaley);
296 ctx.lineTo((i + 0.01 - dx) * scalex, (-f(i + 0.01) + dy) * scaley);
297 }
298 ctx.stroke();
299
300
301 ctx.strokeStyle = 'red';
302
303 //разбиение
304 ctx.beginPath();
305 for (var i = 0; i < number + 1; i++){
306 ctx.moveTo((x[i] - dx) * scalex, dy * scaley);
307 ctx.lineTo((x[i] - dx) * scalex, (-y[i] + dy) * scaley)
308 }
309 ctx.stroke();
310
311 ctx.beginPath();
312 for(var i = 0; i < number; i++){
313 ctx.moveTo((x[i] - dx) * scalex, (-y[i] + dy) * scaley);
314 ctx.lineTo((x[i+1] - dx) * scalex, (-y[i+1] + dy) * scaley);
315 }
316 ctx.stroke();
317
318 }
319
320
321 knopka.onclick = function(){
322
323 if (flag){
324 location.reload();
325 }
326
327 flag = true;
328 knopka.value = 'Очистить';
329 input();
330 calculation();
331 result1.innerHTML = Math.round(result * 100)/100;
332 draw();
333 }
334 }
335
336
337 //Метод прямоугольников
338 window.addEventListener("load", program_code, false);
339 function program_code() {
340
341 var ctx = canvas_example.getContext("2d");
342 var w = canvas_example.width;
343 var h = canvas_example.height;
344
345 var flag = false;
346 var a;
347 var b;
348 var func;
349 var max = -100000;
350 var min = 100000;
351 var scale, scale1, scale2;
352 var x = [];
353 var y = [];
354 var result = 0;
355 var dt;
356 var dx = 0;
357 var dy = 0;
358
359 ctx.translate(w/2, h/2);
360
361 //функция
362 function f(x){
363 return (eval(func));
364 }
365
366 function input(){
367
368 a = parseFloat(document.getElementById("a1").value);
369 b = parseFloat(document.getElementById("b1").value);
370 func = document.getElementById("fun").value;
371 var number = parseFloat(document.getElementById('qual').value);
372 dx = (b - a)/number;
373
374 //минимум и максимум
375 for (var i = a; i < b; i+=0.001){
376
377 //максимум
378 if (f(i) > max){
379 max = f(i);
380 }
381
382 //минимум
383 if (f(i) < min){
384 min = f(i);
385 }
386 }
387
388 //масштаб
389 scaley = 100/(Math.abs(max) + Math.abs(min));
390
391 if (Math.abs(b) >= Math.abs(a)){
392 scalex = 250/b * 0.9;
393 }
394
395 if (Math.abs(b) < Math.abs(a)){
396 scalex = 250/Math.abs(a) * 0.9;
397 }
398
399 //сдвиг
400 dx = (b + a)/2;
401 dy = (max + min)/2;
402 dt = (b - a)/number;
403
404 }
405
406 function calculation(){
407
408 if (menu.value == 'first'){
409 console.log('a');
410 for (var i = a; i < b; i += dt){
411
412 if (f(i) >= 0 & f(i + dt) >= 0){
413 result += Math.abs(f(i))*dt;
414 }
415
416 if (f(i) < 0 & f(i + dt) < 0){
417 result -= Math.abs(f(i))*dt;
418 }
419
420 else{
421 result += 0;
422 }
423
424 }
425 }
426
427 if (menu.value == 'second'){
428 for (var i = a + dt; i < b; i += dt){
429 if (f(i) >= 0 & f(i + dt) >= 0){
430 result += Math.abs(f(i))*dt;
431 }
432
433 if (f(i) < 0 & f(i + dt) < 0){
434 result -= Math.abs(f(i))*dt;
435 }
436
437 else{
438 result += 0;
439 }
440 }
441 }
442
443 if (menu.value == 'third'){
444 for (var i = a + dt/2 ; i < b; i += dt){
445 if (f(i) >= 0 & f(i + dt) >= 0){
446 result += Math.abs(f(i))*dt;
447 }
448
449 if (f(i) < 0 & f(i + dt) < 0){
450 result -= Math.abs(f(i))*dt;
451 }
452
453 else{
454 result += 0;
455 }
456 }
457 }
458
459 }
460
461 function draw(){
462
463 //оси
464 ctx.beginPath();
465 ctx.moveTo(0 - dx * scalex, -h/2);
466 ctx.lineTo(0 - dx * scalex, h/2);
467 ctx.moveTo(w/2 , 0 + dy * scaley);
468 ctx.lineTo(-w/2 , 0 + dy * scaley);
469 ctx.stroke();
470
471 ctx.font = '10px Arial';
472 ctx.strokeText('1', (1 - dx) * scalex, (-0.1 + dy) * scaley);
473 ctx.strokeText('1', (0.1 -dx) * scalex, (-1 + dy) * scaley);
474
475 //кривая
476 ctx.beginPath();
477 for (var i = a; i < b; i+=0.01){
478 ctx.moveTo((i - dx) * scalex, (-f(i) + dy) * scaley);
479 ctx.lineTo((i + 0.01 - dx) * scalex, (-f(i + 0.01) + dy) * scaley);
480 }
481 ctx.stroke();
482
483 ctx.strokeStyle = 'red';
484
485 if (menu.value == 'first'){
486 for (var i = a; i < b - dt/2; i += dt){
487 if(f(i) >= 0){
488 ctx.strokeRect((i - dx) * scalex, (-f(i) + dy) * scaley, dt * scalex, Math.abs(f(i)) * scaley);
489 }
490 else{
491 ctx.strokeRect((i - dx) * scalex, (dy) * scaley, dt * scalex, Math.abs(f(i)) * scaley);
492 }
493 }
494 }
495
496 if (menu.value == 'second'){
497 for (var i = a + dt; i < b + dt/2; i += dt){
498 if(f(i) >= 0){
499 ctx.strokeRect((i - dx) * scalex, (-f(i) + dy) * scaley, dt * scalex, Math.abs(f(i)) * scaley);
500 }
501 else{
502 ctx.strokeRect((i - dx) * scalex, (dy) * scaley, dt * scalex, Math.abs(f(i)) * scaley);
503 }
504 }
505 }
506
507 if (menu.value == 'third'){
508 for (var i = a + dt/2; i < b + dt/4; i += dt){
509 if(f(i) >= 0){
510 ctx.strokeRect((i - dt/2 - dx) * scalex, (-f(i) + dy) * scaley, dt * scalex, Math.abs(f(i)) * scaley);
511 }
512 else{
513 ctx.strokeRect((i - dt/2 - dx) * scalex, (dy) * scaley, dt * scalex, Math.abs(f(i)) * scaley);
514 }
515 }
516 }
517
518 }
519
520 knopka.onclick = function(){
521 if (flag){
522 location.reload();
523 }
524
525 flag = true;
526 knopka.value = 'Очистить';
527 input();
528 calculation();
529 draw();
530 result1.innerHTML = Math.round(result * 100)/100;
531 }
532
533 }