八度编码 - 我需要帮助编码多项式的系数

问题描述 投票:-1回答:2

这个问题很容易手动完成,但是,我很难用代码编写。

有一个四次多项式:

P(x)=ax^4+bx^3+cx^2+dx+e

还有一个给定的矩阵M:

5 0 -1 2 9
-2 -1 0 1 2

其中第一行给出P(x),第二行给出x的值。

使用矩阵M中的信息,找到系数:

a,b,c,d,e

我会知道如何手动操作,对每列进行分页并与其他列同时求解以获得每个系数的值或将其放入矩阵中。

我知道该怎么做,但我不知道如何编码。

我相信最后一行是linearsolve(M(,1),M(,2)),因此能够获得每个系数,但我不知道如何到达那一行。

matlab octave linear-algebra polynomial-math
2个回答
0
投票

欢迎J Cheong

% Values of y and x (note: in your post you had 2 values at x = 1, I assumed that was an accident)
M = [5 0 -1 2 9 ; -2 -1 0 1 2];

% Separate for clarity
y = M(1,:);
x = M(2,:);

% Fit to highest order polynomial model
f = fit(x',y',['poly', num2str(length(y)-1)])

% Extract coefficients
coeff = coeffvalues(f);

% Plotting
X = linspace(min(x)-1, max(x) + 1, 1000) ;
plot(x,y,'.',X,f(X))

编辑

对不起,我正在使用Matlab。查看Octave文档。您应该能够使用系数

p = polyfit(x,y,length(y)-1)';

然后按照您指定的方式显示系数,试试这个

strcat(cellstr(char(96+(1:length(p))')), { ' = ' } , cellstr(num2str(p)))

0
投票
y=[5 0 -1 2 9];
x=[-2 -1 0 1 2];
P=polyfit(x,y,2)

P =
   2.0000   1.0000  -1.0000

这些是c,d,e的系数,其他是零。你可以查看结果:

polyval(P, x)

ans =
   5.0000e+00   2.2204e-16  -1.0000e+00   2.0000e+00   9.0000e+00

这给你y

顺便说一句,你可以在没有计算器的情况下快速解决这个问题,因为x = 0和x = + / - 1的函数值非常容易计算。

© www.soinside.com 2019 - 2024. All rights reserved.