How to use polyfit to get the difference out of two functions?

조회 수: 17 (최근 30일)
Hi
I have two datasets (X,Y). For every value of Y that corresponds to a value of X.
I am thinking of applying a polyfit command to get the best curve fitting for the data, and as a result I will come up with a function (slope+intercept).
How do I find the difference between those two functions? What command do I apply to subtract Function A from Function B?
  댓글 수: 2
Walter Roberson
Walter Roberson 2019년 4월 21일
One of the functions is the one implied by polyfit(x,y,1) . But what is the other function?
A collection of points is not considered a function except in mathematical theory (in which case, the collection of ordered values is considered to define the function, and formulae are just shorthand for the collection of values.)
Stelios Fanourakis
Stelios Fanourakis 2019년 4월 21일
@Walter. The datasets are curves. If I plot them in excel and apply a polynomial curve fitting of high degree (e.g. 6) then I get the best line fit and a function. I need to get the same function in Matlab. And then subtract one function from another since the datasets are very close each other, to find their difference. How do I do that?

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

채택된 답변

Walter Roberson
Walter Roberson 2019년 4월 21일
P1 = polyfit((1:length(X)).', X(:), 6);
Likewise for Y producing P2
Pd = polyval(P1, 1:length(X)) - polyval(P2, 1:length(X))
  댓글 수: 23
Stelios Fanourakis
Stelios Fanourakis 2019년 4월 23일
I get the below error
P1 =
0 0 0 0 0 0 0
Warning: Polynomial is not unique; degree >= number of data points.
> In polyfit (line 74)
In curvefitdif (line 19)
P2 =
0 0 0 0 0 0 0
Pd =
0×1 empty double column vector
P11 =
0×1 empty double column vector
P22 =
0×1 empty double column vector
Stelios Fanourakis
Stelios Fanourakis 2019년 4월 24일
I also get the error
Error using isnan
Too many input arguments.
Error in see (line 38)
mask = isnan(x1{:,:}) | isnan(y1{:,:});
Why?

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

추가 답변 (2개)

John D'Errico
John D'Errico 2019년 4월 21일
Polynomials are linear in the coefficients. So the difference of two polynomials is obtained by just subtracting the coefficients. If they are different order polynomials, just pad the low order polynomial with zeros as necessary to correspond to the higher order coefficients that are effectively zero.
  댓글 수: 8
John D'Errico
John D'Errico 2019년 4월 23일
If pdiff gives a result of NaN, then one or both of the polynomials was already garbage. LOOK at the cofficients that you got from those two fits.
NaN can only ever result as a subtraction by things like inf - inf, or anything plus a NaN. So the problem lies in your original fits.
Stelios Fanourakis
Stelios Fanourakis 2019년 4월 23일
Actually, the problem lies where I use readtable and import the columns from excel.
They are imported as NaNs

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


Stelios Fanourakis
Stelios Fanourakis 2019년 4월 21일
Actually, both datasets are high degree polynomials e.g 6. They are almost identical and need to find their difference. How do I do that in Matlab?

카테고리

Help CenterFile Exchange에서 Curve Fitting Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by