非线性方程的函数拟合

题目

某类疾病发生率y‰和年龄段x(每五年为一段,例如0-5岁为第一段,6-10岁为第二段)之间有形如y=a*e^bx的关系。试根据观测得到的如下 数据表,用最小二乘法确定式中的参数a和b,并计算相应的均方误差与最大偏差。

x 1 2 3 4 5 6 7 8 9
y 0.898 2.38 3.07 1.84 2.02 1.94 2.22 2.77 4.02
10 11 12 13 14 15 16 17 18 19
4.76 5.46 6.53 10.9 16.5 22.5 35.7 50.6 61.6 81.8

代码实现

方法一:将非线性方程变换成线性方程,对y=a*e^bx两边同时求导,得到lny=lna+bx,再代入方程求解。

function [ ] = minmulti( ~ )
%UNTITLED 此处显示有关此函数的摘要
%   此处显示详细说明
x  = 1:19;
y = [0.898, 2.38, 3.07, 1.84, 2.02, 1.94, 2.22, 2.77, 4.02, 4.76, 5.46, 6.53, 10.9, 16.5, 22.5, 35.7, 50.6, 61.6, 81.8];
n = length(y);
z = log(y);
a = zeros(2,2);
b = 0;
c = 0;
e = 0;
f = 0;
d = zeros(2,1);
for  i = 1:n
    c = c + x(i);
    b = b + x(i)^2;
    e = e + z(i);
    f = f + z(i)*x(i);
end
a(1,1) = n;
a(1,2) = c;
a(2,1) = c;
a(2,2) = b;
d(1,1) = e;
d(2,1) = f;
g = a\d
r = exp(g(1,1))   %%a
u = g(2,1)       %%即为b
q = r*exp(u*x);
m = q-y;
l = abs(m);
k1 = 0;
for i =1:n
    k1 = k1 + m(i)^2;
end
h = max(l)^2    %%最大偏差
k1 = k1/n      %%均方差
end

方法二:按照最小二乘法的原始定义,用牛顿法解非线性方程组。

function [ ] = minmul( ~ )
%UNTITLED 此处显示有关此函数的摘要
%   此处显示详细说明
x  = 1:19;
y = [0.898, 2.38, 3.07, 1.84, 2.02, 1.94, 2.22, 2.77, 4.02, 4.76, 5.46, 6.53, 10.9, 16.5, 22.5, 35.7, 50.6, 61.6, 81.8];
n = length(y);
f = 0;
f1 = 0;
f2 = 0;
g = 0;
g1 = 0;
g2 = 0;
a = 0.68;   %%迭代初值
b = 0.23;    %%迭代初值
k1 = 0;
z = zeros(1,10);
p = z;
k = z;
h =z;
for j = 1:10
    f = 0;
f1 = 0;
f2 = 0;
g = 0;
g1 = 0;
g2 = 0;
for i =1:n
    f = f+2*(a*exp(i*b) - y(i))*exp(b*i);
    f1 = f1+2*exp(2*i*b);
    f2 = f2+4*i*a*exp(2*i*b)-2*i*y(i)*exp(b*i);
    g = g+2*(a*exp(i*b)-y(i))*i*a*exp(i*b);
    g1 = g1+4*i*a*exp(2*i*b)-2*i*y(i)*exp(b*i);
    g2 = g2+4*(a*i)^2*exp(2*i*b)-2*(i)^2*a*y(i)*exp(i*b);
end
c = f1*a+f2*b-f;
d = g1*a+g2*b-g;
b = (d-c*(g1/f1))/(g2-f2*(g1/f1));
a = (c-f2*b)/f1;
q = a*exp(b*x);
m = q-y;
for i =1:n
    k1 = k1 + m(i)^2;
end
p(j) = a;
z(j) = b;
l = abs(m);
h(j) = max(l);
k(j) = k1/n;
end
p        %%a的多次迭代效果
z        %%b的多次迭代效果
k       %%均方差
h        %%最大偏差
end

计算结果

方法一:a=0.6814,b=0.2306,最大偏差745.5493,均方差77.3085

方法二:初值对牛顿法的影响较大,先用方法一的结果作为初值,结果不理想。再用靠近结果的初值迭代,效果较好

a=0.2369,b=0.3090,最大偏差5.3397,均方差3.9428