Following code (function) makes integrals and then replace f[x] with Cos functions and calculate the definite parametric integrals as it is shown below :

DoGenerateIntegrals[Number_] :=

Block[{GeneratingIntegrals, COS, LimitCOS},

GeneratingIntegrals = 
   Symbol["f"][x], {x, Symbol["x" <> ToString[2 # - 1]], 
    Symbol["x" <> ToString[2 #]]}] &, Number];

COS = Total[
 GeneratingIntegrals /. 
  f[x] -> Cos[((i \[Pi])/L) x] Cos[((j \[Pi])/L) x]];
LimitCOS = Limit[COS, i -> j];

{COS, LimitCOS}];

Then :

In[1]:= Produce = DoGenerateIntegrals[2]

By using the results of the In[1] :

CosInt = Which[i != j, Produce[[1, 1]], i == j, Produce[[1, 2]]];

Which CosInt is to avoid encountering Infinite expression (1/0).

Now it will use in a code like below in a matrix :

List1 = Table[0, {i, 1, 6}, {j, 1, 6}];

Do[Do[List1[[i + 2, j + 2]] = CosInt, {j, 1, 2}], {i, 1, 2}];

List1 // MatrixForm

In DoGenerateIntegrals[] when the integrals evaluates with mentioned f[x], there will i-j in their denominator of their fractions which cause Infinite expression in List1 calculation. In order to avoid encountering Infinite expression (1/0), I use LimitCOS and CosInt to calculate the circumstances of i=j . But after calculation, it will face encountering Infinite expression (1/0) with following error:

Power::infy: Infinite expression 1/0 encountered.>>
Infinity::indet: Indeterminate expression 0 ComplexInfinity encountered.>>

I wonder why this is happening; however, I have used some strategies to avoid it.

  • $\begingroup$ Related, I believe: What is the cleanest way to prevent divide-by-zero warnings? $\endgroup$
    – Mr.Wizard
    Apr 16, 2014 at 22:01
  • $\begingroup$ Also, FYI: CosInt * BoxMatrix[0, 6] // MatrixForm $\endgroup$
    – Mr.Wizard
    Apr 16, 2014 at 22:04
  • $\begingroup$ @Mr.Wizard : I think the problem is not about "Indeterminate expression",the major problem about above code is that when I put Produce[[1,1]] and Produce[[1,2]] into CosInt, CosInt can not understand that Produce[[1,1]] and Produce[[1,1]] are also functions of i and j. In spite of using strategies to avoid infinite expression encountering, it ignore Which loop and just uses the Produce[[1,1]]. I can not understand why this is happening !! $\endgroup$
    – Shellp
    Apr 17, 2014 at 6:44
  • $\begingroup$ I wish to help, but I would like to address your questions in the order they appeared. Will you please reply to my requests in my answer to A problem with set values first? $\endgroup$
    – Mr.Wizard
    Apr 17, 2014 at 6:57
  • 2
    $\begingroup$ This question appears to be off-topic because it is too localized. $\endgroup$ May 11, 2014 at 3:04

1 Answer 1


[Update notice: Yesterday, I had some time to play, so I thought I'd help the site out and go through some of the unanswered Q's, answering some and voting to close others. I agree with Oleksandr that this question is of highly localized interest, as well as a newbie mistake. But I suppose that if this answer, which is the answer, is not found useful by the community, then this question should be closed and probably even deleted.]

The problem is that the whole of Produce is evaluated before its part can be extracted in your double-Do loop. Since sometimes i and j have the same value when Produce is evaluated, you get division by zero before the non-offending part is obtained.


CosInt = Which[
  i != j, Evaluate@ Produce[[(*1,*) 1]],
  i == j, Evaluate@ Produce[[(*1,*) 2]]];

Then your loop for filling List1 runs without error messages.

(Since Produce is a simple list of two elements, the part specification ...[[1, 2]] causes an error. Produce would have to be a two-dimensional array for it to be correct.)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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