How do I find the best fit line of data without the polyfit command?

5 views (last 30 days)
How would I find the coefficients of a set of data using linear algebra, so without using the polyfit command?
WELL SORRY I DIDN'T MENTION THIS WAS A HOMEWORK PROBLEM PEOPLE YA'LL DON'T HAVE TO BE SNOTTY SORRY FOR WANTING SOME HELP.
  3 Comments
Jan
Jan on 5 Oct 2017
Edited: Jan on 5 Oct 2017
Sorry, Paige, this is the wrong way to use the forum.
Your question was vague only: you did neither explain the available inputs, nor the wanted outputs. "Using linear algebra" is not meaningful - the problem cannot be solved without it at all. You did not mention, that it is a homework, and this is not liked here.
Show any own effort. Ask clearly for what you want to implement. Post the existing code and explain, what is not working yet. It is not clear, if you want to write the QR or LU factorization by your own. But do not let the readers guess this.
But two persons took the time to write answers. Then it is a good idea to say "thanks" and to refine the question, if the problem is not solved yet. It is a bad choice to rant instead.
A short search in the forum or the internet would reveal the needed algorithm and even complete Matlab code.
John BG
John BG on 5 Oct 2017
hi Paige
If Paige could provide the answer on her own, why would then be writing a question in this forum anyway?
The MATLAB forum is open to all sort of MATLAB questions.
Whether a question is homework or not is considered by some forum members as a valid reason not to provide a complete fully working answer.
Some other members consider that hints are not answers, therefore hints should be considered COMMENTS only.
Answers solve questions, hints and comments may point on the right direction.
Paige, the right approach here is Jan Simon's, just open polyfit and learn how does it work
type polyfit
If you still don't want to use command qr, qr is not an open function, then you may want to consider writing or asking for orthogonal-triangular decomposition.

Sign in to comment.

Answers (2)

Jan
Jan on 4 Oct 2017
Edited: Jan on 4 Oct 2017
But polyfit uses linear algebra methods also. Or is this a homework question and you should program this by your own? Then it is required to mention this, because now you let the forum do this for you.
Start with looking into the code of polyfit. The core is:
% Construct Vandermonde matrix:
x = x(:);
V = ones(length(x), n + 1);
for j = n:-1:1
V(:, j) = V(:, j + 1) .* x;
end
% Solve least squares problem - equivalent to (V \ y)':
[Q, R] = qr(V, 0);
p = transpose(R \ (transpose(Q) * y(:)));
Is using this what you call "without polyfit"? If so, omitting the security checks, the output of the 2nd output and the documentation is a light way to "programming by your own".

Star Strider
Star Strider on 4 Oct 2017
This is a fairly trivial problem in MATLAB. I will give you one hint:
DM = (x(:) * ones(1,N+1)) .^ (N:-1:0); % Build Design Matrix
where ‘N’ is the polynomial order, and ‘x’ is the independent variable.
You can figure out the rest.

Categories

Find more on Linear Algebra in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!