%可用样条曲面拟合,最好对原数据整理一下,拟合的代码如下:
x0=2.2:0.1:7;y0=10:5:30;
z0 =[ 0.0121 0.0118 0.0129 0.1098 0.0103
0.0116 0.0116 0.0124 0.1007 0.0111
0.0110 0.0113 0.0120 0.0914 0.0119
0.0105 0.0111 0.0116 0.0820 0.0128
0.0099 0.0109 0.0112 0.0726 0.0136
0.0094 0.0107 0.0108 0.0635 0.0144
0.0090 0.0105 0.0105 0.0547 0.0151
0.0085 0.0104 0.0101 0.0465 0.0158
0.0081 0.0102 0.0098 0.0391 0.0164
0.0078 0.0101 0.0096 0.0325 0.0170
0.0075 0.0100 0.0094 0.0270 0.0174
0.0073 0.0099 0.0092 0.0228 0.0177
0.0072 0.0099 0.0091 0.0200 0.0179
0.0071 0.0098 0.0091 0.0187 0.0180
0.0071 0.0098 0.0091 0.0183 0.0180
0.0071 0.0098 0.0091 0.0179 0.0180
0.0071 0.0098 0.0091 0.0176 0.0180
0.0072 0.0099 0.0091 0.0172 0.0180
0.0072 0.0099 0.0091 0.0169 0.0180
0.0072 0.0099 0.0091 0.0165 0.0180
0.0072 0.0099 0.0091 0.0162 0.0180
0.0072 0.0099 0.0091 0.0159 0.0180
0.0073 0.0099 0.0091 0.0156 0.0179
0.0073 0.0100 0.0092 0.0154 0.0179
0.0074 0.0100 0.0092 0.0151 0.0178
0.0075 0.0101 0.0093 0.0149 0.0178
0.0076 0.0101 0.0093 0.0147 0.0177
0.0077 0.0102 0.0094 0.0144 0.0177
0.0078 0.0102 0.0095 0.0142 0.0176
0.0079 0.0103 0.0095 0.0140 0.0175
0.0081 0.0104 0.0096 0.0139 0.0174
0.0082 0.0105 0.0097 0.0137 0.0173
0.0084 0.0106 0.0099 0.0135 0.0171
0.0086 0.0107 0.0100 0.0134 0.0170
0.0089 0.0108 0.0101 0.0133 0.0168
0.0091 0.0109 0.0103 0.0131 0.0166
0.0094 0.0111 0.0105 0.0130 0.0164
0.0097 0.0112 0.0107 0.0129 0.0162
0.0100 0.0114 0.0109 0.0128 0.0160
0.0104 0.0115 0.0111 0.0128 0.0157
0.0108 0.0117 0.0114 0.0127 0.0155
0.0112 0.0119 0.0116 0.0126 0.0152
0.0116 0.0121 0.0119 0.0126 0.0148
0.0121 0.0123 0.0122 0.0125 0.0145
0.0126 0.0126 0.0126 0.0125 0.0141
0.0131 0.0128 0.0129 0.0125 0.0138
0.0137 0.0131 0.0133 0.0125 0.0133
0.0143 0.0133 0.0137 0.0124 0.0129
0.0150 0.0136 0.0141 0.0124 0.0124];
sp=csapi({x0 y0},z0);%可得到每片三次样条曲面的系数
fnplt(sp); %可得到拟合曲面的效果图
%也可以用最小二乘曲面拟合,代码为:
[m,n]=size(z0);k=9;
C=ones(m*n,1/2*(k+1)*(k+2));
x=reshape(ones(n,1)*x0,m*n,1);
y=reshape(y0'*ones(1,m),m*n,1);
z=reshape(z0,m*n,1);
for j=0:k
for i=0:j
C(:,1/2*(j+1)*(j+2)-j+i)=x.^(j-i).*y.^i;
end
end
coefficient=C\z;
%做出曲面效果与图1稍有不同,因为采集点较少,所以拟合次数对结果影响很大
syms x y
z=ones(1,1/2*(k+1)*(k+2));sum=0;
for j=0:k
for i=0:j
sum=sum+sym(x^(j-i)*y^i)*coefficient(1/2*(j+1)*(j+2)-j+i);
end
end
figure;ezmesh(sum,[min(x0),max(x0),min(y0),max(y0)])
标签:matlab,拟合,曲面