September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
125
Task/Resistor-mesh/C++/resistor-mesh.cpp
Normal file
125
Task/Resistor-mesh/C++/resistor-mesh.cpp
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
class Node {
|
||||
private:
|
||||
double v;
|
||||
int fixed;
|
||||
|
||||
public:
|
||||
Node() : v(0.0), fixed(0) {
|
||||
// empty
|
||||
}
|
||||
|
||||
Node(double v, int fixed) : v(v), fixed(fixed) {
|
||||
// empty
|
||||
}
|
||||
|
||||
double getV() const {
|
||||
return v;
|
||||
}
|
||||
|
||||
void setV(double nv) {
|
||||
v = nv;
|
||||
}
|
||||
|
||||
int getFixed() const {
|
||||
return fixed;
|
||||
}
|
||||
|
||||
void setFixed(int nf) {
|
||||
fixed = nf;
|
||||
}
|
||||
};
|
||||
|
||||
void setBoundary(std::vector<std::vector<Node>>& m) {
|
||||
m[1][1].setV(1.0);
|
||||
m[1][1].setFixed(1);
|
||||
|
||||
m[6][7].setV(-1.0);
|
||||
m[6][7].setFixed(-1);
|
||||
}
|
||||
|
||||
double calculateDifference(const std::vector<std::vector<Node>>& m, std::vector<std::vector<Node>>& d, const int w, const int h) {
|
||||
double total = 0.0;
|
||||
for (int i = 0; i < h; ++i) {
|
||||
for (int j = 0; j < w; ++j) {
|
||||
double v = 0.0;
|
||||
int n = 0;
|
||||
if (i > 0) {
|
||||
v += m[i - 1][j].getV();
|
||||
n++;
|
||||
}
|
||||
if (j > 0) {
|
||||
v += m[i][j - 1].getV();
|
||||
n++;
|
||||
}
|
||||
if (i + 1 < h) {
|
||||
v += m[i + 1][j].getV();
|
||||
n++;
|
||||
}
|
||||
if (j + 1 < w) {
|
||||
v += m[i][j + 1].getV();
|
||||
n++;
|
||||
}
|
||||
v = m[i][j].getV() - v / n;
|
||||
d[i][j].setV(v);
|
||||
if (m[i][j].getFixed() == 0) {
|
||||
total += v * v;
|
||||
}
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
double iter(std::vector<std::vector<Node>>& m, const int w, const int h) {
|
||||
using namespace std;
|
||||
vector<vector<Node>> d;
|
||||
for (int i = 0; i < h; ++i) {
|
||||
vector<Node> t(w);
|
||||
d.push_back(t);
|
||||
}
|
||||
|
||||
double curr[] = { 0.0, 0.0, 0.0 };
|
||||
double diff = 1e10;
|
||||
|
||||
while (diff > 1e-24) {
|
||||
setBoundary(m);
|
||||
diff = calculateDifference(m, d, w, h);
|
||||
for (int i = 0; i < h; ++i) {
|
||||
for (int j = 0; j < w; ++j) {
|
||||
m[i][j].setV(m[i][j].getV() - d[i][j].getV());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < h; ++i) {
|
||||
for (int j = 0; j < w; ++j) {
|
||||
int k = 0;
|
||||
if (i != 0) ++k;
|
||||
if (j != 0) ++k;
|
||||
if (i < h - 1) ++k;
|
||||
if (j < w - 1) ++k;
|
||||
curr[m[i][j].getFixed() + 1] += d[i][j].getV()*k;
|
||||
}
|
||||
}
|
||||
|
||||
return (curr[2] - curr[0]) / 2.0;
|
||||
}
|
||||
|
||||
const int S = 10;
|
||||
int main() {
|
||||
using namespace std;
|
||||
vector<vector<Node>> mesh;
|
||||
|
||||
for (int i = 0; i < S; ++i) {
|
||||
vector<Node> t(S);
|
||||
mesh.push_back(t);
|
||||
}
|
||||
|
||||
double r = 2.0 / iter(mesh, S, S);
|
||||
cout << "R = " << setprecision(15) << r << '\n';
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
|
||||
my ($w, $h) = (9, 9);
|
||||
my @v = map([ (0) x ($w + 1) ], 0 .. $h); # voltage
|
||||
|
|
@ -35,17 +36,16 @@ sub calc_diff {
|
|||
|
||||
sub iter {
|
||||
my $diff = 1;
|
||||
while ($diff > 1e-24) { # 1e-24 is overkill (12 digits of precision)
|
||||
while ($diff > 1e-15) {
|
||||
set_boundary();
|
||||
$diff = calc_diff();
|
||||
print "error^2: $diff\r";
|
||||
#print "error^2: $diff\n"; # un-comment to see slow convergence
|
||||
for my $i (0 .. $h) {
|
||||
for my $j (0 .. $w) {
|
||||
$v[$i][$j] -= $d[$i][$j];
|
||||
}
|
||||
}
|
||||
}
|
||||
print "\n";
|
||||
|
||||
my @current = (0) x 3;
|
||||
for my $i (0 .. $h) {
|
||||
|
|
@ -57,4 +57,4 @@ sub iter {
|
|||
return ($current[1] - $current[-1]) / 2;
|
||||
}
|
||||
|
||||
print "R = @{[2 / iter()]}\n";
|
||||
printf "R = %.6f\n", 2 / iter();
|
||||
|
|
|
|||
27
Task/Resistor-mesh/Phix/resistor-mesh.phix
Normal file
27
Task/Resistor-mesh/Phix/resistor-mesh.phix
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
function resistormesh(integer ni, nj, ai, aj, bi, bj)
|
||||
integer n = ni*nj, k, c
|
||||
sequence A = repeat(repeat(0,n),n),
|
||||
B = repeat({0},n)
|
||||
for i=1 to ni do
|
||||
for j=1 to nj do
|
||||
k = (i-1)*nj + j--1
|
||||
if i=ai and j=aj then
|
||||
A[k,k] = 1
|
||||
else
|
||||
c = 0
|
||||
if i<ni then c += 1; A[k,k+nj] = -1 end if
|
||||
if i>1 then c += 1; A[k,k-nj] = -1 end if
|
||||
if j<nj then c += 1; A[k,k+1] = -1 end if
|
||||
if j>1 then c += 1; A[k,k-1] = -1 end if
|
||||
A[k,k] = c
|
||||
end if
|
||||
end for
|
||||
end for
|
||||
k = (bi-1)*nj +bj
|
||||
B[k,1] = 1
|
||||
A = inverse(A)
|
||||
B = matrix_mul(A,B)
|
||||
return B[k,1]
|
||||
end function
|
||||
|
||||
printf(1,"Resistance = %.13f ohms\n",resistormesh(10, 10, 2, 2, 8, 7))
|
||||
|
|
@ -1,45 +1,45 @@
|
|||
/*REXX program calculates the resistance between any two points on a resister grid.*/
|
||||
/*REXX program calculates the resistance between any two points on a resistor grid.*/
|
||||
if 2=='f2'x then ohms = "ohms" /*EBCDIC machine? Then use 'ohms'. */
|
||||
else ohms = "Ω" /* ASCII " " " Greek Ω.*/
|
||||
parse arg high wide Arow Acol Brow Bcol digs . /*obtain optional arguments from the CL*/
|
||||
if high=='' | high=="," then high=10 /*Not specified? Then use the default.*/
|
||||
if wide=='' | wide=="," then wide=10 /* " " " " " " */
|
||||
if Arow=='' | Arow=="," then Arow= 2 /* " " " " " " */
|
||||
if Acol=='' | Acol=="," then Acol= 2 /* " " " " " " */
|
||||
if Brow=='' | Brow=="," then Brow= 7 /* " " " " " " */
|
||||
if Bcol=='' | Bcol=="," then Bcol= 8 /* " " " " " " */
|
||||
if digs=='' | digs=="," then digs=20 /* " " " " " " */
|
||||
if high=='' | high=="," then high= 10 /*Not specified? Then use the default.*/
|
||||
if wide=='' | wide=="," then wide= 10 /* " " " " " " */
|
||||
if Arow=='' | Arow=="," then Arow= 2 /* " " " " " " */
|
||||
if Acol=='' | Acol=="," then Acol= 2 /* " " " " " " */
|
||||
if Brow=='' | Brow=="," then Brow= 7 /* " " " " " " */
|
||||
if Bcol=='' | Bcol=="," then Bcol= 8 /* " " " " " " */
|
||||
if digs=='' | digs=="," then digs= 20 /* " " " " " " */
|
||||
numeric digits digs /*use moderate decimal digs (precision)*/
|
||||
minVal = 1'e-' || (digs*2) /*calculate the threshold minimul value*/
|
||||
minVal = 1'e-' || (digs*2) /*calculate the threshold minimal value*/
|
||||
say ' minimum value is ' format(minVal,,,,0) " using " digs ' decimal digits'; say
|
||||
say ' resistor mesh size is: ' wide "wide, " high 'high' ; say
|
||||
say ' resistor mesh size is: ' wide "wide, " high 'high' ; say
|
||||
say ' point A is at (row,col): ' Arow"," Acol
|
||||
say ' point B is at (row,col): ' Brow"," Bcol
|
||||
@.=0; cell.=1
|
||||
do until $<=minVal; v=0
|
||||
@.Arow.Acol= 1 ; cell.Arow.Acol=0
|
||||
@.Brow.Bcol= '-1' ; cell.Brow.Bcol=0
|
||||
$=0
|
||||
do i=1 for high; im=i-1; ip=i+1
|
||||
do j=1 for wide; n=0; v=0
|
||||
if i\==1 then do; v=v + @.im.j; n=n+1; end
|
||||
if j\==1 then do; jm=j-1; v=v + @.i.jm; n=n+1; end
|
||||
if i<high then do; v=v + @.ip.j; n=n+1; end
|
||||
if j<wide then do; jp=j+1; v=v + @.i.jp; n=n+1; end
|
||||
v=@.i.j - v/n; #.i.j=v; if cell.i.j then $=$ + v*v
|
||||
end /*j*/
|
||||
end /*i*/
|
||||
do r=1 for High
|
||||
do c=1 for Wide; @.r.c= @.r.c - #.r.c
|
||||
end /*c*/
|
||||
end /*r*/
|
||||
end /*until*/
|
||||
@.=0; cell.= 1
|
||||
do until $<=minVal; v= 0
|
||||
@.Arow.Acol= 1 ; cell.Arow.Acol= 0
|
||||
@.Brow.Bcol= '-1' ; cell.Brow.Bcol= 0
|
||||
$=0
|
||||
do i=1 for high; im= i-1; ip= i+1
|
||||
do j=1 for wide; n= 0; v= 0
|
||||
if i\==1 then do; v= v + @.im.j; n= n+1; end
|
||||
if j\==1 then do; jm= j-1; v= v + @.i.jm; n= n+1; end
|
||||
if i<high then do; v= v + @.ip.j; n= n+1; end
|
||||
if j<wide then do; jp= j+1; v= v + @.i.jp; n= n+1; end
|
||||
v= @.i.j - v / n; #.i.j= v; if cell.i.j then $= $ + v*v
|
||||
end /*j*/
|
||||
end /*i*/
|
||||
do r=1 for High
|
||||
do c=1 for Wide; @.r.c= @.r.c - #.r.c
|
||||
end /*c*/
|
||||
end /*r*/
|
||||
end /*until*/
|
||||
say
|
||||
Acur= #.Arow.Acol * sides(Arow, Acol)
|
||||
Bcur= #.Brow.Bcol * sides(Brow, Bcol)
|
||||
Acur= #.Arow.Acol * sides(Arow, Acol)
|
||||
Bcur= #.Brow.Bcol * sides(Brow, Bcol)
|
||||
say ' resistance between point A and point B is: ' 4 / (Acur - Bcur) ohms
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
sides: parse arg row,col; z=0; if row\==1 & row\==high then z=z+2; else z=z+1
|
||||
if col\==1 & col\==wide then z=z+2; else z=z+1
|
||||
sides: parse arg row,col; z=0; if row\==1 & row\==high then z= z+2; else z= z+1
|
||||
if col\==1 & col\==wide then z= z+2; else z= z+1
|
||||
return z
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue