Matlab:最小二乘法拟合二维数据集
Matlab: Least square fit to 2d data set
我有以下问题:
模型:中心对称圆,轮廓是高斯分布和洛伦兹分布的组合。要获得模型图,只需将以下代码插入 Matlab:
N = 501; %Matrix size
R = zeros(N,N); %Initializing matrix R
x0 = ceil(N/2); y0 = x0; %Barycenter coordinates
for i=1:N %Calculation of matrix R
for j=1:N
R(i,j) = sqrt((x0-j)^2 + (y0-i)^2);
end
end
%Model z1 and the parameters:
peak = 0.275*N; %Peak location
m = 0.3;
sigma = 0.1*N;
gamma = 15;
A1 = 1000;
A2 = 50;
z1 = (1-m)*A1/(sigma*sqrt(pi))*exp(-(abs(R - peak)).^2/sigma^2) + m*A2/pi * (gamma./((abs(R - peak1)).^2 + gamma^2));
figure('name','Show Model')
surf(z1,'EdgeColor','none','LineStyle','none','FaceLighting','phong');
这就是 "idealistic" 模型。为了模拟真实数据,我将随机噪声添加到 z1:
z2 = z1 + random('Normal',0,1,N,N);
figure('name','Show random noise data')
surf(z2,'EdgeColor','none','LineStyle','none','FaceLighting','phong');
最后是通过重心的相交平面图:
figure('name','Show intersecting plane with model and random noise data')
xaxis = -floor(N/2):1:floor(N/2);
intersectionline1 = z1(ceil(N/2),:);
intersectionline2 = z2(ceil(N/2),:);
plot(xaxis,intersectionline1,xaxis,intersectionline2,'.r');
Z2 可以是我测量的真实数据集。圆圈在我的图像中的任何地方。我实际上能够找到重心并得到一个平方扇区。除此之外,我得到了半径 r 的一个很好的近似值(变量 "peak")。
我现在的问题:
是否可以对我的数据集进行最小二乘拟合以从我的模型中获取参数 m、sigma、gamma、peak、A1 和 A2???不知道Matlab能不能做到...
提前致谢!
您可以使用 fminsearch
找到给出最小二乘法的系数集:
% peak = K(1)^2
% m = K(2)^2
% sigma = K(3)^2
% gamma = K(4)^2
% A1 = K(5)^2
% A2 = K(6)^2
model = @(z0, K) (1-K(2)^2)*K(5)^2/(K(3)^2*sqrt(pi))*exp(-(abs(z0 - K(1)^2)).^2/K(3)^4) + K(2)^2*K(6)^2/pi * (K(4)^2./((abs(z0 - K(1)^2)).^2 + K(4)^4));
errsq = @(K) sum(sum((z2-model(R,K)).^2));
K0 = ones(6,1);
K = fminsearch(errsq,K0);
figure('name','Reconstructed');
surf(model(R,K),'EdgeColor','none','LineStyle','none','FaceLighting','phong');
为确保系数保持正值,模型使用平方参数化。当然,最初的猜测K0
对最后的结果很重要
我有以下问题:
模型:中心对称圆,轮廓是高斯分布和洛伦兹分布的组合。要获得模型图,只需将以下代码插入 Matlab:
N = 501; %Matrix size
R = zeros(N,N); %Initializing matrix R
x0 = ceil(N/2); y0 = x0; %Barycenter coordinates
for i=1:N %Calculation of matrix R
for j=1:N
R(i,j) = sqrt((x0-j)^2 + (y0-i)^2);
end
end
%Model z1 and the parameters:
peak = 0.275*N; %Peak location
m = 0.3;
sigma = 0.1*N;
gamma = 15;
A1 = 1000;
A2 = 50;
z1 = (1-m)*A1/(sigma*sqrt(pi))*exp(-(abs(R - peak)).^2/sigma^2) + m*A2/pi * (gamma./((abs(R - peak1)).^2 + gamma^2));
figure('name','Show Model')
surf(z1,'EdgeColor','none','LineStyle','none','FaceLighting','phong');
这就是 "idealistic" 模型。为了模拟真实数据,我将随机噪声添加到 z1:
z2 = z1 + random('Normal',0,1,N,N);
figure('name','Show random noise data')
surf(z2,'EdgeColor','none','LineStyle','none','FaceLighting','phong');
最后是通过重心的相交平面图:
figure('name','Show intersecting plane with model and random noise data')
xaxis = -floor(N/2):1:floor(N/2);
intersectionline1 = z1(ceil(N/2),:);
intersectionline2 = z2(ceil(N/2),:);
plot(xaxis,intersectionline1,xaxis,intersectionline2,'.r');
Z2 可以是我测量的真实数据集。圆圈在我的图像中的任何地方。我实际上能够找到重心并得到一个平方扇区。除此之外,我得到了半径 r 的一个很好的近似值(变量 "peak")。
我现在的问题: 是否可以对我的数据集进行最小二乘拟合以从我的模型中获取参数 m、sigma、gamma、peak、A1 和 A2???不知道Matlab能不能做到...
提前致谢!
您可以使用 fminsearch
找到给出最小二乘法的系数集:
% peak = K(1)^2
% m = K(2)^2
% sigma = K(3)^2
% gamma = K(4)^2
% A1 = K(5)^2
% A2 = K(6)^2
model = @(z0, K) (1-K(2)^2)*K(5)^2/(K(3)^2*sqrt(pi))*exp(-(abs(z0 - K(1)^2)).^2/K(3)^4) + K(2)^2*K(6)^2/pi * (K(4)^2./((abs(z0 - K(1)^2)).^2 + K(4)^4));
errsq = @(K) sum(sum((z2-model(R,K)).^2));
K0 = ones(6,1);
K = fminsearch(errsq,K0);
figure('name','Reconstructed');
surf(model(R,K),'EdgeColor','none','LineStyle','none','FaceLighting','phong');
为确保系数保持正值,模型使用平方参数化。当然,最初的猜测K0
对最后的结果很重要