% begin preamble
\documentclass[11pt,titlepage]{article}
\usepackage[margin=1in,noheadfoot]{geometry}
\usepackage[parfill]{parskip}
\usepackage{graphicx}
\usepackage[colorlinks,urlcolor=blue]{hyperref}
\usepackage{amssymb}
\usepackage{amsbsy}
\usepackage{wrapfig}

\newcommand{\e}[1]{\times 10^{#1}}
\newcommand{\q}{\mathrm}
\newcommand{\where}[1][where]{\qquad\mbox{#1}\qquad}
\newcommand{\bs}[1]{\boldsymbol{#1}}
\newcommand{\bh}[1]{\hat{\boldsymbol{#1}}}
\newcommand{\bv}[1]{\vec{\boldsymbol{#1}}}
\newcommand{\bt}[1]{\tilde{\boldsymbol{#1}}}
\newcommand{\ad}{^{\dagger}}
\newcommand{\flow}[1]{\mathcal{E}_{#1}}
\newcommand{\flux}[1]{\Phi_{#1}}
\newcommand{\dslash}{d \hspace{-0.5ex}\rule[1.2ex]{0.6ex}{.1ex}}
\newcommand{\ts}{\textsuperscript}
\def\rcurs{{\mbox{$\resizebox{.16in}{.08in}{\includegraphics{ScriptR}}$}\!\!}}
\def\brcurs{{\mbox{$\resizebox{.16in}{.08in}{\includegraphics{BoldR}}$}\!\!}}
\def\hrcurs{{\mbox{$\hat \brcurs$}\!}}
\newcommand{\var}{\mbox{Var}}
\newcommand{\cov}{\mbox{Cov}}
%end preamble

\begin{document}
\begin{center}
  {\bf University of Kentucky, Physics 335 \\
    Homework \#4, Rev. B, due Thursday, 2021-11-17 }
\end{center}

\vspace{.3 in}

{\bf 1. Linear fit.}  Fit the following data with uncertainties to a straight line:

% \begin{wrapfigure}[10]{r}{3in}
\begin{center}
\begin{tabular}{l|r|r@{$\;\pm\;$}l}
  $i$ & $x_i$ & $y_i$ & $\delta y_{i}$ \\
  \hline
  1 & -3 & 20 & 3 \\
  2 & -1 & 30 & 4 \\
  3 &  1 & 25 & 5 \\
  4 &  3 & 35 & 6 \\
\end{tabular}
\hspace{1 in}
\includegraphics[width=1.5in, trim=0 5.5in 0 2in]{H04_fig1}
% \end{wrapfigure}
\end{center}

{\bf \quad a)} Calculate the $n\times m$ {\it design matrix} $X=[x^{0} ~ x^{1}]$ for the linear fit $y=f(x)=a+bx$  for $n=4$ data points to $m=2$ fit parameters, and write out the corresponding matrix equation $Y=Xa$.  Calculate the weight [metric] $W=\delta^{-2}Y$, (inverse covariance matrix), the least squares inverse $X^{\dashv}=(X^{T}WX)^{-1}X^{T}W$, the fit coefficients $a=X^{\dashv} Y$, and covariance matrix $\delta^{2}a$.  Tabulate the residuals $\chi=Y-Xa$, individual squares $\chi_{i}^{2}=((y_{i}-f(x_{i}))/\delta y_{i})^{2}$, and the sum $\chi^{2}=\chi^{T}W\chi$.  How many degrees of freedom $\nu$ are there? %  What is the likelihood that $\chi^{2}$ is greater than the fitted value?

{\bf \quad b)} [{\it bonus}: Neglecting the uncertainties for simplicity, solve for $X^{\dashv}$ by computing the \href{https://en.wikipedia.org/wiki/Singular_value_decomposition}{Singular Value Decomposition} (SVD)  $X=UWV^{T}$ of the $n\times m$ matrix $X$, where $U$ and $V$ are orthogonal matrices of the {\it singular vectors} $U^{T}U=I_{n}$, $V^{T}V=I_{m}$, and $W$ is the $m\times n$ diagonal matrix of {\it singular values} (eigenvalues) of $X$.  Show algebraically that $X^{\dashv}=VW^{\dashv}U^{T}$, where $W^{\dashv}$ is the $m\times n$ diagonal matrix of inverse singular values.  What is the geometric interpretation of the left and right singular vectors?  Repeat with $\delta^{2} Y$ using the Generalized SVD. ]


{\bf 2.} The following {\bf waveform data} in mV were captured on an oscilloscope with a $\Delta t=$1~ms sample interval, from the voltage output of a magnetometer measuring the field of an AC-driven coil: \\
\verb|   V= [3.81, 2.96, 2.36, 2.40, 0.47, 2.29, 0.51, -0.06, -2.00, -0.42, 0.06, |\ldots \\
\verb|       0.57, 0.29, 2.07, 2.47, 2.86, 2.78, 1.86,  1.37, -0.43,  0.03]| \\
Fit these data for the amplitude $A$, angular frequency $\omega$, phase $\phi$, and offset $B$ of a sine wave in the semilinear model $V(t)=A\cos(\omega t - \phi)+B$ ignoring errors using the following steps:

% {\bf \quad a)} Plot the \href{https://en.wikipedia.org/wiki/Fast_Fourier_transform}{\it Fast Fourier Transform} (FFT) of the waveform to estimate its frequency $\omega$.  This is the only nonlinear fit parameter in the model $V=a+b\cos(\omega t + \phi)$.

{\bf \quad a)} Plot the data and fit function, estimating of all four parameters (Note the data span approximately one period, ranging from the peak $V\approx 2$~mV, to the minimum $V\approx 0$~ms and back to the top). [{\it bonus}: estimate $A$, $\phi$, $\omega$, and $B$ from the complex \href{https://en.wikipedia.org/wiki/Fast_Fourier_transform}{FFT} of $V$.]

{\bf \quad b)} Linearize this model to $V=B+C\cos(\omega t)+D\sin(\omega t)=Ta$
for constant $\omega$ using the addition formula
$\cos(\alpha-\beta)=\cos(\alpha)\cos(\beta)+\sin(\alpha)\sin(\beta)$.  Follow the steps in 1a) to perform a linear
fit on $V \approx Ta$ for parameters $a=(B C D)^{T}=T^{\dashv}V$, and calculate $\chi^{2}=(V-Ta)^{T}(V-Ta)$.

{\bf \quad c)} Fit for $\omega$ by minimizing $\chi^{2}$ as a function of $\omega$.  [{\it bonus}: fit $\chi^{2}(\omega)$ to a parabola]  Calculate the fitted fitted amplitude, phase, and offset from the corresponding fit parameters $a$ and  plot $V(t)$.  Estimate the uncertainty $\delta V$ of the voltage data by setting $\chi^{2}=\nu=21-4$ the number of degrees of freedom.  [{\it bonus}: find covariance matrix of the fit parameters from $\delta^{2}a$.]

{\bf \quad c)} [{\it bonus}:  Fit a histogram of the residuals to the Gaussian $p_{G}(x)=N\exp(-((x-\mu)/\sigma)^{2}/2)$ by linearizing it to the quadratic form $\ln(p_{G})=a+bx+cx^{2}$ and performing a linear fit on the binned $\chi^{2}$ data for $(a~ b~ c)^{T}$.  Compare your fitted values of $\mu$ and $\sigma$ with the mean and residual of the distribution.  Are they within error of what you expect, given $\chi^{2}$ above?  What is the likelihood of $\chi^{2}$ being greater than the fitted value?


\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:
