Minimize integral with piecewise functions

I am not an experienced user of Mathematica.

I would like to find parameters $d1,d2,d3,e1,e2,e3$ which minimize such an integral (given $c11,c12,c13,c21,c22,c23,c31,c32,c33$):

$\int_{0}^{1}\int_{0}^{1} (\sum^3_{k=1} d_kN_k(\sum^3_{i=1}\sum^3_{j=1} c_{ij}N_i(x)N_j(y))-\sum_{i=1}^3 e_i(N_i(x)+N_i(y)))^2 dxdy$

where

$N_1 = 1-2x$ for $x \in [0,0.5]$ and $0$ otherwise

$N_2 = 2x$ for $x \in [0,0.5]$, $2-2x$ for $x \in (0.5, 1]$, and $0$ otherwise

$N_3 = 2x-1$ for $x \in [0.5,1]$ and $0$ otherwise

which are of course B-spline basis functions.

In Mathematica I defined such helper functions:

N0 [x_] = Piecewise[{{1 - 2*x, x >= 0 && x <= 1/2}}];

N1 [x_] =
Piecewise[{{2 x, x >= 0 && x <= 1/2}, {2 - 2 x, x > 0.5 && x <= 1}}];

N2 [x_] = Piecewise[{{2*x - 1, x >= 1/2 && x <= 1}}];

poly2d[x_, y_] =
c00*N0[x]*N0[y] + c01*N0[x]*N1[y] + c02*N0[x]*N2[y] +
c10*N1[x]*N0[y] + c11*N1[x]*N1[y] + c12*N1[x]*N2[y] +
c20*N2[x]*N0[y] + c21*N2[x]*N1[y] + c22*N2[x]*N2[y];

So code for the integral is like that:

Integrate[
(d0*N0[poly2d[x, y]] + d1*N1[poly2d[x, y]] +
d2*N2[poly2d[x, y]] - (e0*(N0[x] + N0[y]) + e1*(N1[x] + N1[y]) +
e2*(N2[x] + N2[y])))^2, {x, 0, 1}, {y, 0, 1}]

The first attempt was to use Minimize function, but it took forever. So I decided to calculate derivatives by hand. So derivative by $e1$ is like that:

$\int_{0}^{1}\int_{0}^{1} -2(N_1(x)+N_1(y))(\sum^3_{k=1} d_kN_k(\sum^3_{i=1}\sum^3_{j=1} c_{ij}N_i(x)N_j(y))-\sum_{i=1}^3 e_i(N_i(x)+N_i(y))) dxdy$

Calculating it in Mathematica takes forever as well. What is more, I tried to compute just the first part of it (and from 0 to 0.5, because I believed it will be faster/easier):

Integrate[
2*(-(N0[x] + N0[y]))*(d0*N0[poly2d[x, y]]), {x, 0, 1/2}, {y, 0, 1/2},
Assumptions -> {d0, d1, d2, e0, e1, e2, c00, c01, c02, c10, c11,
c12, c20, c21, c22} \[Element] Reals && x >= 0 && x < 1/2 &&
y >= 0 && y < 1/2 && ForAll[{x, y}, poly2d[x, y] < 1/2] &&
ForAll[{x, y}, poly2d[x, y] >= 0] ]

It takes forever as well.

Am I missing something? Maybe there is some easier/faster approach? Maybe someone has some idea how to calculate it in an analytic way?

• You are missing colons (:) in your delayed function definitions. The syntax is f[x_]:=Piecewise[...]. The integrals should be trivial to calculate, and so are the derivatives wrt. di and ei. However, unless you define constraints, you will find that all coefficients will be $\pm \infty$ because you are essentially minimizing di*const. – Felix Jan 12 '17 at 16:41
• Forget the last part of my comment, I counted the brackets wrong. Still, the integral should be straight forward once you define the functions correctly. – Felix Jan 12 '17 at 16:44