Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

(Not an answer, but a note needing more space than one allowed in a comment. I'll update this answer if necessary)

What version of Sage do you use ? At least with Sagemath 9.4.rc1, piecewise functions seem problematic :

sage: var("a, j, k, L")
(a, j, k, L)
sage: int1(a, j, k, L) =  piecewise([[[0, 1/4], 1 * cos(pi * (j + 1) * a/L) * cos(pi * (k + 1) * a/L)], [(1/4, 3/4), 2 * cos(pi * (j + 1) * a/L) * cos(pi * (k + 1) * a/L)], [[3/4, 1], 1 * cos(pi * (j + 1) * a/L) * cos(pi * (k + 1) * a/L)]])

So far so good. but :

sage: int1(a, j, k, L)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-162-c48732677c56> in <module>
----> 1 int1(a, j, k, L)

/usr/local/sage-9/local/lib/python3.9/site-packages/sage/symbolic/expression.pyx in sage.symbolic.expression._eval_on_operands.new_f (build/cythonized/sage/symbolic/expression.cpp:70643)()
  13102         new_args = list(ex._unpack_operands())
  13103         new_args.extend(args)
> 13104         return f(ex, *new_args, **kwds)
  13105     return new_f
  13106 

TypeError: __call__() takes from 3 to 4 positional arguments but 7 were given

Ditto when calling with numerical arguments. This could be a newly-introduced bug...

BTW, I do not see the point of using numerical integration in this specific case : your function $f_1$ can be described as :

$$ f_0 : \left( a, j, k, L \right) \ {\mapsto} \ \cos\left(\frac{\pi a j}{L} + \frac{\pi a}{L}\right) \cos\left(\frac{\pi a k}{L} + \frac{\pi a}{L}\right) $$

$$ f_1 : \begin{cases} f_0\left(a, j, k, L\right) & a < \frac{1}{4} \\ 2\,f_0\left(a, j, k, L\right) & a \geq \frac{1}{4} \wedge a \leq \frac{3}{4} \\ f_0\left(a, j, k, L\right) & a > \frac{3}{4} \end{cases} $$

Your integral $\displaystyle\int_0^1 f_1\left(a, j, k, l\right)\,\mathrm{d}a$ is simply $\displaystyle{\int_0^\frac{1}{4} f_0\left(a, j, k, l\right)\,\mathrm{d}a + \int_\frac{1}{4}^\frac{3}{4} 2\,f_0\left(a, j, k, l\right)\,\mathrm{d}a + \int_\frac{3}{4}^1 f_0\left(a, j, k, l\right)\,\mathrm{d}a}$.

Since

$$ \displaystyle{\int f_0\left(a, j, k, l\right)\,=\,\frac{{\left(L j - L k\right)} \sin\left(\frac{\pi a j}{L} + \frac{\pi a k}{L} + \frac{2 \, \pi a}{L}\right) - {\left(L j + L k + 2 \, L\right)} \sin\left(-\frac{\pi a j}{L} + \frac{\pi a k}{L}\right)}{2 \, {\left(\pi j^{2} - \pi k^{2} + 2 \, \pi j - 2 \, \pi k\right)}}} $$

your (definite) integral, which is a function of j, k and L, has an explicit (closed form) expression (which my laziness leaves to the reader as an exercise...).

HTH,