拟合算法2(Curve Fitting 工具箱的使用)
文章目录
一、打开Curve Fitting工具箱
-
在APP中点开Curve Fitting
-
代码区输入
cftool
也可以直接启动
二、Curve Fitting工具箱简介
2.1 常用的拟合曲线类型
- Custom Equation:可以自己输入拟合曲线方程
- Linear Fitting:线性拟合
- Polynomial:多项式拟合
2.2 Curve Fitting工具箱界面
三、Curve Fitting工具使用实例
3.1 案例1.预测美国人口
3.1.1 数据+拟合函数+题目要求
要 求 使 用 函 数 : x m 1 + ( x m 3.9 − 1 ) e − r ( t − 1790 ) 要求使用函数:\frac{x_m}{1+(\frac{x_m}{3.9}-1)e^{-r(t-1790)}} 要求使用函数:1+(3.9xm−1)e−r(t−1790)xm
- 要求能估算30年后的美国人口数
3.1.2 代码
clear,clc;
load American_population.mat %加载数据
cftool; %打开曲线拟合的工具,并输入要求的拟合函数
[fitresult, gof] = USA_predict_code(year, population) %拟合曲线的函数
Xm = 342.4;r = 0.02735;
new_year = 2000 : 10 : 2030;
%% 矩阵运算一定要注意点乘和点除
predict_population = Xm./( 1 + ( Xm./3.9 - 1 ) .* exp(-r .* (new_year - 1790)));
plot(new_year,predict_population,"c-")
[fitresult, gof] = USA_predict_code(year, population)
-
工具生成的函数
-
函数实现代码↓
function [fitresult, gof] = USA_predict_code(year, population)
%% USA_predict_code为生成的文件名,如果修改文件名,函数的名字也需要同步进行更改
%% Fit: 'American_Population'.
[xData, yData] = prepareCurveData( year, population );
% Set up fittype and options.
ft = fittype( 'Xm/( 1 + ( Xm/3.9 - 1 ) * exp(-r * (x - 1790)))', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [0.546881519204984 0.957506835434298];
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.作出拟合曲线图
figure( 'Name', 'American_Population' );
h = plot( fitresult, xData, yData );
legend( h, 'population vs. year', 'American_Population', 'Location', 'SouthEast', 'Interpreter', 'none' );
% Label axes
xlabel( 'year', 'Interpreter', 'none' );
ylabel( 'population', 'Interpreter', 'none' );
grid on
hold on
3.1.3运行结果 + 拟合结果
- 拟合曲线
- 人口预测
3.2案例2.曲线拟合+随机数生成
3.2.1题目要求
# y = exp(0.5xi) -5 + ei (i = 1,2,3……)
# xi ~ U[0,10];ei ~ N[0,10]
# 用自定义曲线对y进行拟合
3.2.2基础知识
1. randi():产生均匀分布的随机整数->rand+int
# randi(10,2,5):生成1~10的随机矩阵,大小2*5
# randi([-5,5],2,5):生成[-5,5]的随机矩阵,大小为2*5
2. rand():产生均匀分布的随机数
# rand(3,3):生成0~1的随机矩阵,大小为3*3
# 生成(a,b)的随机矩阵:a + (b - a)*rand(3,3)
3. normrnd:产生正态分布的随机数
# normrnd(μ,σ,3,5):生成均值为μ,标准差为σ的随机矩阵,大小为3*5
4. roundn:任意位四舍五入
# e = 2.718
# roundn(e , -1) % 2.700
# roundn(e , 0) % 3
# roundn(e , 1) % 0
3.2.3代码
- 主程序代码
clear,clc;
%% 生成30个均匀分布的x值,服从U[0,10]
x = 10 * rand(1 , 30);
%% 噪声e服从正态分布N(0,10)
e = normrnd(0 , sqrt(10) , 1 , 30);
%% 建构需要拟合的y
y = exp(0.5*x) - 5 + e;
%% y的图像
plot(x,y,"o")
xlabel("x的值")
ylabel("y的值")
legend("样本点","location","southeast")
grid on
hold on
[fitresult, gof] = Curve_Exercise_Fitting_Code(x, y);
- 生成拟合代码
function [fitresult, gof] = Curve_Exercise_Fitting_Code(x, y)
%% Fit: 'Curve'.
[xData, yData] = prepareCurveData( x, y );
% Set up fittype and options.
ft = fittype( 'a*exp(b*x)+c', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [0.798228090398017 0.542736137989038 0.232630828301327];
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', 'Curve' );
h = plot( fitresult , "m-" , xData, yData ,"o" );
title("Curve Fitting Exercise")
legend( h, 'y vs. x', 'Curve', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
xlabel( 'x', 'Interpreter', 'none' );
ylabel( 'y', 'Interpreter', 'none' );
grid on
3.2.4运行结果
3.3案例三.选择最优的拟合曲线
3.3.1 题目要求:为人口选择最合适的拟合曲线(三种备选)
3.3.2 代码
clear,clc;
load Chinese_Population
plot(Year,Population,"o")
xlabel("Year")
ylabel("Population")
legend("样本点")
grid on
cftool;
%% 线性拟合->Linear Fitting 拟合公式:a*(sin(x-pi))+b*((x-10)^2)+c*(1)
%参数的值:
%Coefficients (with 95% confidence bounds):
% a = -19.81 (-138, 98.43)
% b = 0.175 (0.1676, 0.1825)
% c = -5.664e+05 (-5.963e+05, -5.365e+05)
[fitresult, gof] = Linear_Fitting(Year, Population)
%线性拟合的参数:
%gof =
% sse: 8.7413e+04
% rsquare: 0.9979
% dfe: 7
% adjrsquare: 0.9972
% rmse: 111.7478
%% 一次多项式拟合->f(x) = p1*x + p2
%Coefficients (with 95% confidence bounds):
% p1 = 702.4 (675.3, 729.6)
% p2 = -1.278e+06 (-1.333e+06, -1.223e+06)
[fitresult1, gof1] = Polynominal1_Fitting(Year, Population)
%Goodness of fit:
% SSE: 9.166e+04
% R-square: 0.9978
% Adjusted R-square: 0.9975
% RMSE: 107
%% 二次多项式拟合->f(x) = p1*x^2 + p2*x + p3
%Coefficients (with 95% confidence bounds):
% p1 = 12.46 (8.639, 16.29)
% p2 = -4.948e+04 (-6.488e+04, -3.409e+04)
% p3 = 4.925e+07 (3.375e+07, 6.475e+07)
[fitresult2, gof2] = polinominal2(Year, Population)
%Goodness of fit:
% SSE: 9662
% R-square: 0.9998
% Adjusted R-square: 0.9997
% RMSE: 37.15
3.3.3 代码运行结果
# 结果判断:
* 由于三种函数都是线性函数,因此可以用拟合优度R^2来衡量拟合好坏
* R^2比较:二次函数(0.9998) > 线性函数(0.9779) > 一次函数(0.9778)
* 函数复杂程度比较:一次函数拟合 < 二次函数拟合 < 线性函数拟合
* 由于三者的拟合优度R^2相差较小,相差最大的仅为2%((0.9998-0.9778)/0.9998),因此选择函数复杂程度最低的一次函数较为合适