JSFiddle - React, Tailwind, and code Playground
by ipeshev
HTML
<div id="view">
<div data-bind="foreach:matrixes">
<table data-bind="foreach:$data">
<tr>
<!--ko foreach:$data -->
<td data-bind="text:$data ? $data.toFixed($root.accuracy) : 0"></td>
<!-- /ko-->
</tr>
</table>
<div class="divider"></div>
</div>
</div>
CSS
table td {
text-align:center;
border:1px solid black;
}
.divider {
border:2px solid black;
}
JavaScript
var E = Math.E;
function calcCoeff(h,xc){
var h_2 = h*h;
return -2*(3*h_2*xc+1)/h_2;
}
function FillMatrix(n,h,xc,firstRow,lastRow){
var result = [],i,xc,cRow,h_2;
result[0] = firstRow;
h_2 = h*h;
for(i=1;i<n-1;i++){
cRow = initRow(n,getValue(xc),xc);
cRow[i-1] = 1/h_2;
cRow[i] = calcCoeff(h,xc) ;
cRow[i+1] = 1/h_2;
xc=xc+h;
result.push(cRow);
}
result[n-1] = lastRow;
return result;
}
function initRow(n,y,x){
var result = [],i;
y = y || 0;
x = x || 0;
for(i=0;i<n;i++){
result.push(0);
}
result.push(y);
result.push(x);
return result;
}
function Solve(n,xc,matrixes,fullResult){
var h = 1/n,h_2 = h*h;
var endY = 1 - 2*E;
var endX = 1;
//n = n -1 ;
var fRow = initRow(n,getValue(xc)-4/h,xc);
fRow[0]= calcCoeff(h,xc);
fRow[1]= 2/h_2;
var xn_1 = xc + (n-1)*h;
var lRow = initRow(n,getValue(xn_1)-(1-2*E)/h_2,xn_1);
lRow[n-2]= 1/h_2;
lRow[n-1]= calcCoeff(h,xn_1);
xc = xc + h;
var matrix = FillMatrix(n,h,xc,fRow,lRow);
if(fullResult){
matrixes.push(JSON.parse(JSON.stringify(matrix)));
}
matrix = Progonka(matrix);
if(fullResult){
return matrix;
}
var result= [];
ko.utils.arrayForEach(matrix,function(item){
var spliced = item.splice(-2,2);
result.push(spliced.reverse());
});
result.push([endX,endY]);
return result;
}
function Progonka(matrix){
var result = [],mat;
var i;
mat = matrix;
for(i=0;i<(mat.length)-1;i++){
var modus = mat[i+1][i]/mat[i][i]*(-1);
mat = multiplyRow(i,modus,mat);
mat = sumRows(i,i+1,mat);
}
for(i;i>0;i--){
var modus = mat[i-1][i]/mat[i][i]*(-1);
mat = multiplyRow(i,modus,mat);
mat = sumRows(i,i-1,mat);
}
for(i=0;i<mat.length;i++){
var modus = 1/mat[i][i];
...