0
$\begingroup$

I encounter a problem when computing the following equations:

c[1, 0] := q (cc - a);
c[0, 1] := p (d - b);
c[0, 0] := 1;
c[i_, 0] := -c[i - 1, 0] ((i - 1 + q) p a - p q cc)/((i + q) p - p q);
c[0, j_] := -c[0, j - 1] ((q p b - (j - 1 + p) q d)/((q p - (j + p) q)));
If[h == 0, c[i_, j_] = 0, c[i_, j_] := -(c[i - 1, j] ((i - 1 + q) p a - (j + p) q cc) + c[i, j - 1] ((i + q) p b - (j - 1 + p) q d))/((i + q) p - (j + p) q)];

Then, when I input $c[1,1]$, it comes out as $c[1,1]$

enter image description here

I don't know what's wrong with it. Hope someone could help. Thanks!

Update:

In the first version, my code looks like this:

c[i_,0]:=-c[i-1,0] ((i-1+q)p a-p q cc)/((i+q) p-p q);
c[0,j_]:=-c[0,j-1] ((q p b-(j-1+p) q d)/((q p-(j+p) q)));
c[i_,j_]:=-(c[i-1,j] ((i-1+q)p a-(j+p) q cc)+c[i,j-1]*
  (((i+q) p b-(j-1+p) q d))/(((i+q) p-(j+p) q)));
c[1,0]:=q (cc-a);
c[0,1]:=p (d-b);

then I can compute, for example, c[3,2]: enter image description here

But since I added a condition statement, things went wrong, and I don't know why?

$\endgroup$
  • 1
    $\begingroup$ First, there is a SetDelayed inside If which would not return a result. Second, c[i,j] has not been defined before it is calculated. Third, I do not understand returning c[1,1]=0 as true and looks like a mistake to me. $\endgroup$ – Titus Dec 9 '18 at 9:20
  • $\begingroup$ If possible, please post some more readable descriptions with necessary better-typeset equations. $\endgroup$ – Αλέξανδρος Ζεγγ Dec 9 '18 at 10:21
  • $\begingroup$ What exactly are you trying to do? While valid, c[1, 0] is an unusual syntax for Mathematica. If you're trying to create a matrix, you probably will want to use [[ ]] to access each part of the matrix. You can create an n x m matrix with ConstantArray[0, {n, m}] if you're looking for C-style method. Additionally, MMA indexes from 1 rather than 0. $\endgroup$ – MassDefect Dec 9 '18 at 10:29
  • 1
    $\begingroup$ Maybe something with Condition, like c[i_, j_] /; h == 0 = 0. Or maybe you want the If[] inside the function body. You might consider SameQ[] instead of Equal[], that is, h === 0. $\endgroup$ – Michael E2 Dec 9 '18 at 13:30
  • $\begingroup$ See also RSolve[] and RecurrenceTable[]. $\endgroup$ – Michael E2 Dec 9 '18 at 13:31
1
$\begingroup$

Perhaps you can change

If[h == 0, c[i_, j_] = 0, c[i_, j_] := -(c[i - 1, j] ((i - 1 + q) p a - (j + p) q cc) + c[i, j - 1] ((i + q) p b - (j - 1 + p) q d))/((i + q) p - (j + p) q)];

to

c[i_, j_] := If[h == 0, 0, -(c[i - 1, j] ((i - 1 + q) p a - (j + p) q cc) + c[i, j - 1] ((i + q) p b - (j - 1 + p) q d))/((i + q) p - (j + p) q)];
$\endgroup$
  • $\begingroup$ Thank you for your advice. But it doesn't work. $\endgroup$ – Yuyi Zhang Dec 10 '18 at 1:04
  • $\begingroup$ @YuyiZhang What do you mean? (Be sure to set h before you try it!) $\endgroup$ – Alan Dec 10 '18 at 3:04
  • $\begingroup$ It seems that it will work if I change h==0 to h===0. $\endgroup$ – Yuyi Zhang Dec 10 '18 at 11:43
  • $\begingroup$ @YuyiZhang I suspect your change is not doing what you think it is. Note that h=0.0; If[h==0,0,99] returns 0 but If[h===0,0,99] then returns 99. $\endgroup$ – Alan Dec 10 '18 at 14:13

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.