MATLAB多项式拟合选择幂

MATLAB多项式拟合选择幂,matlab,least-squares,polynomials,Matlab,Least Squares,Polynomials,我有两个向量x和y,我想在MATLAB中拟合多项式作为y=f(x)。 我本可以使用polyfit。然而,我只想拟合多项式的选择性幂项。例如,y=f(x)=a*x^3+b*x+c。注意这里没有x^2术语。MATLAB中是否有任何内置函数来实现此功能? 我不确定简单地忽略MATLAB为x^2给出的系数是否与不使用x^2项拟合多项式相同。听起来你有拟合工具箱,只想删除一个可能的系数。如果是这样的话,这里有一个方法 %What is the degree of the polynomial (cubic

我有两个向量x和y,我想在MATLAB中拟合多项式作为
y=f(x)

我本可以使用
polyfit
。然而,我只想拟合多项式的选择性幂项。例如,
y=f(x)=a*x^3+b*x+c
。注意这里没有
x^2
术语。MATLAB中是否有任何内置函数来实现此功能?

我不确定简单地忽略MATLAB为
x^2
给出的系数是否与不使用
x^2
项拟合多项式相同。

听起来你有拟合工具箱,只想删除一个可能的系数。如果是这样的话,这里有一个方法

%What is the degree of the polynomial (cubic)
polyDegree = 3;

%What powers do you want to skip (x^2 and x)
skipPowers = [2, 1];

%This sets up the options
opts = fitoptions( 'Method', 'LinearLeastSquares' );

%All coefficients of degrees not specified between x^n and x^0 can have any value
opts.Lower = -inf(1, polyDegree + 1);
opts.Upper = inf(1, polyDegree + 1);

%The coefficients you want to skip have a range from 0 to 0.
opts.Lower(polyDegree + 1 - skipPowers) = 0;
opts.Upper(polyDegree + 1 - skipPowers) = 0;

%Do the fit using the specified polynomial degree.
[fitresult, gof] = fit( x, y, ['poly', num2str(polyDegree)] , opts );

如果您没有曲线拟合工具箱(请参阅@thewaywewalk的注释),或者无论如何,可以很容易地使用
mldivide

x=rand(10,1);                  % your x data
y=5*x.^3+2*x+7+rand(10,1)*.01; % some y data with noise
[x.^3 x ones(size(x))]\y       % least squares solve to y = a*x^3 + b*x + c
给予


请注意,“简单地忽略MATLAB为x^2给出的系数”绝对不是“拟合没有x^2项的多项式”。

在最近的MATLAB版本中,您可以通过选择
应用程序
选项卡,然后选择
曲线拟合工具
,使用GUI轻松完成这项工作

例如,我定义:

x = 1:10;
y = x + randn(1,10);
然后,我在工具中选择变量为
X data
Y data
,然后选择定义为
a*X^3+b*X+c
自定义方程。结果是:


不,不是。你有曲线拟合工具箱吗?如果是,请看一下函数。谢谢您的回答@user1543042的答案更适合我,因为我试图在MATLAB中模拟Excel LINEST函数的行为。@user1543042的方法很好,因为它提供了拟合优度统计数据和置信区间。不过,这种更简单的
mldivide
方法给出了完全相同的结果。
x = 1:10;
y = x + randn(1,10);