polynomial regression of a given function
3 次查看(过去 30 天)
显示 更早的评论
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;
0 个评论
采纳的回答
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,'-')
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Polynomials 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!