curve of best fit

6 ビュー (過去 30 日間)
Nana Kwaku Antwi-Darkwah
Nana Kwaku Antwi-Darkwah 2022 年 5 月 19 日
コメント済み: Bjorn Gustavsson 2022 年 5 月 20 日
I want to get a curve of best fit that looks like this
But after testing a number of curve fitting methods I have not been able to obtain a curve such as the one above. For example when I use polynomial curve fitting the resulting curves I get, even after increasing the polynomial degree, are nothing like my desired curve:
n = 2
n = 3
Please can anyone offer me assistance with this ?
load ('eddy.mat')
load ('x1.mat')
coefficients = polyfit(eddy, x1, 2);
numFitPoints = 100;
xFit = linspace(min(eddy), max(eddy), numFitPoints);
yFit = polyval(coefficients, xFit);
scatter(eddy,x1,'d','filled','k')
hold on
grid on
plot(xFit,yFit,'r-','LineWidth',2)
x([0 0.01])
y([0 0.5])
hold off

採用された回答

Bjorn Gustavsson
Bjorn Gustavsson 2022 年 5 月 19 日
In order to get a function that has two y-values for for some range of x-values you will cannot fit a polynomial such that y=p(x). If you instead fit a polynomial such that x=p(y) then you might produce a curve that you've sketched. So try something like:
px_of_y = polyfit(y,x,2);% or some higher order polynomial.
y_i = linspace(0,1,101);
x_2 = polyval(px_of_y,y_i);
plot(x_2,y_i)
HTH
  2 件のコメント
Nana Kwaku Antwi-Darkwah
Nana Kwaku Antwi-Darkwah 2022 年 5 月 19 日
n=2
thank you so much, this is close enough to what I was looking for. The curve resembled this from polynomial degrees of 2 to 4. Is there any way to force the curve to at least pass over (/ go inbetween/ get closer to) the first 2 points without increasing the degree past 4. For degrees of 5 and above I get graphs that have more than one peak:
n = 5
Bjorn Gustavsson
Bjorn Gustavsson 2022 年 5 月 20 日
If you know that you have different uncertainties (expected/known standar deviation of the different points not the same) there are a couple of poly-fit variants on the file exchange, look there for polyfit3 (one I've used). You can also "roll your own" least-square fitting-function with lscov (but then you'll have to set up your own matrix for the least-square fitting and such).
HTH

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangePolynomials についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by