# Least square with rectangular function

I have the function $c(t) = A \cdot \cos \left(\dfrac{2\pi}{\tau} \cdot t + \phi \right)$ which is used to define $T(t) = \begin{cases} M + c(t), & c(t) > 0 \\ M, &c(t) \leq 0. \end{cases}$

$T(t)$ is used to fit in the least square sense to measurements $y(t)$. Now, the question is how to get the partial derivative of this piece wise defined function?

The function is not continuous, therefore I thought to approximate the rect function through a Fourier series in order to calculate the derivative required by the least square procedure:

$f(t_i, \gamma) = M + \left[ \dfrac{4h}{\pi} \sum_{k = 1}^{n}{\dfrac{\sin \left( (2k-1)w(t+\Delta t) \right)}{2k-1}} \right] \cdot A \cdot \cos (\omega t+\phi)$ $f(t_i, \gamma) = M + \left[ \dfrac{4h}{\pi} \sum_{k = 1}^{n}{x_k\alpha_k+z_k\beta_k} \right] \cdot A \cdot \cos (\omega t+\phi)$

with $\alpha_k = A_k\cos(\phi_k),\ \beta_k=A_k\sin(\phi_k),\ x_k = \sin(\omega_kt),\ z_k=\cos(\omega_kt)$ and

$S = \sum r_i^2$ with $r_i = T_i - f(t_i, \gamma),\$ where the adjustable parameters are held in the vector $\gamma$.

Is this the right way or should I continue with the first try with the piecewise defined function and how to proceed?

@Choward: You're right, it was an idea how to avoid the piecewise derivative. Let's rewrite the equation above to the form

$T(t) = M + \alpha\cdot x + \beta\cdot z + e\left(t\right)$

with

$\alpha = A \cdot \cos \left(\phi \right)$, $\beta = -A \cdot \sin \left(\phi \right)$, $x = \cos \left(2 \cdot \pi \cdot \frac{t}{\tau}\right)$ and $z = \sin \left(2 \cdot \pi \cdot \frac{t}{\tau}\right)$.

For all partial derivatives then I would get:

$\dfrac{\partial f(t_i, \gamma)}{\partial M} = 1$ for $c(t) > 0$

$\dfrac{\partial f(t_i, \gamma)}{\partial (A\cos(\phi))} = \cos(\frac{2\pi t}{\tau})$ for $c(t) > 0$

$\dfrac{\partial f(t_i, \gamma)}{\partial (-A\sin(\phi))} = \sin(\frac{2\pi t}{\tau})$ for $c(t) > 0$

and

$\dfrac{\partial f(t_i, \gamma)}{\partial M} = 1$ for $c(t) < 0$

$\dfrac{\partial f(t_i, \gamma)}{\partial (A\cos(\phi))} = 0$ for $c(t) < 0$

$\dfrac{\partial f(t_i, \gamma)}{\partial (-A\sin(\phi))} = 0$ for $c(t) < 0$

Now I have two sets of equations, one for $c(t) >0$ and $c(t) < 0$. To proceed I continue as following:

$\dfrac{\partial S}{\partial \gamma_i} = 2 \cdot \sum{r_i \dfrac{\partial r_i}{\partial \gamma _j}} \ \forall j \\ \rightarrow 0 = -2 \cdot \sum{r_i \dfrac{\partial f(T_i, \gamma)}{\partial \gamma _j}} \ \forall j$

and get for $c(t) > 0$

