参考代码:
% 生成测试数据
X = rand(100,1);
Y = rand(100,1);
k = 2;
a = 1.5;
b = 2.5;
Z = k*(X.^a).*(Y.^b);
% 数据加入噪声
Z = Z + randn(size(Z))*0.1;
% 数据拟合
f=inline('c(1)*x(:,1).^c(2).*x(:,2).^c(3)','c','x');
c=lsqcurvefit(f,[1 1 1],[X Y],Z)
% 绘图
[x,y] = meshgrid(linspace(min(X),max(X),20),linspace(min(Y),max(Y),20));
z = reshape(f(c, [x(:), y(:)]), size(x));
clf
a = 0.5;
mesh(x,y,z,'FaceAlpha',a,'EdgeAlpha',a);
hold on
plot3(X,Y,Z,'r.')
view(15,5)
xlabel('x'); ylabel('y'); zlabel('z');
拟合结果:
c =
1.9917 1.3921 2.4509
与真实模型的参数[2 1.5 2.5]吻合。
x=[0,2,3,4.5,5,6.5,8,10,12.5,15,18.5,20,22,23.5]';
y=[0,10,15,20,25,30,35,40,45,50,55,57.5,60,62]';
z=10*rand*[0,10,15,20,25,30,35,40,45,50,55,57.5,60,62]';
X=[x.^2.*y.^2,x.*y.^2,x.^2.*y,x.*y,x.^2,x,ones(length(y),1)];
Z=z;
[b,bint,r,rint,stats] = regress(Z,X);
xt=linspace(1,25,50);yt=linspace(1,65,50);
zt=[];
for i=1:length(xt)
for j=1:length(yt)
zt(i,j)=b(1)*(xt(i).^2).*(yt(j).^2)+b(2)*xt(i).*yt(j).^2+b(3)*xt(i).^2.*yt(j)+b(4)*xt(i).*yt(j)+b(5)*xt(i).^2+b(6)*xt(i)+b(7);
end
end
[XX,YY]=meshgrid(xt,yt);
ZZ=zt;
mesh(XX,YY,ZZ)
hold on
plot3(x',y',z','*') 数据乱弄的,拟合出来不太像哈,你只要把相关的数变了就行。