Least Squares Problem

In [1]:
import math as m
import numpy as np
import matplotlib.pyplot as plt


Write a function least_squares(d) which takes as input a dictionary d mapping floats to floats. The dictionary represents some finite collection of data points (x, y) given by those pairs for which d[x]=y. The function least_squares(d) should return a pair (a, b) determining a function of the form $$f(x) = a \cos(x) + b \sin(x),$$ which minimizes the $\ell^2$-distance from d: $$\text{dist}(f,d) = \sqrt{ \sum_{y=d[x]} \big(f(x)-y\big)^2}.$$

In [ ]: