How to code a basic fitting quadratic in this?

Hi everyone,
I am using two scatter plots. How can I code the quadratic fitting in that? I tried more than 7 ways on the internet and it's not working. It does not work with polyfit n=5. It just turns into a mesy multiple lines!
Thanks
s
ScatterPlotPreCOVID = scatter(x,y,'o')
hold on
ScatterPlotPostCOVID = scatter(x1,y1,'^')
hold off
Now, I do not insist coding it into it but I cannot have both quadratics simultanously for two datasets from the tools too.

 採用された回答

Walter Roberson
Walter Roberson 2021 年 2 月 2 日

1 投票

P = polyfit(x, y, 2);
The approximation function is then P(1)*x^2 + P(2)*x + P(3)

5 件のコメント

Wolfgang McCormack
Wolfgang McCormack 2021 年 2 月 2 日
編集済み: Wolfgang McCormack 2021 年 2 月 2 日
@Walter Roberson thank you so much. Three values are calculated for P as array. Now how do I plot it? I want it for both data sets for comparison
I am new and not very much familiar with statistical concepts so I didnot understand what the approximation function is used for
Walter Roberson
Walter Roberson 2021 年 2 月 2 日
P = polyfit(x, y, 2);
XI = linspace(min(x), max(x));
YI = polyval(P, XI);
plot(XI, YI, 'k-');
Wolfgang McCormack
Wolfgang McCormack 2021 年 2 月 3 日
@Wolfgang McCormack oh THANK YOUUUUU SOOOO MUCHHHHHHHHHHHH! I really don't know how to thank you! I was really frustrated after all my tries. How can I pick your answer as the correct answer?
Walter Roberson
Walter Roberson 2021 年 2 月 3 日
You already Accepted my answer, earlier ;-)
Walter Roberson
Walter Roberson 2021 年 2 月 3 日
By the way, the way to do it without polyval would have been
P = polyfit(x, y, 2);
XI = linspace(min(x), max(x));
YI = P(1).*XI.^2 + P(2).*XI + P(3);
plot(XI, YI, 'k-');

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeGet Started with Curve Fitting Toolbox についてさらに検索

製品

リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by