polynomial regression of a given function

i am trying to write a function which produces the regression polynomial of degree 2 for a given datapoints (x and f(x) where f is the function to be approximated). this is what i wrote !! the code gives me 0 errors but i can not plot the results for some reasons that i dont know
x=input('Enter the x coordinates');
y=input('Enter the y coordinates');
n=input('Enter the order of the polynomial');
coeff=polyfit(x,y,n);
p = polyfit(x,y,n);
plot p;

 採用された回答

Jon
Jon 2022 年 1 月 25 日

0 投票

You only solved for the coefficients. If you want to compare the original data and the fit you can do something like this, after your code above (I assume the x values are entered in increasing order)
xfit = linspace(x(1),x(end))
yfit = polyval(p,xfit);
plot(x,y,'o',xfit,yfit,'-')

1 件のコメント

Anas Gharsa
Anas Gharsa 2022 年 1 月 25 日
Yes that's right!! i intend to enter the x values in increasing order!!! Thank you so much this is so helpful

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLinear and Nonlinear Regression についてさらに検索

製品

リリース

R2021b

質問済み:

2022 年 1 月 25 日

コメント済み:

2022 年 1 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by