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