使用nlinfit进行非线性拟合。程序如下:
x= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
y = [0, -44, -427, -501, -549, -580, -596, -686, -709, -727];
f = @(beta, x)beta(1)./(x+beta(2))+beta(3);
beta = nlinfit(x, y, f, [0; 0; 0]);
a = beta(1), b = beta(2), c = beta(3)
xx = 1:.1:10;
yy = a./(xx+b)+c;
plot(x, y, 'o', xx, yy)
输出:
%%%%%%%%%%%%%%% 程序 %%%%%%%%%%%%%%%%%%%%%%
clear
clc
syms t
x=[239:1:341]';
y=[-2799,-2799,-2799,-2799,-2799,-2799,-2823,-2823,-2823,-2823,...
-2823,-2823,-2823,-2823,-2852,-2852,-2852,-2852,-2852,-2864,-2864,...
-2864,-2864,-2864,-2864,-2873,-2873,-2873,-2873,-2873,-2873,-2873,...
-2893,-2893,-2893,-2893,-2893,-2893,-2899,-2899,-2899,-2899,-2899,...
-2899,-2899,-2907,-2907,-2907,-2907,-2907,-2907,-2907,-2923,-2923,...
-2923,-2923,-2923,-2923,-2927,-2927,-2927,-2927,-2927,-2927,-2927,...
-2927,-2942,-2942,-2942,-2942,-2942,-2942,-2942,-2942,-2942,-2953,...
-2953,-2953,-2953,-2956,-2956,-2956,-2956,-2956,-2956,-2956,-2973,...
-2973,-2973,-2973,-2973,-2973,-2973,-2973,-2973,-2973,-2973,-2973,...
-2973,-2973,-2973,-2973,-2973]';
% plot(x,y);
f = fittype('a/(t+b)+c','independent','t','coefficients',{'a','b','c'});
opt = fitoptions(f);
set(opt,'startpoint',[1 1 1]);
cfun = fit(x,y,f,opt)
yy = cfun(x);
plot(x,y,'*',x,yy,'r');
legend('原始数据点','拟合后曲线');
%%%%%%%%%%%%% 结果 %%%%%%%%%%%%%%%%%%%
cfun =
General model:
cfun(t) = a/(t+b)+c
Coefficients (with 95% confidence bounds):
a = 2.096e+04 (1.662e+04, 2.529e+04)
b = -171 (-181.5, -160.6)
c = -3095 (-3116, -3075)
a,b,c为拟合的系数,95%为置信区间
%%%%%%%%%%%%%% 图像对比 %%%%%%%%%%%%%%%%%%%