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

9 ビュー (過去 30 日間)
Stelios Fanourakis 2019 年 4 月 20 日
コメント済み: Stelios Fanourakis 2019 年 4 月 24 日
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 件のコメント表示非表示 1 件の古いコメント
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 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 件のコメント表示非表示 22 件の古いコメント
Stelios Fanourakis 2019 年 4 月 24 日
I also get the error
Error using isnan
Too many input arguments.
Error in see (line 38)
Why?

サインインしてコメントする。

### その他の回答 (2 件)

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 件のコメント表示非表示 7 件の古いコメント
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 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?
##### 1 件のコメント表示非表示 なし
John D'Errico 2019 年 4 月 21 日
Did I not say just how to do that in my answer?

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!

Translated by