一、擬合概述
擬合(Fitting)是指用某種函數描述數據的過程。在Matlab中,可以通過自定義函數擬合對數據進行處理。自定義函數擬合是將函數調整為最接近數據集的形式的過程。
Matlab中自定義函數可以使用polyfit函數進行擬合。設置x、y兩個輸入變量和一個n階多項式,函數將返回p個係數,使得下式成立:
y = p(1)*x^n + p(2)*x^(n-1) + … + p(n)*x^0
擬合函數在數據中找到一個最合適的曲線去描述數據。這個曲線可以是通過多項式、指數、對數等等不同類型的函數實現的。
二、自定義函數擬合
1、自定義函數語法
在Matlab中,定義自定義函數時使用function關鍵字。函數文件名應與函數名稱相同。例如,定義名為addition的函數:
function z = addition(x, y)
z = x + y;
end
其中,x和y是輸入變量,z是輸出變量。這個函數可以通過調用addition(x, y)來執行。
2、自定義多項式函數擬合
可以定義一個函數f(x, c),其中c是多項式係數,來實現多項式函數的擬合。
例如,定義一個自定義函數polyFitting,來進行n次多項式函數的擬合,如下所示:
function [p, f] = polyFitting(x, y, n)
f = @(c) sum(c .* x .^ (n:-1:0), 2) - y;
p = lsqnonlin(f, zeros(n+1,1));
end
其中,x和y是輸入的數據集合,n是多項式的次數。x的大小應該是[N x 1],而y的大小應該是[N x 1]。最後,使用p(1)到p(n+1)表示多項式的係數。
調用方法如下:
x = 0:0.1:10;
y = 2*x.^2 + 4*x + 1 + randn(1,length(x));
[p,f] = polyFitting(x',y',2);
plot(x,y,'b.')
hold on
xx = 0:0.1:10;
yy = polyval(p,xx,[],1);
plot(xx,yy,'r')
hold off
在上面的示例中,我們使用polyval函數來計算擬合函數的值,並與原始數據作圖進行比較。
三、應用示例
1、擬合正弦函數
我們可以通過擬合正弦函數,來進行自定義函數擬合的應用示例。
function [param, yFit] = fitSinusPattern(x, y)
% Fit sinusoidal pattern to y(x)
% Input: x,y input data
% Output: param is a struct containing the parameters:
% param.amp amplitude of the sinusoid
% param.freq frequency of the sinusoid
% param.phase phase shift of the sinusoid
% yFit is the values of the fitted function corresponding to the input x
% define the function to be minimized
fitFunc = @(p) p(1)*sin(2*pi*p(2)*x+p(3))-y;
% set initial parameter guesses
p0 = [range(y)/2, 1/mean(diff(x))/2, pi/4];
% fit the function (using 'fminsearch')
options = optimset('Display','off', 'TolFun',1e-12, 'TolX',1e-12);
param = fminsearch(fitFunc, p0, options);
% evaluate the fitted function at x values
yFit = param(1)*sin(2*pi*param(2)*x+param(3));
end
我們輸入x,y,自定義的函數fitSinusPattern將計算出相應的正弦擬合參數和擬合處理後的函數yFit。下面我們來進行測試:
x = linspace(0,2*pi,100);
y = 2*sin(3*x+pi/2) + 1 + randn(size(x));
[param, yFit] = fitSinusPattern(x,y);
plot(x,y,'b.')
hold on
plot(x,yFit,'r')
hold off
結果如下:
2、擬合二次曲線
在這個例子中,我們將擬合一個二次曲線。
function [param, yFit] = fitQuadratic(x, y)
% Fit a quadratic polynomial function to y(x)
% Input: x,y input data
% Output: param is a struct containing the parameters:
% param.a coefficient of x^2
% param.b coefficient of x
% param.c constant
% yFit is the values of the fitted function corresponding to the input x
% define the function to be minimized
fitFunc = @(p) polyval(p,x) - y;
% set initial parameter guesses
p0 = polyfit(x,y,2);
% fit the function (using 'lsqnonlin')
options = optimoptions('lsqnonlin', 'Algorithm','levenberg-marquardt', ...
'MaxIter',1e3, 'MaxFunEvals',1e3, ...
'FunctionTolerance',1e-12, 'StepTolerance',1e-12);
p = lsqnonlin(fitFunc, p0, [], [], options);
param.a = p(1);
param.b = p(2);
param.c = p(3);
% evaluate the fitted function at x values
yFit = param.a*x.^2 + param.b*x + param.c;
end
我們輸入x,y,自定義的函數fitQuadratic將計算出相應的二次擬合參數和擬合處理後的函數yFit。下面我們來進行測試:
x = linspace(0,10,100);
y = 2*x.^2 + 4*x + 1 + randn(size(x));
[param, yFit] = fitQuadratic(x,y);
plot(x,y,'b.')
hold on
plot(x,yFit,'r')
hold off
結果如下:
3、擬合高斯函數
在這個例子中,我們將擬合一個高斯函數。
function [param, yFit] = fitGaussian(x, y)
% Fit a Gaussian function to y(x)
% Input: x,y input data
% Output: param is a struct containing the parameters:
% param.a amplitude
% param.b mean
% param.c sigma
% yFit is the values of the fitted function corresponding to the input x
% define the function to be minimized
fitFunc = @(p) p(1)*exp(-((x-p(2))./p(3)).^2/2) - y;
% set initial parameter guesses
p0 = [range(y), mean(x), std(x)];
% fit the function (using 'lsqnonlin')
options = optimoptions('lsqnonlin', 'Algorithm','trust-region-reflective', ...
'MaxIter',1e3, 'MaxFunEvals',1e3, ...
'FunctionTolerance',1e-12, 'StepTolerance',1e-12);
p = lsqnonlin(fitFunc, p0, [], [], options);
param.a = p(1);
param.b = p(2);
param.c = p(3);
% evaluate the fitted function at x values
yFit = param.a*exp(-((x-param.b)./param.c).^2/2);
end
我們輸入x,y,自定義的函數fitGaussian將計算出相應的高斯擬合參數和擬合處理後的函數yFit。下面我們來進行測試:
x = linspace(-5,5,100);
y = 2*exp(-(x-1).^2/(2*0.5^2))+1 + randn(size(x));
[param, yFit] = fitGaussian(x,y);
plot(x,y,'b.')
hold on
plot(x,yFit,'r')
hold off
結果如下:
四、總結
本文提供了Matlab自定義函數擬合方案,涉及到多項式函數、正弦函數、二次曲線和高斯函數的擬合處理。通過定義自定義函數,然後用Matlab自帶的函數polyfit進行調用,得出擬合結果。這些方法都使用了現成的Matlab自帶函數及自定義函數,讀者也可以根據需要自定義擬合算法,以達到需要的擬合效果。
原創文章,作者:APMDC,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/332453.html