Create function that functionally same to polyfit

Create a user-defined function 'fitpoly' that has the same function as 'polyfit'
Reads the given data points from a text or Excel file and approximates the curve with an nth-order polynomial function
Set the input and output factors the same as ‘polyfit’

댓글 수: 4

Steven Lord
Steven Lord 2022년 5월 16일
편집: Jan 2022년 5월 16일
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the free MATLAB Onramp tutorial ( https://www.mathworks.com/support/learn-with-matlab-tutorials.html ) to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.
Jan
Jan 2022년 5월 16일
@종석 박: Which parts of the polyfit function should be recreated? Just the 1st output or the normalization of the input data also? Do you need the 2nd output for the error estimation?
The mathematical background is explained exhaustively here: https://en.wikipedia.org/wiki/Polynomial_regression . If only the parameters of the polynomial are wanted, this is a one-liner: x.^(n:-1:0) \ y , where x and y are column vectors. You see, there is no magic to do.
Rik
Rik 2022년 5월 17일
Unfortunately for you, Google had a cached version of this page from before you attempted to edit away your homework question.
It is now also on the Wayback Machine.
Sam Chak
Sam Chak 2022년 5월 17일
편집: Sam Chak 2022년 5월 17일
I think the Professor won't penalize @종석 박 so long as he acquired the regression analysis knowledge (cognitive) and the coding skills (psychomotor) as required by the assignment and outlined in the Outcome-Based Education (OBE). The Professor will be happy when writing the Education Report at the end of the academic term. As long as there is no blatant plagiarism elements, then he should be "SAFE".
Nevertheless Park will still have to produce the math as shown in Polynomial Regression.

댓글을 달려면 로그인하십시오.

답변 (3개)

Image Analyst
Image Analyst 2022년 5월 17일

1 개 추천

There is still another way @종석 박 can do the assignment without plagiarizing. He can construct the equation like
Ax = y
using a for loop where A =
x(1)^n, x(1)^(n-1), x(1)^(n-2), ....., x(1), 1
x(2)^n, x(2)^(n-1), x(2)^(n-2), ....., x(2), 1
x(3)^n, x(3)^(n-1), x(3)^(n-2), ....., x(3), 1
...
x(m)^n, x(m)^(n-1), x(m)^(n-2), ....., x(m), 1
Then once you have the tall array you can do
coefficients = A \ y(:)
which is basically the method for doing least squares regression "manually".
Sam Chak
Sam Chak 2022년 5월 16일
I find it a little strange. Should your Intructor/Professor send you to learn the essentials through MATLAB Onramp at the beginning of the course in MATLAB?
From your description, it seems that your Intructor/Professor wants you to study the algorithm in polyfit.m.
This example only has 3 points, (1, 4), (2, 7), (3, 14). Try to improvise from here.
% Formulate the problem as a linear equation, A*x = b
P = [1 4; 2 7; 3 14]
x = P(:, 1)
A = [x.^2 x repelem(1, 3, 1)]
b = P(:, 2)
x = A\b
Image Analyst
Image Analyst 2022년 5월 16일
@종석 박 did you try what Jan told you?
function coefficients = myPolyFit(x, y, n)
coefficients = x(:) .^ (n : -1 : 0) \ y(:);
end
If not, why not?

카테고리

도움말 센터File Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

질문:

2022년 5월 16일

편집:

2022년 6월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by