如何在Scilab中进行多项式逼近?

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

我有一套措施,我想近似一些。我知道我可以使用4次多项式来做到这一点,但我不知道如何使用Scilab找到它的五个系数。

现在,我必须使用Open office calc的用户友好功能...所以,为了继续使用Scilab,我想知道是否存在内置函数,或者我们是否可以使用简单的脚本。

polynomial-math scilab approximation
2个回答
5
投票

在Matlab中没有内置的polyfit函数,但你可以创建自己的函数:

function cf = polyfit(x,y,n)
A = ones(length(x),n+1)
for i=1:n
    A(:,i+1) = x(:).^i
end
cf = lsq(A,y(:))
endfunction

此函数接受两个大小相等的向量(它们可以是行向量或列向量;冒号运算符确保它们在计算中是面向列的)和多项式的次数。

它返回系数列,从0到第n度排序。

计算方法很简单:设置(通常是超定的)线性系统,要求多项式通过每个点。然后在lsq的最小二乘意义上解决它(在实践中,似乎cf = A\y(:)执行相同,虽然算法有点不同)。

用法示例:

x = [-3 -1 0 1 3 5 7]
y = [50 74 62 40 19 35 52]
cf = polyfit(x,y,4)

t = linspace(min(x),max(x))'   // now use these coefficients to plot the polynomial
A = ones(length(t),n+1)
for i=1:n
    A(:,i+1) = t.^i
end
plot(x,y,'r*')
plot(t,A*cf)

输出:


2
投票

Atom的工具箱“stixbox”包含Matlab兼容的“polyfit”和“polyval”功能。

// Scilab 6.x.x need:
atomsInstall(["stixbox";"makematrix";"distfun";"helptbx";linalg"]) // install toolboxes

// POLYNOMINAL CURVE_FITTING
// Need toolboxes above

x = [-3 -1 0 1 3 5 7];
y = [50 74 62 40 19 35 52];
plot(x,y,".");           // plot sample points only

pcoeff = polyfit(x,y,4); // calculate polynominal coefficients (4th-degree)
xp = linspace(-3,7,100); // generate a little more x-values for a smoother curve fitting
yp = polyval(pcoeff,xp); // calculate the y-values for the curve fitting
plot(xp, yp,"k");        // plot the curve fitting in black

enter image description here

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