필터 지우기
필터 지우기

polynomial regression of a given function

조회 수: 3 (최근 30일)
Anas Gharsa
Anas Gharsa 2022년 1월 25일
댓글: Anas Gharsa 2022년 1월 25일
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일
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개)

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by