# A problem with functions and Infinite expression 1/0 encountering

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 =
Array[Integrate[
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.

-
Related, I believe: What is the cleanest way to prevent divide-by-zero warnings? – Mr.Wizard Apr 16 '14 at 22:01
Also, FYI: CosInt * BoxMatrix[0, 6] // MatrixForm – Mr.Wizard Apr 16 '14 at 22:04
@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 !! – Shellp Apr 17 '14 at 6:44
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? – Mr.Wizard Apr 17 '14 at 6:57
This question appears to be off-topic because it is too localized. – Oleksandr R. May 11 '14 at 3:04

[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.

Try

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.)

-