%% 输入数据
clc, clear all, close all
x=[1.5, 4.5, 7.5,10.5,13.5,16.5,19.5,22.5,25.5];
y=[7.0,4.8,3.6,3.1,2.7,2.5,2.4,2.3,2.2];
plot(x,y,'*','linewidth',2);
set(gca,'linewidth',2);
xlabel('销售额x/万元','fontsize', 12)
ylabel('流通费率y/%', 'fontsize',12)
%% 对数形式
m1 = @(b,x) b(1) + b(2)*log(x);
nonlinfit1 = fitnlm(x,y,m1,[0.01;0.01])
b=nonlinfit1.Coefficients.Estimate;
Y1=b(1,1)+b(2,1)*log(x);
hold on
plot(x,Y1,'--k','linewidth',2)
%% 指数形式拟合
m2 = 'y ~ b1*x^b2';
nonlinfit2 = fitnlm(x,y,m2,[1;1])
b1=nonlinfit2.Coefficients.Estimate(1,1);
b2=nonlinfit2.Coefficients.Estimate(2,1);
Y2=b1*x.^b2;
hold on
plot(x,Y2,'r','linewidth',2)
legend('原始数据','a+b*lnx','a*x^b')
nonlinfit1 =
非线性回归模型:
y ~ b1 + b2*log(x)
估计系数:
Estimate SE tStat pValue
________ _______ _______ __________
b1 7.3979 0.26667 27.742 2.0303e-08
b2 -1.713 0.10724 -15.974 9.1465e-07
观测值数目: 9,误差自由度: 7
均方根误差: 0.276
R 方: 0.973,调整 R 方 0.969
F 统计量(常量模型): 255,p 值 = 9.15e-07
nonlinfit2 =
非线性回归模型:
y ~ b1*x^b2
估计系数:
Estimate SE tStat pValue
________ ________ _______ __________
b1 8.4112 0.19176 43.862 8.3606e-10
b2 -0.41893 0.012382 -33.834 5.1061e-09
观测值数目: 9,误差自由度: 7
均方根误差: 0.143
R 方: 0.993,调整 R 方 0.992
F 统计量(零模型): 3.05e+03,p 值 = 5.1e-11
本程序摘自《高职高专MATLAB数学建模》,北航出版社,卓金武、王鸿钧编著.
开发工具:MATLAB 2022b