拟合算法2(Curve Fitting 工具箱的使用)

拟合算法2(Curve Fitting 工具箱的使用)

一、打开Curve Fitting工具箱
  • 在APP中点开Curve Fitting

  • 代码区输入cftool也可以直接启动

二、Curve Fitting工具箱简介
2.1 常用的拟合曲线类型
  • Custom Equation:可以自己输入拟合曲线方程
  • Linear Fitting:线性拟合
  • Polynomial:多项式拟合
2.2 Curve Fitting工具箱界面

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dWc1HEP1-1657623148424)(https://s1.ax1x.com/2022/07/11/jc6csU.md.png)]

三、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.9xm1)er(t1790)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运行结果 + 拟合结果
  • 拟合曲线

jgA0WF.png

  • 人口预测

jgAwJU.png

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运行结果

jgmOxO.md.png

3.3案例三.选择最优的拟合曲线
3.3.1 题目要求:为人口选择最合适的拟合曲线(三种备选)

jgvgr8.png

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 代码运行结果

jgvcKf.png

jgvWVg.png
jgv2qS.png

# 结果判断:
* 由于三种函数都是线性函数,因此可以用拟合优度R^2来衡量拟合好坏
* R^2比较:二次函数(0.9998) > 线性函数(0.9779) > 一次函数(0.9778)
* 函数复杂程度比较:一次函数拟合 < 二次函数拟合 < 线性函数拟合
* 由于三者的拟合优度R^2相差较小,相差最大的仅为2%((0.9998-0.9778)/0.9998),因此选择函数复杂程度最低的一次函数较为合适
  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
高斯随机数拟合PDF(Probability Density Function)在Matlab中可以通过以下步骤实现: 1. 生成高斯随机数样本:使用Matlab中的randn函数生成符合标准正态分布的随机数,可以指定生成的样本个数。 2. 统计样本数据:对生成的随机数样本进行统计,计算出数据的均值和标准差。 3. 根据统计数据绘制PDF曲线:根据均值和标准差来计算高斯分布的概率密度函数,并使用Matlab中的normpdf函数绘制高斯分布曲线。 4. 绘制随机数样本的直方图:使用Matlab中的hist函数绘制生成的随机数样本的直方图,作为对比。 5. 显示图形:使用Matlab中的plot函数将PDF曲线和直方图绘制在同一张图上,并使用legend函数添加图例。 下面是一个简单的示例代码: ```matlab % 生成高斯随机数样本 sampleSize = 1000; randomNumbers = randn(sampleSize, 1); % 统计样本数据 meanValue = mean(randomNumbers); standardDeviation = std(randomNumbers); % 计算概率密度函数 x = -3:0.1:3; pdf = normpdf(x, meanValue, standardDeviation); % 绘制PDF曲线和直方图 figure; hold on; plot(x, pdf, 'r', 'LineWidth', 2); histogram(randomNumbers, 'Normalization', 'probability'); hold off; % 添加图例 legend('PDF', 'Random Numbers'); % 显示图形 title('Gaussian Random Number Fit PDF'); xlabel('Value'); ylabel('Probability'); ``` 以上代码将生成1000个高斯随机数样本,并通过概率密度函数拟合PDF,同时绘制了随机数样本的直方图,以及添加了图例和坐标轴的标签。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值