$0 = -2 \cdot \sum_{i}^{N} \left(T_i - \alpha x_i - \beta z_i \right) \cdot \frac{\partial f(t_i, \gamma)}{\partial M} \\ \Leftrightarrow\sum_{i}^{N}{T_i} = M \cdot N + \alpha \cdot \sum_{i}^{N}{x_i} + \beta \cdot \sum_{i}^{N}{z_i}\\ 0 = -2 \cdot \sum_{i}^{N} \left(T_i - \alpha x_i - \beta z_i \right) \cdot \frac{\partial f(t_i, \gamma)}{\partial \alpha} \\ \Leftrightarrow\sum_{i}^{N}{T_i\cdot x_i} = M \cdot \sum_{i}^{N}{x_i} + \alpha \cdot \sum_{i}^{N}{x_i^2} + \beta \cdot \sum_{i}^{N}{x_i\cdot z_i} \\ 0 = -2 \cdot \sum_{i}^{N} \left(T_i - \alpha x_i - \beta z_i \right) \cdot \frac{\partial f(t_i, \gamma)}{\partial \beta} \\ \Leftrightarrow \sum_{i}^{N}{T_i\cdot z_i} = M \cdot \sum_{i}^{N}{z_i} + \alpha \cdot \sum_{i}^{N}{x_i\cdot z_i} + \beta \cdot \sum_{i}^{N}{z_i^2}$

and for $c(t) < 0$

$0 = -2 \cdot \sum_{i}^{N} \left(T_i - \alpha x_i - \beta z_i \right) \cdot \frac{\partial f(t_i, \gamma)}{\partial M} \\ \Leftrightarrow\sum_{i}^{N}{T_i} = M \cdot N + \alpha \cdot \sum_{i}^{N}{x_i} + \beta \cdot \sum_{i}^{N}{z_i}\\ 0 = -2 \cdot \sum_{i}^{N} \left(T_i - \alpha x_i - \beta z_i \right) \cdot \frac{\partial f(t_i, \gamma)}{\partial \alpha} \\ \Leftrightarrow = 0 \\ 0 = -2 \cdot \sum_{i}^{N} \left(T_i - \alpha x_i - \beta z_i \right) \cdot \frac{\partial f(t_i, \gamma)}{\partial \beta} \\ \Leftrightarrow = 0.$

For $c(t) > 0$ I can express it using $d = B \cdot u$ $\begin{pmatrix} \sum_{i}^{N}{T_i} \\ \sum_{i}^{N}{T_i\cdot x_i} \\ \sum_{i}^{N}{T_i\cdot z_i} \end{pmatrix} = \begin{pmatrix} N & \sum_{i}^{N}{x_i} & \sum_{i}^{N}{z_i} \\ \sum_{i}^{N}{x_i} & \sum_{i}^{N}{x_i^2} & \sum_{i}^{N}{x_i \cdot z_i} \\ \sum_{i}^{N}{z_i} & \sum_{i}^{N}{x_i \cdot z_i} & \sum_{i}^{N}{z_i ^2} \\ \end{pmatrix} \cdot \begin{pmatrix} M \\ \alpha \\ \beta \end{pmatrix}$

and solve it easily with $u = B^{-1} \cdot d$. For $c(t) < 0$ this does not make sense... How to take this into account?

• What adjustable parameters does $\gamma$ represent in your model? – spektr Feb 9 '17 at 18:31
• In my opinion it would be better to define $T(t) = M + H(c(t))$, where $H(\cdot)$ is a Heaviside step function, and just approximate $H(\cdot)$ using something like the sigmoid function to ensure the derivatives are continuous. – spektr Feb 9 '17 at 19:47
• $\gamma$ contains $M$, $A\cos(\phi)$ and $-A\sin(\phi)$ – FixedFilip Feb 11 '17 at 23:01

Is there a reason you considered the Fourier series? It actually seems fairly simple to compute $\frac{\partial T}{\partial t}$ as a piecewise function of $t$:
$$\frac{\partial T}{\partial t} = \left\{ \begin{matrix} \frac{\partial c}{\partial t} & c(t) > 0 \\ 0 & c(t) < 0 \end{matrix} \right. = \left\{ \begin{matrix} -\frac{2\pi}{\tau} A \sin( \frac{2\pi}{\tau} t + \phi) & c(t) > 0 \\ 0 & c(t) < 0 \end{matrix} \right.$$
So, from here, you just need to figure out the values of $t$ for which $c(t)$ is positive/negative.