MATLAB作业4参考答案.docx
《MATLAB作业4参考答案.docx》由会员分享,可在线阅读,更多相关《MATLAB作业4参考答案.docx(7页珍藏版)》请在冰豆网上搜索。
![MATLAB作业4参考答案.docx](https://file1.bdocx.com/fileroot1/2023-1/14/f97c3e9f-99fd-4904-851e-3c31fafff8c2/f97c3e9f-99fd-4904-851e-3c31fafff8c21.gif)
MATLAB作业4参考答案
MATLAB作业4参考答案
MATLAB作业四参考答案
2yt,,sin(103)1、用在(0,3)区间内生成一组较稀疏的数据,并用一维数据插值的方法对给出的
数据进行曲线拟合,并将结果与理论曲线相比较。
【求解】类似于上面的例子~可以用几乎一致的语句得出样本数据和插值效果。
>>t=0:
0.2:
3;
y=sin(10*t.^2+3);plot(t,y,'o')
ezplot('sin(10*t^2+3)',[0,3]);holdonx1=0:
0.001:
3;y1=interp1(t,y,x1,'spline');plot(x1,y1)
由于曲线本身变换太大~所以在目前选定的样本点下是不可能得出理想插值效果的~因为样本数据提供的信息量不够。
为了得到好的插值效果~必须增大样本数据的信息量~对本例来说~必须在快变化区域减小样本点的步长。
>>holdoff
t=[0:
0.1:
1,1.1:
0.04:
3];y=sin(10*t.^2+3);plot(t,y,'o')
ezplot('sin(10*t^2+3)',[0,3]);holdonx1=0:
0.001:
3;y1=interp1(t,y,x1,'spline');plot(x1,y1)
241,,xy22fxyexyxy,,(,)sin()2、用原型函数生成一组网络数据或随机数据,分别拟合出曲3xy,3
面,并和原曲面进行比较。
【求解】由下面的语句可以直接生成一组网格数据~用下面语句还可以还绘制出给定样本点是三维表面图。
>>[x,y]=meshgrid(0.2:
0.2:
2);
z=exp(-x.^2-y.^4).*sin(x.*y.^2+x.^2.*y)./(3*x.^3+y);surf(x,y,z)
选择新的密集网格~则可以通过二元插值得出插值曲面。
对比插值结果和新网格下的函数值精确解~则可以绘制出绝对插值误差曲面。
由插值结果可见精度是令人满意的。
>>[x1,y1]=meshgrid(0.2:
0.02:
2);
z1=interp2(x,y,z,x1,y1,'spline');
surf(x1,y1,z1)
>>z0=exp(-x1.^2-y1.^4).*sin(x1.*y1.^2+x1.^2.*y1)./(3*x1.^3+y1);
surf(x1,y1,abs(z1-z0))
现在假设已知的样本点不是网格形式分布的~而是随机分布的~则可以用下面语句生成样本点~得出分布的二维、三维示意图。
>>x=0.2+1.8*rand(400,1);y=0.2+1.8*rand(400,1);%仍生成(0.2,2)区间的均匀分布随机数
z=exp(-x.^2-y.^4).*sin(x.*y.^2+x.^2.*y)./(3*x.^3+y);plot(x,y,'x')
figure,plot3(x,y,z,'x')
利用下面的语句可以得出三维插值结果,同时可以绘制出插值的绝对误差曲面~可见插值结果还是很好的~但由于边界样本点信息不能保证~所以不能像网格数据那样对(0.2,2)区域~而只能选择(0.3,1.9)区域进行插值。
>>[x1,y1]=meshgrid(0.3:
0.02:
1.9);
z1=griddata(x,y,z,x1,y1,'v4');
surf(x1,y1,z1)
>>z0=exp(-x1.^2-y1.^4).*sin(x1.*y1.^2+x1.^2.*y1)./(3*x1.^3+y1);
surf(x1,y1,abs(z1-z0))
、假设已知一组数据,试用插值方法绘制出区间内的光滑函数曲线,比较各种插值算法3x,,(2,4.9)
的优劣。
-2-1.7-1.4-1.1-0.8-0.5-0.20.10.40.711.3xi
.10289.11741.13158.14483.15656.16622.17332.1775.17853.17635.17109.16302yi
1.61.92.22.52.83.13.43.744.34.64.9xi
.15255.1402.12655.11219.09768.08353.07015.05786.04687.03729.02914.02236yi
【求解】用下面的语句可以立即得出给定样本点数据的三次插值与样条插值~得出的结果如~可见~用两种插值方法对此例得出的结果几乎一致~效果均很理想。
>>x=[-2,-1.7,-1.4,-1.1,-0.8,-0.5,-0.2,0.1,0.4,0.7,1,1.3,...
1.6,1.9,2.2,2.5,2.8,3.1,3.4,3.7,4,4.3,4.6,4.9];y=[0.10289,0.11741,0.13158,0.14483,0.15656,0.16622,0.17332,...
0.1775,0.17853,0.17635,0.17109,0.16302,0.15255,0.1402,...0.12655,0.11219,0.09768,0.08353,0.07019,0.05786,0.04687,...
0.03729,0.02914,0.02236];
x0=-2:
0.02:
4.9;
y1=interp1(x,y,x0,'cubic');
y2=interp1(x,y,x0,'spline');
plot(x0,y1,':
',x0,y2,x,y,'o')
(,)xy、假设已知实测数据由下表给出,试对在(0.1,0.1)~(1.1,1.1)区域内的点进行插值,并用三维曲面4
的方式绘制出插值结果。
yxxxxxxxxxxxi3567891011124
00.10.20.30.40.50.60.70.80.911.10.1.83041.82727.82406.82098.81824.8161.81481.81463.81579.81853.823040.2.83172.83249.83584.84201.85125.86376.87975.89935.92263.94959.98010.3.83587.84345.85631.87466.89867.9284.963771.00451.05021.11.15290.4.84286.86013.88537.91865.959851.00861.06421.12531.19041.2571.3222
0.5.85268.88251.92286.973461.03361.10191.17641.2541.33081.40171.4605
0.6.86532.91049.968471.03831.1181.20461.29371.37931.45391.50861.5335
0.7.88078.943961.02171.11181.21021.3111.40631.48591.53771.54841.5052
0.8.89904.982761.0821.19221.30611.41381.50211.55551.55731.49151.346
0.9.920061.02661.14821.27681.40051.50341.56611.56781.48891.31561.0454
1.943811.07521.21911.36241.48661.56841.58211.50321.3151.0155.62477
1.1.970231.12791.29291.44481.55641.59641.53411.34731.0321.61268.14763
【求解】直接采用插值方法可以解决该问题~得出的插值曲面。
>>[x,y]=meshgrid(0.1:
0.1:
1.1);
z=[0.83041,0.82727,0.82406,0.82098,0.81824,0.8161,0.81481,0.81463,0.81579,0.81853,0.82304;
0.83172,0.83249,0.83584,0.84201,0.85125,0.86376,0.87975,0.89935,0.92263,0.94959,0.9801;0.83587,0.84345,0.85631,0.87466,0.89867,0.9284,0.96377,1.0045,1.0502,1.1,1.1529;0.84286,0.86013,0.88537,0.91865,0.95985,1.0086,1.0642,1.1253,1.1904,1.257,1.3222;0.85268,0.88251,0.92286,0.97346,1.0336,1.1019,1.1764,1.254,1.3308,1.4017,1.4605;0.86532,0.91049,0.96847,1.0383,1.118,1.2046,1.2937,1.3793,1.4539,1.5086,1.5335;0.88078,0.94396,1.0217,1.1118,1.2102,1.311,1.4063,1.4859,1.5377,1.5484,1.5052;0.89904,0.98276,1.082,1.1922,1.3061,1.4138,1.5021,1.5555,1.5573,1.4915,1.346;0.92006,1.0266,1.1482,1.2768,1.4005,1.5034,1.5661,1.5678,1.4889,1.3156,1.0454;0.94381,1.0752,1.2191,1.3624,1.4866,1.5684,1.5821,1.5032,1.315,1.0155,0.62477;0.97023,1.1279,1.2929,1.4448,1.5564,1.5964,1.5341,1.3473,1.0321,0.61268,0.14763];[x1,y1]=meshgrid(0.1:
0.02:
1.1);
z1=interp2(x,y,z,x1,y1,'spline');
surf(x1,y1,z1)
axis([0.1,1.1,0.1,1.1,min(z1(:
)),max(z1(:
))])
其实~若光需要插值曲面而不追求插值数值的话~完全可以直接采用MATLAB下的shadinginterp命令
来实现。
可见~这样的插值方法更好~得出的插值
曲面更光滑。
>>surf(x,y,z);shadinginterp
5、习题3和4给出的数据分别为一元数据和二元数据,试用分段三次样条函数和B样条函数对其进行
拟合。
【求解】先考虑习题4~相应的三次样条插值和B-样条插值原函数与导数函数分别为:
>>x=[-2,-1.7,-1.4,-1.1,-0.8,-0.5,-0.2,0.1,0.4,0.7,1,1.3,...
1.6,1.9,2.2,2.5,2.8,3.1,3.4,3.7,4,4.3,4.6,4.9];
y=[0.10289,0.11741,0.13158,0.14483,0.15656,0.16622,0.17332,...
0.1775,0.17853,0.17635,0.17109,0.16302,0.15255,0.1402,...
0.12655,0.11219,0.09768,0.08353,0.07019,0.05786,0.04687,...
0.03729,0.02914,0.02236];
S=csapi(x,y);S1=spapi(6,x,y);
fnplt(S);holdon;fnplt(S1)
>>Sd=fnder(S);Sd1=fnder(S1);
fnplt(Sd),holdon;fnplt(Sd1)
再考虑习题5中的数据~原始数据不能直接用于样条处理~因为meshgrid()函数产生的数据
格式与要求的ndgrid()函数不一致~所以需要对数据进行处理~其中需要的x和y均应该
是向量~而z是原来z矩阵的转置~所以用下面的语句可以建立起三次样条和B-样条的插值
模型~函数的表面图所示~可见二者得出的结果很接近。
>>[x,y]=meshgrid(0:
0.1:
1.1);
z=[0.83041,0.82727,0.82406,0.82098,0.81824,0.8161,0.81481,0.81463,0.81579,0.81853,0.82304;
0.83172,0.83249,0.83584,0.84201,0.85125,0.86376,0.87975,0.89935,0.92263,0.94959,0.9801;0.83587,0.84345,0.85631,0.87466,0.89867,0.9284,0.96377,1.0045,1.0502,1.1,1.1529;0.84286,0.86013,0.88537,0.91865,0.95985,1.0086,1.0642,1.1253,1.1904,1.257,1.3222;0.85268,0.88251,0.92286,0.97346,1.0336,1.1019,1.1764,1.254,1.3308,1.4017,1.4605;0.86532,0.91049,0.96847,1.0383,1.118,1.2046,1.2937,1.3793,1.4539,1.5086,1.5335;0.88078,0.94396,1.0217,1.1118,1.2102,1.311,1.4063,1.4859,1.5377,1.5484,1.5052;0.89904,0.98276,1.082,1.1922,1.3061,1.4138,1.5021,1.5555,1.5573,1.4915,1.346;0.92006,1.0266,1.1482,1.2768,1.4005,1.5034,1.5661,1.5678,1.4889,1.3156,1.0454;0.94381,1.0752,1.2191,1.3624,1.4866,1.5684,1.5821,1.5032,1.315,1.0155,0.62477;0.97023,1.1279,1.2929,1.4448,1.5564,1.5964,1.5341,1.3473,1.0321,0.61268,0.14763];>>x0=[0.0:
0.1:
1];y0=x0;z=z';
S=csapi({x0,y0},z);fnplt(S)
figure;S1=spapi({5,5},{x0,y0},z);fnplt(S1)
>>S1x=fnder(S1,[0,1]);fnplt(S1x)
figure;S1y=fnder(S1,[0,1]);fnplt(S1y)
6、重新考虑习题3中给出的数据,试考虑用多项式插值的方法对其数据进行逼近,并选择一个能较好
拟合原数据的多项式阶次。
【求解】可以选择不同的多项式阶次~例如选择3,5,7,9,11~则可以对其进行多项式拟合~并绘制出曲
线。
>>x=[-2,-1.7,-1.4,-1.1,-0.8,-0.5,-0.2,0.1,0.4,0.7,1,1.3,...
1.6,1.9,2.2,2.5,2.8,3.1,3.4,3.7,4,4.3,4.6,4.9];
y=[0.10289,0.11741,0.13158,0.14483,0.15656,0.16622,0.17332,...
0.1775,0.17853,0.17635,0.17109,0.16302,0.15255,0.1402,...
0.12655,0.11219,0.09768,0.08353,0.07019,0.05786,0.04687,...
0.03729,0.02914,0.02236];
x0=-2:
0.02:
4.9;
p3=polyfit(x,y,3);y3=polyval(p3,x0);
p5=polyfit(x,y,5);y5=polyval(p5,x0);
p7=polyfit(x,y,7);y7=polyval(p7,x0);
p9=polyfit(x,y,9);y9=polyval(p9,x0);
p11=polyfit(x,y,11);y11=polyval(p11,x0);
plot(x0,[y3;y5;y7;y9;y11])
从拟合的结果可以发现~选择5次多项式就能较好地拟合原始数据。
221,,()/2x,,yxe,()7、假设习题3中给出的数据满足原型,试用最小二乘法求出的值,并,,,,,2
用得出的函数将函数曲线绘制出来,观察拟合效果。
221,,()/2xaa12yxe(),【求解】令则可以将原型函数写成aa,,,,,,122,a2
这时可以写出原型函数为
>>f=inline('exp(-(x-a
(1)).^2/2/a
(2)^2)/(sqrt(2*pi)*a
(2))','a','x');由原型函数则可以用下面的语句拟合出待定参数aa,,。
这样~拟合曲线得出的拟合效果是满意的。
12
>>x=[-2,-1.7,-1.4,-1.1,-0.8,-0.5,-0.2,0.1,0.4,0.7,1,1.3,...
1.6,1.9,2.2,2.5,2.8,3.1,3.4,3.7,4,4.3,4.6,4.9];
y=[0.10289,0.11741,0.13158,0.14483,0.15656,0.16622,0.17332,...
0.1775,0.17853,0.17635,0.17109,0.16302,0.15255,0.1402,...
0.12655,0.11219,0.09768,0.08353,0.07019,0.05786,0.04687,...
0.03729,0.02914,0.02236];
a=lsqcurvefit(f,[1,1],x,y)
a=
0.346057535548862.23400202798747
>>x0=-2:
0.02:
5;y0=f(a,x0);
plot(x0,y0,x,y,'o')
222zxyaxybyxcxdxye(,)sin()cos(),,,,,8、假设习题4中数据的原型函数为,试用最小二乘
方法识别出abcde,,,,的数值。
【求解】用下面的语句可以用最小二乘的得出
>>[x,y]=meshgrid(0.1:
0.1:
1.1);
z=[0.83041,0.82727,0.82406,0.82098,0.81824,0.8161,0.81481,0.81463,0.81579,0.81853,0.82304;
0.83172,0.83249,0.83584,0.84201,0.85125,0.86376,0.87975,0.89935,0.92263,0.94959,0.9801;0.83587,0.84345,0.85631,0.87466,0.89867,0.9284,0.96377,1.0045,1.0502,1.1,1.1529;0.84286,0.86013,0.88537,0.91865,0.95985,1.0086,1.0642,1.1253,1.1904,1.257,1.3222;0.85268,0.88251,0.92286,0.97346,1.0336,1.1019,1.1764,1.254,1.3308,1.4017,1.4605;0.86532,0.91049,0.96847,1.0383,1.118,1.2046,1.2937,1.3793,1.4539,1.5086,1.5335;0.88078,0.94396,1.0217,1.1118,1.2102,1.311,1.4063,1.4859,1.5377,1.5484,1.5052;0.89904,0.98276,1.082,1.1922,1.3061,1.4138,1.5021,1.5555,1.5573,1.4915,1.346;0.92006,1.0266,1.1482,1.2768,1.4005,1.5034,1.5661,1.5678,1.4889,1.3156,1.0454;0.94381,1.0752,1.2191,1.3624,1.4866,1.5684,1.5821,1.5032,1.315,1.0155,0.62477;0.97023,1.1279,1.2929,1.4448,1.5564,1.5964,1.5341,1.3473,1.0321,0.61268,0.14763];x1=x(:
);y1=y(:
);z1=z(:
);
A=[sin(x1.^2.*y1)cos(y1.^2.*x1)x1.^2x1.*y1ones(size(x1))];
theta=A\z1
theta=
-0.89204693251635
3.09378647090361
-0.12203277931186
2.70828089435211
-2.42507028220675
用下面的语句可以绘制出拟合结果~如图所示。
>>[x,y]=meshgrid(0.1:
0.02:
1.1);z=theta
(1)*sin(x.^2.*y)+theta
(2)*cos(y.^2.*x)+theta(3)*x.^2+...
theta(4)*x.*y+theta(5);
surf(x,y,z)
9、假设已知一组实测数据在文件c8pdat.dat中给出,试通过插值的方法绘制出三维曲面。
【求解】由该文件可见~给定的数据是非网格型的x;y;z向量~故提取这些向量并按非网格数据进行插值~则将得出如图所示的插值结果。
>>loadc8pdat.dat
x=c8pdat(:
1);y=c8pdat(:
2);z=c8pdat(:
3);[max(x),min(x)max(y),min(y)]%找出插值区域
ans=
0.99430.01290.99940.0056
>>[x1,y1]=meshgrid(0:
0.02:
1);
z1=griddata(x,y,z,x1,y1,'v4');
surf(x1,y1,z1)