Well, I'll do my best. I'm basically relying on somebody's random-walk algorithm at one point.
First your original problem:
Think of a random process and write a Monte Carlo program, which finds the solution for threshold/limit of differential equations system: dx/dt = -2x +2y + 2; dy/dt = x - 4y + 4z + 1; dz/dt y - 8z + 6. |
The long-time limit will be when the system goes to steady state. (This system does reach a steady state, but others can grow exponentially.) Then the time derivatives dx/dt, dy/dt and dz/dt tend to zero, so in the long-time limit your equations become the algebraic system
0 = -2x +2y + 2
0 = x - 4y + 4z + 1
0 = y - 8z + 6
In matrix/vector form you could write this as
0 = Ax + b
and you will find
A and
b hard-coded in my code as
1 2 3 4
|
matrix A = { { -2, 2, 0 },
{ 1, -4, 4 },
{ 0, 1, -8 } };
vec b = { 2, 1, 6 };
| |
(Note that I would usually write something like
Ax=b, so be careful with the signs here.)
The method coded wants that written slightly differently, as
x = Hx + b so add
x (or
Ix, where I is the identity matrix), to either side:
x = (A+I)x + b
or
x = Hx + b
This is done with the lines
1 2
|
matrix H = A;
for ( int i = 0; i < N; i++ ) H[i][i]++;
| |
Note that the method won't always converge; you need the "size" of
H to be quite small. (Sorry, I'm not going into eigenvalue spectra - just note that simply dividing all elements of
A and
b by
Amax in my lines 90-99 was attempting to keep
H small in some sense.)
OK, so you have now got a system of the form
x = Hx + b
Each component of
x (i.e. x, y, z) can then be shown to be the "expected value"; i.e.
mean of the displacement X at the end of a sequence of random walks, with probabilities at each step related to the elements in
H - see slides 10,11,12 in
https://math.nist.gov/mcsd/Seminars/2014/2014-11-04-Li-presentation.pdf
(Sorry, these don't constitute a proof - you will have to do quite a lot of research online to work that out: I don't intend to compress my scribbled notes. I managed to follow the first three pages of
http://www.cs.ubbcluj.ro/~studia-m/2006-1/rosca.pdf
before getting lost, but it adds a bit more detail to the other link.)
Slides 13-27 in the first reference go through (in excruciating detail) a SINGLE random walk. The following code (lines 55-67 of the original) corresponds to a single random walk as set out in, e.g., slide 13.
1 2 3 4 5 6 7 8 9 10 11 12 13
|
int index = i;
double W = 1, X = b[i]; // W = multiplied weights; X = displacement on random walk
while ( abs( W ) > SMALL )
{
double r = myRand(); // Random number on [0,1)
int j = 0;
while ( r > sump[index][j] ) j++; // First column where cumulative probability exceeds r
W *= rowSum[index];
if ( H[index][j] < 0 ) W = -W;
X += W * b[j];
index = j;
}
| |
Then I average over a lot of random walks:
1 2 3
|
sumX += X;
}
result[i] = sumX / num;
| |
Sorry, I'm too shaky on the algorithm itself to write anything more substantive than that.
This seems a horrendously complex problem to do with no other guidance. A Monte Carlo method is definitely not what I would usually think of for solving linear equations - straight
Gaussian elimination would do.
Maybe there's some better stochastic method ... perhaps somebody else on the forum can help. My original google search was
"Monte Carlo method linear algebra".
Maybe also I've gone off in completely the wrong direction, so it might be advisable to contact your teacher (nagging gets a surprisingly long way at universities) and ask him/her what was intended. I would hate to send you on a wild goose chase. This seems a real sledgehammer method.