Solving a linear least squares problem with trigonometric functions We want to calculate the amplitude $A$ and the phase angle $\phi$ of the oscillation $b(t)=A\sin(2t+\phi)$.
We have $t_k=(0,\pi/4, \pi/2, 3\pi/4)$ and $b_k=(1.6,1.1,-1.8,0.9)$
Use $\sin(A+B)=\sin(A)\cos(B)+\cos(A)\sin(B)$ and $\alpha=A\cos(\phi), \beta=A\sin(\phi)$ to get a linear problem.
We get $b(t)=\alpha\sin(2t)+\beta\cos(2t)$
Using the above, we get $b^T=A (\alpha, \beta)^T$
Using QR and/or normal equation [ code:https://hastebin.com/otezejobaj.pl ] we get $\alpha=0.1, \beta=1.7$
Now, I should write down the residual vectors for QR and for the normal equation
Question 1: Are the residual vectors here: $Ax_1 - b$ and $Ax_2-b$ with $x_1=(\alpha,\beta)$ from QR Method and $x_2=(\alpha,\beta)$ from the normal equation. (It's the same result here)?
Now, I should calculate $A$ and $\phi$. How should I do that numerically? Also, I noted the following:
(1)$\beta = A\sin(\phi) \Rightarrow 1.7=a\sin(\phi)$ and $b(0)=A\sin(\phi)=1.6$ which can't be.
Question 2: Is there a reason that (1) isn't legit?
Edit: Since I'm in a least square problem, I can't actually expect (1) to work, right? Anyway, Question 1 is the important question here.
 A: The four residuals are $$b(t_k)-b_k$$ evaluated with the computed $\alpha,\beta$, which you can group as a sum of squares
$$\sum_{k=1}^4(b(t_k)-b_k)^2.$$
Also,
$$A=\sqrt{\alpha^2+\beta^2},\\\tan\phi=\frac\beta\alpha.$$
A: @Yves Daoust answers the issues succinctly. It is worthwhile to make the linear algebra more explicit.
The trial function is $y(t) = \alpha \sin (2t) + \beta \cos (2t)$, and the linear system is
$$
\left[
\begin{array}{ll}
  \sin 2t_{1} & \cos 2t_{1} \\
  \sin 2t_{2} & \cos 2t_{2} \\
  \sin 2t_{3} & \cos 2t_{3} \\
  \sin 2t_{4} & \cos 2t_{4}
\end{array}
\right]
%
\left[
\begin{array}{l}
  \alpha \\
  \beta
\end{array}
\right]
=
\left[
\begin{array}{ll}
  b_{1} \\
  b_{2} \\
  b_{3} \\
  b_{4}
\end{array}
\right].
$$
How to solve this system? Normal equations, $\mathbf{Q}\mathbf{R}$, SVD? A place to start is the product matrix:
$$
\mathbf{A}^{\mathrm{T}}\mathbf{A} = 
\left[
  \begin{array}{rrrr}
  0 & 1 &  0 & -1 \\
  1 & 0 & -1 &  0
  \end{array}
\right]
%
\left[
\begin{array}{rr}
   0 &  1 \\
   1 &  0 \\
   0 & -1 \\
  -1 &  0
\end{array}
\right]
=
\left[
\begin{array}{cc}
   2 &  0 \\
   0 &  2
\end{array}
\right].
$$
The condition number of this matrix is unity - the best possible case. Without the need to resolve an ill-conditioned system, we don't need to us the $\mathbf{Q}\mathbf{R}$ or singular value decompositions.
The rank of the system matrix $\mathbf{A}$ is $\rho=2$. One way to see this is to note the two columns are linearly independent. The number of rows $m=4>\rho$, so the null space $\mathcal{N}(\mathbf{A})$ is trivial so $\mathbf{A}^{\dagger}\mathbf{A} = \mathbf{I}_{2}.$ The least squares solution
$$
  \left[
    \begin{array}{c}
      \alpha \\ \beta
    \end{array}
  \right]_{LS}
=
\mathbf{A}^{\dagger}b + \left(\mathbf{I}_{2} - \mathbf{A}^{\dagger}\mathbf{A} \right)y, \quad y\in\mathbb{C}^{2}
$$
is a unique point and
$$
  \mathbf{A}^{\dagger} = \left( \mathbf{A}^{\mathrm{T}}\mathbf{A} \right)^{-1} \mathbf{A}^{\mathrm{T}}.
$$
Postscript: see the more general problem at Trigonometrical Least Squares (Linear Algebra)
