Численные методы интегрирования — различия между версиями
Строка 8: | Строка 8: | ||
{{#widget:Iframe |url=http://tm.spbstu.ru/htmlets/Durnev_AA/home.html |width=1000 |height=1500 |border=0 }} | {{#widget:Iframe |url=http://tm.spbstu.ru/htmlets/Durnev_AA/home.html |width=1000 |height=1500 |border=0 }} | ||
+ | |||
+ | ==Код программы== | ||
+ | <div class="mw-collapsible mw-collapsed"> | ||
+ | '''Текст программы на языке JavaScript:''' <div class="mw-collapsible-content"> | ||
+ | <syntaxhighlight lang="javascript" line start="1" enclose="div"> | ||
+ | |||
+ | //Монте-Карло | ||
+ | window.addEventListener("load", program_code, false); | ||
+ | function program_code() { | ||
+ | |||
+ | var ctx = canvas_example.getContext("2d"); | ||
+ | var w = canvas_example.width; | ||
+ | var h = canvas_example.height; | ||
+ | |||
+ | ctx.translate(w/2, h/2); | ||
+ | |||
+ | var x = []; | ||
+ | var y = []; | ||
+ | var min = 1000; | ||
+ | var max = -1000; | ||
+ | var n = 0; | ||
+ | var points; | ||
+ | var cur_points; | ||
+ | var ok = 0; | ||
+ | var a; | ||
+ | var b ; | ||
+ | var scalex, scaley; | ||
+ | var dx; | ||
+ | var dy; | ||
+ | var func; | ||
+ | var time = 5000; | ||
+ | var dt; | ||
+ | var flag = false; | ||
+ | |||
+ | |||
+ | var result; | ||
+ | |||
+ | |||
+ | |||
+ | //функция | ||
+ | function f(x){ | ||
+ | return (eval(func)); | ||
+ | } | ||
+ | |||
+ | function search(){ | ||
+ | |||
+ | func = document.getElementById("fun").value; | ||
+ | |||
+ | for (var i = a; i < b; i+=0.001){ | ||
+ | |||
+ | //максимум | ||
+ | if (f(i) > max){ | ||
+ | max = f(i); | ||
+ | } | ||
+ | |||
+ | //минимум | ||
+ | if (f(i) < min){ | ||
+ | min = f(i); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | //масштаб | ||
+ | scaley = 100/(Math.abs(max) + Math.abs(min)); | ||
+ | |||
+ | if (Math.abs(b) >= Math.abs(a)){ | ||
+ | scalex = 250/b * 0.9; | ||
+ | } | ||
+ | |||
+ | if (Math.abs(b) < Math.abs(a)){ | ||
+ | scalex = 250/Math.abs(a) * 0.9; | ||
+ | } | ||
+ | |||
+ | //сдвиг | ||
+ | dx = (b + a)/2; | ||
+ | dy = (max + min)/2; | ||
+ | |||
+ | //точки | ||
+ | //points = 100; | ||
+ | } | ||
+ | |||
+ | function calculation(){ | ||
+ | |||
+ | cur_points++; | ||
+ | |||
+ | //положительная функция | ||
+ | if (min * max >= 0 && min >= 0 && n < points){ | ||
+ | x[n] = Math.random() * (b - a) + a; | ||
+ | y[n] = Math.random() * (max - 0) + 0; | ||
+ | |||
+ | if (y[n] < f(x[n])){ | ||
+ | ok++; | ||
+ | } | ||
+ | |||
+ | |||
+ | result = (ok / cur_points) * (b - a) * max; | ||
+ | n++; | ||
+ | |||
+ | } | ||
+ | |||
+ | //отрицательная функция | ||
+ | if (min * max >= 0 && max <= 0 && n < points){ | ||
+ | x[n] = Math.random() * (b - a) + a; | ||
+ | y[n] = Math.random() * (min - 0) + 0; | ||
+ | |||
+ | if (y[n] > f(x[n])){ | ||
+ | ok++; | ||
+ | } | ||
+ | |||
+ | result = -(ok/cur_points) * (b - a) * (-min); | ||
+ | n++; | ||
+ | } | ||
+ | |||
+ | //знакопеременные функции | ||
+ | if (min * max < 0 && n < points){ | ||
+ | |||
+ | x[n] = Math.random() * (b - a) + a; | ||
+ | y[n] = Math.random() * (max - min) + min; | ||
+ | |||
+ | |||
+ | if (y[n] > 0 && f(x[n]) > 0 && y[n] < f(x[n])){ | ||
+ | ok++; | ||
+ | } | ||
+ | |||
+ | if (y[n] < 0 && f(x[n]) < 0 && y[n] > f(x[n])){ | ||
+ | ok--; | ||
+ | } | ||
+ | |||
+ | result = (ok/cur_points) * (b - a) * (max - min); | ||
+ | n++; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | function draw(){ | ||
+ | //оси | ||
+ | ctx.beginPath(); | ||
+ | ctx.moveTo(0 - dx * scalex, -h/2); | ||
+ | ctx.lineTo(0 - dx * scalex, h/2); | ||
+ | ctx.moveTo(w/2 , dy * scaley); | ||
+ | ctx.lineTo(-w/2 , dy * scaley); | ||
+ | ctx.stroke(); | ||
+ | |||
+ | ctx.font = '10px Arial'; | ||
+ | ctx.strokeText('1', (1 - dx) * scalex, (-0.1 + dy) * scaley); | ||
+ | ctx.strokeText('1', (0.1 -dx) * scalex, (-1 + dy) * scaley); | ||
+ | |||
+ | |||
+ | //кривая | ||
+ | ctx.beginPath(); | ||
+ | for (var i = a; i < b; i+=0.01){ | ||
+ | ctx.moveTo((i - dx) * scalex, (-f(i) + dy) * scaley); | ||
+ | ctx.lineTo((i+0.01 - dx) * scalex, (-f(i+0.01) + dy) * scaley); | ||
+ | } | ||
+ | |||
+ | ctx.stroke(); | ||
+ | |||
+ | |||
+ | //точки | ||
+ | ctx.fillStyle = 'red'; | ||
+ | ctx.beginPath(); | ||
+ | ctx.arc((x[n-1] - dx) * scalex, (-y[n-1] + dy) * scaley, 1, 0, 2 * Math.PI); | ||
+ | ctx.fill(); | ||
+ | |||
+ | } | ||
+ | |||
+ | function control(){ | ||
+ | calculation(); | ||
+ | draw(); | ||
+ | result1.innerHTML = Math.round(result * 100)/100; | ||
+ | } | ||
+ | |||
+ | knopka.onclick = function(){ | ||
+ | |||
+ | if (flag){ | ||
+ | location.reload(); | ||
+ | } | ||
+ | |||
+ | flag = true; | ||
+ | knopka.value = 'Очистить'; | ||
+ | cur_points = 0; | ||
+ | points = parseFloat(document.getElementById("qual").value); | ||
+ | dt = time/points; | ||
+ | console.log(dt) | ||
+ | a = parseFloat(document.getElementById("a1").value); | ||
+ | b = parseFloat(document.getElementById("b1").value); | ||
+ | search(); | ||
+ | var name = setInterval(control, dt); | ||
+ | } | ||
+ | |||
+ | |||
+ | if (n == points){ | ||
+ | clearInterval(name); | ||
+ | console.log('a'); | ||
+ | } | ||
+ | } |
Версия 11:32, 17 мая 2019
Описание
Визуализация различных численных методов
Исполнитель: Дурнев Андрей
Группа 13632/1 Кафедра Теоретической механики
Код программы
<syntaxhighlight lang="javascript" line start="1" enclose="div">
//Монте-Карло window.addEventListener("load", program_code, false); function program_code() {
var ctx = canvas_example.getContext("2d"); var w = canvas_example.width; var h = canvas_example.height;
ctx.translate(w/2, h/2);
var x = []; var y = []; var min = 1000; var max = -1000; var n = 0; var points; var cur_points; var ok = 0; var a; var b ; var scalex, scaley; var dx; var dy; var func; var time = 5000; var dt; var flag = false;
var result;
//функция function f(x){ return (eval(func)); }
function search(){
func = document.getElementById("fun").value;
for (var i = a; i < b; i+=0.001){
//максимум if (f(i) > max){ max = f(i); }
//минимум if (f(i) < min){ min = f(i); } }
//масштаб
scaley = 100/(Math.abs(max) + Math.abs(min));
if (Math.abs(b) >= Math.abs(a)){ scalex = 250/b * 0.9; }
if (Math.abs(b) < Math.abs(a)){ scalex = 250/Math.abs(a) * 0.9; }
//сдвиг
dx = (b + a)/2; dy = (max + min)/2;
//точки //points = 100; }
function calculation(){
cur_points++;
//положительная функция if (min * max >= 0 && min >= 0 && n < points){ x[n] = Math.random() * (b - a) + a; y[n] = Math.random() * (max - 0) + 0;
if (y[n] < f(x[n])){ ok++; }
result = (ok / cur_points) * (b - a) * max;
n++;
}
//отрицательная функция if (min * max >= 0 && max <= 0 && n < points){ x[n] = Math.random() * (b - a) + a; y[n] = Math.random() * (min - 0) + 0;
if (y[n] > f(x[n])){ ok++; }
result = -(ok/cur_points) * (b - a) * (-min);
n++;
}
//знакопеременные функции if (min * max < 0 && n < points){
x[n] = Math.random() * (b - a) + a; y[n] = Math.random() * (max - min) + min;
if (y[n] > 0 && f(x[n]) > 0 && y[n] < f(x[n])){
ok++;
}
if (y[n] < 0 && f(x[n]) < 0 && y[n] > f(x[n])){ ok--; }
result = (ok/cur_points) * (b - a) * (max - min); n++; } }
function draw(){ //оси ctx.beginPath(); ctx.moveTo(0 - dx * scalex, -h/2); ctx.lineTo(0 - dx * scalex, h/2); ctx.moveTo(w/2 , dy * scaley); ctx.lineTo(-w/2 , dy * scaley); ctx.stroke();
ctx.font = '10px Arial'; ctx.strokeText('1', (1 - dx) * scalex, (-0.1 + dy) * scaley); ctx.strokeText('1', (0.1 -dx) * scalex, (-1 + dy) * scaley);
//кривая
ctx.beginPath();
for (var i = a; i < b; i+=0.01){
ctx.moveTo((i - dx) * scalex, (-f(i) + dy) * scaley);
ctx.lineTo((i+0.01 - dx) * scalex, (-f(i+0.01) + dy) * scaley);
}
ctx.stroke();
//точки
ctx.fillStyle = 'red';
ctx.beginPath();
ctx.arc((x[n-1] - dx) * scalex, (-y[n-1] + dy) * scaley, 1, 0, 2 * Math.PI);
ctx.fill();
}
function control(){ calculation(); draw(); result1.innerHTML = Math.round(result * 100)/100; }
knopka.onclick = function(){
if (flag){ location.reload(); }
flag = true; knopka.value = 'Очистить'; cur_points = 0; points = parseFloat(document.getElementById("qual").value); dt = time/points; console.log(dt) a = parseFloat(document.getElementById("a1").value); b = parseFloat(document.getElementById("b1").value); search(); var name = setInterval(control, dt); }
if (n == points){
clearInterval(name);
console.log('a');
}