polyfitn is an extension of polyfit, allowing the user to create models with more than one independent variable. it also allows the user to specify a general model, for example, a quadratic model, with constant and quadratic terms, but no linear term.
for example, to fit a polynomial model to points selected from a cosine curve, we will only need the even ordered terms.
x = -2:.1:2;
y = cos(x);
p = polyfitn(x,y,'constant x^2 x^4 x^6');
p.coefficients
ans =
[0.99996 -0.49968 0.041242 -0.0012079]
the coefficients won't be exact of course, as i used only a finite number of terms for what is essentially a truncated taylor series, and i had only a finite amount of points to build the model from. the exact first 4 coefficients for a cosine series would have been:
>> [1 -1/2 1/24 -1/720]
ans =
1 -0.5 0.041667 -0.0013889
so we got the expected result.
of course, polyfitn works in higher dimensions, as it was this problem it was really designed to solve.
x = rand(100,1);
y = rand(100,1);
z = exp(x y) randn(100,1)/100;
p = polyfitn([x,y],z,3);
the result can be converted into a symbolic form to view the model more simply. here i'll use my sympoly toolbox, but there is also a polyn2sym function provided.
polyn2sympoly(p)
ans =
0.43896*x1^3 1.4919*x1^2*x2 0.041084*x1^2 1.4615*x1*x2^2 - 0.095977*x1*x2 1.2799*x1 0.56912*x2^3 - 0.15306*x2^2 1.361*x2 0.94819
and of course, parameter error estimates are generated for those who wish to determine the significance of the terms generated.
i've also provided tools for evaluating these models, and to differentiate a model.
a caveat - beware the use of high order polynomials to fit your data. just because a low order model works, a high order model is not necessarily better. high order polynomials often suffer from severe ringing between the data points. always plot your data. think about the model you will be building. then plot the resulting model. use your eyes to validate the result, more so than simply looking at an r-squared coefficient (although i do return that parameter too.)
if you do find that a high order polynomial mode is necessary because your curve is simply too complicated, consider using a regression or smoothing spline model instead.
引用格式
john d'errico (2023). polyfitn (https://www.mathworks.com/matlabcentral/fileexchange/34765-polyfitn), matlab central file exchange. 检索来源 .
matlab 版本兼容性
平台兼容性
windows macos linux类别
- > > >
- > > >
- > > > >
标签
致谢
启发作品: ,
community treasure hunt
find the treasures in matlab central and discover how the community can help you!
start hunting!探索实时编辑器
创建集代码、输出和格式化文本于一体的可执行脚本文档。
polyfitntools/
polyfitntools/demo/html/
版本 | 已发布 | 发行说明 | |
---|---|---|---|
1.3 | documentation added for p and dof, also an example was added in the help. |
|
|
1.2.0.0 | remove nans in data for fit |
|
|
1.1.0.0 | added a p-value on the coefficients. |
|
|
1.0.0.0 |
|