函数逼近与曲线(面)拟合的MATLAB程序(3)
发布时间:2021-06-06
发布时间:2021-06-06
函数逼近与曲线(面)拟合的MATLAB程序
运行后屏幕显示拟合函数f及其系数C如下
C = 5.0911 -14.1905 6.4102 -8.2574
f=716503695845759/140737488355328*x^3
-7988544102557579/562949953421312*x^2
+1804307491277693/281474976710656*x
-4648521160813215/562949953421312
故所求的拟合曲线为
f(x) 5.0911x3 14.1905x2 6.4102x 8.2574.
(4)编写下面的MATLAB程序估计其误差,并作出拟合曲线和数据的图形.输入程序
>> xi=[-2.5 -1.7 -1.1 -0.8 0 0.1 1.5 2.7 3.6];
y=[-192.9 -85.50 -36.15 -26.52 -9.10 -8.43 -13.12 6.50 68.04];
n=length(xi);
f=5.0911.*xi.^3-14.1905.*xi.^2+6.4102.*xi -8.2574;
x=-2.5:0.01: 3.6;
F=5.0911.*x.^3-14.1905.*x.^2+6.4102.*x -8.2574;
fy=abs(f-y); fy2=fy.^2; Ew=max(fy),
E1=sum(fy)/n, E2=sqrt((sum(fy2))/n)
plot(xi,y,'r*'), hold on, plot(x,F,'b-'), hold off
legend('数据点(xi,yi)','拟合曲线y=f(x)'),
xlabel('x'), ylabel('y'),
title('例7.2.1的数据点(xi,yi)和拟合曲线y=f(x)的图形')
运行后屏幕显示数据(xi,yi)与拟合函数f的最大误差Ew,平均误差E1和均方根误差E2及其数据点(xi,yi)和拟合曲线y=f(x)的图形(略).
Ew = E1 = E2 =
3.105 4 0.903 4 1.240 9
7.3 函数rk(x)的选取及其MATLAB程序
例7.3.1 给出一组实验数据点(xi,yi)的横坐标向量为
x=(-8.5,-8.7,-7.1,-6.8,-5.10,-4.5,-3.6,-3.4,-2.6,-2.5, -2.1,-1.5, -2.7,-3.6),纵横坐标向量为y=(459.26,52.81,198.27,165.60,59.17,41.66,25.92, 22.37,13.47, 12.87, 11.87,6.69,14.87,24.22),试用线性最小二乘法求拟合曲线,并用(7.2),(7.3)和(7.4)式估计其误差,作出拟合曲线.
解 (1)在MATLAB工作窗口输入程序
>>x=[-8.5,-8.7,-7.1,-6.8,-5.10,-4.5,-3.6,-3.4,-2.6,-2.5,
-2.1,-1.5, -2.7,-3.6];
y=[459.26,52.81,198.27,165.60,59.17,41.66,25.92,
22.37,13.47, 12.87, 11.87,6.69,14.87,24.22];
plot(x,y,'r*'),legend('实验数据(xi,yi)')
xlabel('x'), ylabel('y'),
title('例7.3.1的数据点(xi,yi)的散点图')
运行后屏幕显示数据的散点图(略).
(3)编写下列MATLAB程序计算f(x)在(xi,yi)处的函数值,即输入程序
>> syms a b
x=[-8.5,-8.7,-7.1,-6.8,-5.10,-4.5,-3.6,-3.4,-2.6,-2.5,-2
.1,-1.5,-2.7,-3.6]; fi=a.*exp(-b.*x)
运行后屏幕显示关于a和b的线性方程组
fi =
[ a*exp(17/2*b), a*exp(87/10*b), a*exp(71/10*b),
a*exp(34/5*b), a*exp(51/10*b), a*exp(9/2*b), a*exp(18/5*b), a*exp(17/5*b), a*exp(13/5*b), a*exp(5/2*b), a*exp(21/10*b),
下一篇:入党志愿书填写参考模板