Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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$?

share|cite|improve this question
You know how to do a linear regression in MATLAB? – J. M. Dec 3 '11 at 6:02
No, I do not. I don't understand the answer given at all. – Randy Dec 7 '11 at 16:27
You know how to solve a least-squares problem, don't you? – J. M. Dec 7 '11 at 16:28
By hand. I don't see how that applies to this – Randy Dec 7 '11 at 16:45
Okay, I added a hint to my answer. – J. M. Dec 7 '11 at 16:53
up vote 1 down vote accepted

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:


share|cite|improve this answer
Thank you. Got it. – Randy Dec 7 '11 at 17:36

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.