MATLAB Curve Fitting Toolbox的输出与生成的函数不匹配 [英] Output of MATLAB Curve Fitting Toolbox does not match generated function

查看:453
本文介绍了MATLAB Curve Fitting Toolbox的输出与生成的函数不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去曾回答过类似的问题,但我的部分问题未得到回答(

A similar question has been answered in the past, but my part of their question was not answered (Matlab curve fitting tool, cftool, generate code function does not give the same fit).

我有一组数据点,旨在显示我正在研究的机制的理想"曲线.

I have a set of data points that are meant to show the "ideal" curve for the mechanism I am studying.

当我在Matlab中的曲线拟合工具箱"中寻找2项指数时,我得到了很好的拟合(R平方:0.9998,调整后的R平方:0.9997).但是,当我为此拟合生成代码时,它会更改系数a,b,c和d.

When I ask the Curve Fitting Toolbox in Matlab to find a 2-term exponential, I get a great fit (R-square: 0.9998, Adjusted R-square: 0.9997). However, when I generate code for this fit, it changes the coefficients a, b, c and d.

在工具箱中,它显示:

常规型号Exp2:

General model Exp2:

 f(x) = a*exp(b*x) + c*exp(d*x)

系数(置信区间为95%):

Coefficients (with 95% confidence bounds):

   a =   4.698e+04  (-1.477e+13, 1.477e+13)

   b =      0.4381  (-1200, 1201)

   c =  -4.698e+04  (-1.477e+13, 1.477e+13)

   d =      0.4381  (-1200, 1201)

合身度:

上交所:0.002979

SSE: 0.002979

R平方:0.9998

R-square: 0.9998

调整后的R平方:0.9997

Adjusted R-square: 0.9997

RMSE:0.006823

RMSE: 0.006823

曲线拟合工具箱生成的函数:

Function generated by the curve-fitting toolbox:

function [fitresult, gof] = createFit1(bgSt, testSt)
%CREATEFIT1(BGST,TESTST)
%  Create a fit.
%
%  Data for 'standard mechanism' fit:
%      X Input : bgSt
%      Y Output: testSt
%  Output:
%      fitresult : a fit object representing the fit.
%      gof : structure with goodness-of fit info.
%
%  See also FIT, CFIT, SFIT.

%  Auto-generated by MATLAB on 29-Apr-2015 15:54:07


%% Fit: 'standard mechanism'.
[xData, yData] = prepareCurveData( bgSt, testSt );

% Set up fittype and options.
ft = fittype( 'exp2' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [0.935605768794225 0.667093185616236 0 0.667093185616236];

% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );

% Plot fit with data.
figure( 'Name', 'standard mechanism' );
h = plot( fitresult, xData, yData );
legend( h, 'testSt vs. bgSt', 'standard mechanism', 'Location', 'NorthEast' );
% Label axes
xlabel bgSt
ylabel testSt
grid on

请注意,系数和生成的曲线完全不同.

Notice that the coefficients are completely different, as are the curves generated.

还请注意,对于曲线拟合工具箱中显示的系数,c = -a和d = b,因此对于任意x值(可笑),y都应等于零.

Notice also that for coefficients displayed in the curve-fitting toolbox, c = -a and d = b, so that y should equal zero for any value of x, which is ludicrous.

但是当我编辑生成的函数以用工具箱中的系数替换函数的系数时,会得到一条很好的曲线.

But when I edit the generated function to replace the function's coefficients with the coefficients from the toolbox, I get a good curve.

修改后的代码:

function [fitresult, gof] = standardFit(bgSt, testSt)
%STANDARDFIT(BGST,TESTST)
%  Create a fit.
%
%  Data for 'standard mechanism' fit:
%      X Input : bgSt
%      Y Output: testSt
%  Output:
%      fitresult : a fit object representing the fit.
%      gof : structure with goodness-of fit info.
%
%  See also FIT, CFIT, SFIT.

%  Auto-generated by MATLAB on 29-Apr-2015 15:54:07

%FROM CURVE FITTING TOOLBOX:
%General model Exp2:
%     f(x) = a*exp(b*x) + c*exp(d*x)
%Coefficients (with 95% confidence bounds):
%       a =   4.698e+04  (-1.477e+13, 1.477e+13)
%       b =      0.4381  (-1200, 1201)
%       c =  -4.698e+04  (-1.477e+13, 1.477e+13)
%       d =      0.4381  (-1200, 1201)

%Goodness of fit:
%  SSE: 0.002979
%  R-square: 0.9998
%  Adjusted R-square: 0.9997
%  RMSE: 0.006823


%% Fit: 'standard mechanism'.
[xData, yData] = prepareCurveData( bgSt, testSt );

% Set up fittype and options.
ft = fittype( 'exp2' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [4.698e+04 0.4381 -4.698e+04 0.4381];

% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );

% Plot fit with data.
figure( 'Name', 'standard mechanism' );
h = plot( fitresult, xData, yData );
legend( h, 'testSt vs. bgSt', 'standard mechanism', 'Location', 'NorthEast' );
% Label axes
xlabel bgSt
ylabel testSt
grid on

我没有足够的声誉来发布曲线的图像,但是在工具箱中它看起来很完美,并且功能中的一个看起来很糟糕-用与链接的海报相同的方式翻译.

I don't have enough reputation to post images of the curves, but in the toolbox it looks perfect and the one from the function looks awful - translated in the same way as the linked poster.

这里是变量bgSt:

-2.85 -2.8 -2.75 -2.7 -2.65 -2.6 -2.55 -2.5 -2.45 -2.4 -2.35 -2.3 -2.25 -2.2 -2.15 -2.1 -2.05 -2 -1.95 -1.9 -1.85 -1.8 -1.75 -1.7 -1.65 -1.6 -1.55 -1.5 -1.45 -1.4 -1.35 -1.3 -1.25 -1.2 -1.15 -1.1 -1.05 -1 -0.95 -0.9 -0.85 -0.8 -0.75 -0.7 -0.65 -0.6 -0.55
-0.5 -0.45 -0.4 -0.35 -0.3 -0.25 -0.2 -0.15 -0.1 -0.05 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5

这是变量testSt:

Here's variable testSt:

0 0.01 0.01 0.02 0.02 0.02 0.03 0.04 0.04 0.05 0.06 0.06 0.07 0.08 0.08 0.09 0.1 0.11 0.12 0.13 0.14 0.15 0.16 0.17 0.18 0.2 0.21 0.23 0.24 0.26 0.28 0.3 0.31 0.33 0.35 0.37 0.39 0.41 0.43 0.45 0.48 0.5 0.52 0.55 0.57 0.6 0.63 0.66 0.68 0.72 0.74 0.78
0.81 0.85 0.88 0.92 0.96 1 1.04 1.08 1.12 1.17 1.21 1.26 1.3 1.35 1.39 1.44

我现在有足够的声誉来添加图像.

曲线拟合工具箱生成的图形:

Figure generated by curve fitting toolbox:

自动生成的函数生成的图形:

Figure generated by automatically-generated function:

推荐答案

由于系数是如何定界的,我在使用指数拟合时遇到了类似的问题.它们可能在对话中受到限制,但是我看不到它们在生成的代码中受到限制的地方.

I ran into a similar problem using the exponential fit due to how the coefficients were bounded. It's possible that they're bounded in the dialogue, but I don't see where they'd be bounded in the generated code.

这篇关于MATLAB Curve Fitting Toolbox的输出与生成的函数不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