Численные методы интегрирования
Описание
Визуализация различных численных методов
Исполнитель: Дурнев Андрей
Группа 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');
}
}
//Метод трапеций 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;
var flag = false; var a; var b; var func; var max = -100000; var min = 100000; var scale, scale1, scale2; var x = []; var y = []; var result = 0;
ctx.translate(w/2, h/2);
//функция function f(x){ return (eval(func)); }
function input(){
a = parseFloat(document.getElementById("a1").value);
b = parseFloat(document.getElementById("b1").value);
func = document.getElementById("fun").value; number = parseFloat(document.getElementById('qual').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; dt = (b - a)/number;
//разбиение for (var i = a; i < b + 1/2 * dt; i += dt){ x.push(i); y.push(f(i)); } }
function calculation(){ for (var i = 0; i < number; i++){
if (f(x[i]) >= 0 && f(x[i+1]) >= 0){ result += (x[i + 1] - x[i]) * (Math.abs(y[i]) + Math.abs(y[i + 1])) * 0.5; }
if (f(x[i]) < 0 && f(x[i+1]) < 0){ result -= (x[i + 1] - x[i]) * (Math.abs(y[i]) + Math.abs(y[i + 1])) * 0.5; }
else{ result += 0; }
} }
function draw(){
//оси
ctx.beginPath(); ctx.moveTo(0 - dx * scalex, -h/2); ctx.lineTo(0 - dx * scalex, h/2); ctx.moveTo(w/2 , 0 + dy * scaley); ctx.lineTo(-w/2 , 0 + 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.strokeStyle = 'red';
//разбиение ctx.beginPath(); for (var i = 0; i < number + 1; i++){ ctx.moveTo((x[i] - dx) * scalex, dy * scaley); ctx.lineTo((x[i] - dx) * scalex, (-y[i] + dy) * scaley) } ctx.stroke();
ctx.beginPath(); for(var i = 0; i < number; i++){ ctx.moveTo((x[i] - dx) * scalex, (-y[i] + dy) * scaley); ctx.lineTo((x[i+1] - dx) * scalex, (-y[i+1] + dy) * scaley); } ctx.stroke();
}
knopka.onclick = function(){
if (flag){ location.reload(); }
flag = true; knopka.value = 'Очистить'; input(); calculation(); result1.innerHTML = Math.round(result * 100)/100; draw(); }
}
//Метод прямоугольников
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;
var flag = false; var a; var b; var func; var max = -100000; var min = 100000; var scale, scale1, scale2; var x = []; var y = []; var result = 0; var dt; var dx = 0; var dy = 0;
ctx.translate(w/2, h/2);
//функция function f(x){ return (eval(func)); }
function input(){
a = parseFloat(document.getElementById("a1").value);
b = parseFloat(document.getElementById("b1").value);
func = document.getElementById("fun").value; var number = parseFloat(document.getElementById('qual').value); dx = (b - a)/number;
//минимум и максимум 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;
dt = (b - a)/number;
}
function calculation(){
if (menu.value == 'first'){ console.log('a'); for (var i = a; i < b; i += dt){
if (f(i) >= 0 & f(i + dt) >= 0){ result += Math.abs(f(i))*dt; }
if (f(i) < 0 & f(i + dt) < 0){ result -= Math.abs(f(i))*dt; }
else{ result += 0; }
} }
if (menu.value == 'second'){ for (var i = a + dt; i < b; i += dt){ if (f(i) >= 0 & f(i + dt) >= 0){ result += Math.abs(f(i))*dt; }
if (f(i) < 0 & f(i + dt) < 0){ result -= Math.abs(f(i))*dt; }
else{ result += 0; } } }
if (menu.value == 'third'){ for (var i = a + dt/2 ; i < b; i += dt){ if (f(i) >= 0 & f(i + dt) >= 0){ result += Math.abs(f(i))*dt; }
if (f(i) < 0 & f(i + dt) < 0){ result -= Math.abs(f(i))*dt; }
else{ result += 0; } } }
}
function draw(){
//оси
ctx.beginPath(); ctx.moveTo(0 - dx * scalex, -h/2); ctx.lineTo(0 - dx * scalex, h/2); ctx.moveTo(w/2 , 0 + dy * scaley); ctx.lineTo(-w/2 , 0 + 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.strokeStyle = 'red';
if (menu.value == 'first'){ for (var i = a; i < b - dt/2; i += dt){ if(f(i) >= 0){ ctx.strokeRect((i - dx) * scalex, (-f(i) + dy) * scaley, dt * scalex, Math.abs(f(i)) * scaley); } else{ ctx.strokeRect((i - dx) * scalex, (dy) * scaley, dt * scalex, Math.abs(f(i)) * scaley); } } }
if (menu.value == 'second'){ for (var i = a + dt; i < b + dt/2; i += dt){ if(f(i) >= 0){ ctx.strokeRect((i - dx) * scalex, (-f(i) + dy) * scaley, dt * scalex, Math.abs(f(i)) * scaley); } else{ ctx.strokeRect((i - dx) * scalex, (dy) * scaley, dt * scalex, Math.abs(f(i)) * scaley); } } }
if (menu.value == 'third'){ for (var i = a + dt/2; i < b + dt/4; i += dt){ if(f(i) >= 0){ ctx.strokeRect((i - dt/2 - dx) * scalex, (-f(i) + dy) * scaley, dt * scalex, Math.abs(f(i)) * scaley); } else{ ctx.strokeRect((i - dt/2 - dx) * scalex, (dy) * scaley, dt * scalex, Math.abs(f(i)) * scaley); } } }
}
knopka.onclick = function(){ if (flag){ location.reload(); }
flag = true; knopka.value = 'Очистить'; input(); calculation(); draw(); result1.innerHTML = Math.round(result * 100)/100; }}