非线性最小二乘拟合:
解法一:用命令lsqcurvefit
function f = curvefun(x, tdata)
f = x() + x()*exp(0.02 * x() * tdata);
%其中x() = a; x() = b; x() = c;
%数据输入
tdata = ::;
cdata = 1e- * [4.54, 4.99, 5.35, 5.65, 5.90, 6.10, 6.26, 6.39, 6.50, 6.59];
%设定预测值
x0 = [0.2 0.05 0.05];
%非线性拟合函数
x = lsqcurvefit('curvefun', x0, tdata, cdata)
%作图
f = curvefun(x, tdata)
plot(tdata, cdata, 'k+')
hold on
plot(tdata, f, 'r')
结果:
x =
-0.0074 0.0116 0.0118
f =
Columns 1 through 8
0.0044 0.0047 0.0050 0.0053 0.0056 0.0059 0.0062 0.0066
Columns 9 through 10
0.0069 0.0072
解法二:用命令lsqnonlin
function f = curvefun1(x)
%curvefun1的自变量是x,cdata和tdata是已知参数,故应将cdata,tdata的值卸载curvefun1中
tdata = ::;
cdata = 1e- * [4.54, 4.99, 5.35, 5.65, 5.90, 6.10, 6.26, 6.39, 6.50, 6.59];
f = x() + x()*exp(0.02 * x() * tdata) - cdata;%注意
tdata = ::;
cdata = 1e- * [4.54, 4.99, 5.35, 5.65, 5.90, 6.10, 6.26, 6.39, 6.50, 6.59];
%预测值
x0 = [0.2 0.05 0.05];
x = lsqnonlin('curvefun1', x0)
f = curvefun1(x)
plot(tdata, cdata, 'k+')
hold on
plot(tdata, f+cdata, 'r')
结果:
x =
-0.0074 0.0116 0.0118
f =
1.0e-003 *
Columns 1 through 8
-0.1168 -0.2835 -0.3534 -0.3564 -0.3022 -0.1908 -0.0320 0.1645
Columns 9 through 10
0.3888 0.6411