Given the equation
$r = B + e(r\cos(\theta))$
and the corresponding data:
$\theta: 0.88; 1.1; 1.42; 1.77; 2.14$ and $r: 3; 2.4; 1.65; 1.25; 1.01$
How do you input these data for matlab to solve for $B$ and $e$?
Given the equation
$r = B + e(r\cos(\theta))$
and the corresponding data:
$\theta: 0.88; 1.1; 1.42; 1.77; 2.14$ and $r: 3; 2.4; 1.65; 1.25; 1.01$
How do you input these data for matlab to solve for $B$ and $e$?
Cleve Moler's Numerical Computing with MATLAB has this excellent chapter on how to do least squares; you can adapt any of the methods discussed there. To give you a few nudges on how to do your code: you have r
as a dependent variable, and you can construct a new independent variable rc=r.*cos(theta);
, where r
and theta
are appropriately constructed arrays. You can use []
to form the columns of the matrix required for the linear regression, and then use \
to get the least squares solution.
Or, there's polyfit()
...
OP seems to have some difficulty seeing how least squares applies here, so here's a hint on how to assemble the equations:
$\begin{pmatrix}1&r_1\cos\,\theta_1\\1&r_2\cos\,\theta_2\\1&r_3\cos\,\theta_3\end{pmatrix}\begin{pmatrix}B\\e\end{pmatrix}=\begin{pmatrix}r_1\\r_2\\r_3\end{pmatrix}$