4
$\begingroup$

To solve all the a[i,j] & b[i,j] through Mathematica,I need to create a group of equations.The m & n are arbitrary.And for all the 0<=i<=m & 0<=j<=n,the equations should be estabished. The equations:

a[i-1,j-1]+b[i,j-1]==a[i-1,j]+b[i-1,j-1];
a[i,j]+b[i,j]==a[i-1,j]+b[i,j-1]+P[i,j];
P[x1,y1]==t;
P[x2,y2]==-t;
else P[i,j]==0;
a[-1,x]==0,
a[m,x]==0,
b[x,-1]==0,
b[x,n]==0

(the x here is arbitrary)

$\endgroup$
3
  • $\begingroup$ Wrap them in a list {eq1, eq2, ...} or link them with the logical And operator (&&) as eq1 && eq2 && .... $\endgroup$ – MarcoB Feb 2 at 13:37
  • $\begingroup$ @MarcoB Thanks,but my question is that I need that the equations could be simply changed when I change m or n,for example,if i want to m=3 &n=2,I need to create a group of equations,but when i change n from 2 to 4,I need to create a different group again $\endgroup$ – cottea Feb 2 at 13:45
  • $\begingroup$ @MarcoB Could you please give me a method to create a group of equations automaticly?I mean that I just need to type in m & n,and the program can change i & j and give out the equations(or the result ofSolve[]) itself $\endgroup$ – cottea Feb 2 at 13:50
10
$\begingroup$

Is this what you want?

First we write all equations that do not depend on i or j. Then we make a list with all equations that depend on i and j. Then we join the 2 lists. For a small example we choose n=m=2:

n = 2; m = 2;

eq1={P[_, _] == 0,
     P[x1, y1] == t,
     P[x2, y2] == -t,
     b[x, n] == 0,
     a[-1, x] == 0,
     a[m, x] == 0,
     b[x, -1] == 0}

eq2= Table[{a[i - 1, j - 1] + b[i, j - 1] == a[i - 1, j] + b[i - 1, j - 1],
   a[i, j] + b[i, j] == a[i - 1, j] + b[i, j - 1] + P[i, j]}
  , {i, 0, m}, {j, 0, n}] // Flatten;

eq=Join[eq1,eq2]
$\endgroup$
6
  • $\begingroup$ Thanks very much,your answer is quite beautiful! I haven't imagine the function flatten could be used in this situation. $\endgroup$ – cottea Feb 2 at 13:57
  • $\begingroup$ And I have a small question still,how to make all P[i,j]==0 except P[x1,y1] & P[x2,y2] $\endgroup$ – cottea Feb 2 at 14:01
  • $\begingroup$ Sorry,I mean that how to put 0 to all P[i,j],that isP[i,j]=0, rather than P[i,j]==0 $\endgroup$ – cottea Feb 2 at 14:38
  • 2
    $\begingroup$ You can achieve this by specifying: P[_,_]=0 . Now, do not think that this will set all P[.,.]=0. Instead MMA applies more specific patterns before more general ones. If you specify a more specific pattern like P[1,1]=2 MMA will try first this pattern before the more general one. Note, I changed my answer to include this case $\endgroup$ – Daniel Huber Feb 2 at 19:57
  • $\begingroup$ Thanks,the method is right.However,before I change x1,x2 or y1,y2,I must close the document and reopen it,or MMA will let both the former and the new P[x1,y1]=t;P[x2,y2]=-t,could you let just the new P = t or -t?Here is my codem =; n =; x1 =; y1 =; x2 =; y2 =; P[_, _] = 0; P[x1, y1] = 1; P[x2, y2] = -1; b[_, n] = 0; a[-1, _] = 0; a[m, _] = 0; b[_, -1] = 0;Solve[Table[{a[i - 1, j - 1] + b[i, j - 1] == a[i - 1, j] + b[i - 1, j - 1], a[i, j] + b[i, j] == a[i - 1, j] + b[i, j - 1] + P[i, j]}, {i, 0, m}, {j, 0, n}] // Flatten] // Flatten $\endgroup$ – cottea Feb 3 at 4:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.