# 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.
{"ft_lang_label":"__label__en","ft_lang_prob":0.54306185,"math_prob":1.000005,"size":4373,"snap":"2019-51-2020-05","text_gpt3_token_len":1761,"char_repetition_ratio":0.23506524,"word_repetition_ratio":0.27492446,"special_character_ratio":0.43082553,"punctuation_ratio":0.05528846,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":1.0000099,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-20T21:02:16Z\",\"WARC-Record-ID\":\"<urn:uuid:c047e12b-2345-4fb4-82b7-ad32668e6308>\",\"Content-Length\":\"140547\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:589e4f32-0b1f-467c-bbc9-a8b81ccfb831>\",\"WARC-Concurrent-To\":\"<urn:uuid:f9a26613-ed34-45a4-a7f4-7f7cbbeb6f81>\",\"WARC-IP-Address\":\"151.101.193.69\",\"WARC-Target-URI\":\"https://scicomp.stackexchange.com/questions/26144/least-square-with-rectangular-function/26146\",\"WARC-Payload-Digest\":\"sha1:WGITI7APXMWV5ETMBHEJS4TW2JMASESI\",\"WARC-Block-Digest\":\"sha1:EVQQIUHCXDMX2XHYW76BCVGKL2XBUMXL\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579250599789.45_warc_CC-MAIN-20200120195035-20200120224035-00541.warc.gz\"}"}