2017-09-15 回答
最簡單的多項式拟合
p = polyfit(x,y,n) finds the coefficients of a polynomial p(x) of degree n that fits the data y best in a least-squares sense. p is a row vector of length n+1 containing the polynomial coefficients in descending powers, p(1)*x^n + p(2)*x^(n-1) +...+ p(n)*x + p(n+1).
三次樣條插值
pp = spline(x,y) returns the piecewise polynomial form of the cubic spline interpolant for later use with ppval and the spline utility unmkpp. x must be a vector. y can be a scalar, a vector, or an array of any dimension. if y is an array that is not a vector, the size of y must have the form [d1,d2,...,dk,n], where n is the length of x. the interpolation is performed for each d1-by-d2-by-...-dk value in y.
yy = spline(x,y,xx) is the same as yy = ppval(spline(x,y),xx), thus providing, in yy, the values of the interpolant at xx. xx can be a scalar, a vector, or a multidimensional array.
bezier曲線
function [x,y]=bezier(x,y)
%用法:
%bezier(x,y)
% 生成n-1次貝塞爾曲線,其中x和y是n個點的坐标
%h=bezier(x,y)
% 生成n-1次貝塞爾曲線并傳回曲線句柄
%[x,y]=bezier(x,y)
% 傳回n-1次貝塞爾曲線的坐标
%例子:
%bezier([5,6,10,12],[0 5 -5 -2])
n=length(x);
t=linspace(0,1);
xx=0;yy=0;
for k=0:n-1
tmp=nchoosek(n-1,k)*t.^k.*(1-t).^(n-1-k);
xx=xx+tmp*x(k+1);
yy=yy+tmp*y(k+1);
end
if nargout==2
x=xx;y=yy;
end
h=plot(xx,yy);
if nargout==1
x=h;
end
end
matlab提供了很多插值,拟合的函數,可在幫助裡查一下,還有例子程式。
比如說polyfit
可用 doc polyfit 來查找,找到後還有很多相關的函數,一個個找下去,能找到你需要的。