Polyfit not showing a curve

8 ビュー (過去 30 日間)
Jamie Ford
Jamie Ford 2019 年 10 月 8 日
コメント済み: Star Strider 2019 年 10 月 8 日
Trying to plot a curve using polyfit. This is my code.
% store coordinates
x = [5.2 5.5 5.6 5.8];
y = [6.408 16.125 19.816 27.912];
% use polyfit to get coefficients of cubic
p = polyfit(x, y, 3);
scatter(x, y, 'black');
hold on
plot(p, 'green');
When plotted, p is jagged and looks nothing like a cubic

採用された回答

Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2019 年 10 月 8 日
polyfit only get you the coefficients, you then have to actually evaluate some values to get the curve. An easy alternative is to use the function polyval (https://de.mathworks.com/help/matlab/ref/polyval.html), which will evaluate the given polynom for the given points. The following code should give you the result you would expect:
% store coordinates
x = [5.2 5.5 5.6 5.8];
y = [6.408 16.125 19.816 27.912];
% use polyfit to get coefficients of cubic
p = polyfit(x, y, 3);
yfit = polyval(p,x); % Evaluate polynomial coefficients p in values x
scatter(x, y, 'black');
hold on
plot(x,yfit, 'green');

その他の回答 (1 件)

カテゴリ

Help Center および File ExchangeDiscrete Data Plots についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by