0

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.

Michael E2
  • 235,386
  • 17
  • 334
  • 747
Shellp
  • 523
  • 3
  • 13

1 Answers1

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.

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

Michael E2
  • 235,386
  • 17
  • 334
  • 747