【发布时间】:2012-12-03 08:21:47
【问题描述】:
我正在使用 Matlab 的曲线拟合工具 cftool 来拟合我拥有的一组点。我面临的问题是生成代码功能不会像cftool 中产生的那样适合我。
这不是我想要的,因为我希望能够从残差图中检索数据。我也可以从cftool 复制函数并手动执行。但我不明白为什么生成的代码不会给我同样的曲线拟合。
cftool 会话文件:http://dl.dropbox.com/u/20782274/test.sfit
Matlab 生成的代码:
function [fitresult, gof] = createFit1(Velocity, kWhPerkm)
%CREATEFIT1(VELOCITY,KWHPERKM)
% Create a fit.
%
% Data for 'untitled fit 3' fit:
% X Input : Velocity
% Y Output: kWhPerkm
% 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 02-Dec-2012 16:36:19
%% Fit: 'untitled fit 3'.
[xData, yData] = prepareCurveData( Velocity, kWhPerkm );
% Set up fittype and options.
ft = fittype( 'a/(0.008*x) + c*x^2 + d*90', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( ft );
opts.DiffMaxChange = 0.01;
opts.Display = 'Off';
opts.Lower = [-Inf -Inf -Inf];
opts.MaxFunEvals = 1000;
opts.MaxIter = 1000;
opts.StartPoint = [0 0 0];
opts.Upper = [Inf Inf Inf];
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Create a figure for the plots.
figure( 'Name', 'untitled fit 3' );
% Plot fit with data.
subplot( 2, 1, 1 );
plot( fitresult, xData, yData, 'predobs' );
% Label axes
xlabel( 'Velocity' );
ylabel( 'kWhPerkm' );
grid on
% Plot residuals.
subplot( 2, 1, 2 );
plot( fitresult, xData, yData, 'residuals' );
% Label axes
xlabel( 'Velocity' );
ylabel( 'kWhPerkm' );
grid on
我用生成的代码得到的曲线: http://i.stack.imgur.com/65d1P.jpg
我需要的曲线: http://i.stack.imgur.com/p3Egp.jpg
那么有谁知道出了什么问题?
-编辑- 还有 Velocity 和 WhPerkm 数据文件:http://dl.dropbox.com/u/20782274/data.mat
【问题讨论】:
-
当我在 CFTOOL 中使用来自dl.dropbox.com/u/20782274/data.mat 的数据时,我得到了看起来很合适的数据:i.stack.imgur.com/DnUND.png。当我使用会话中的数据 (dl.dropbox.com/u/20782274/test.sfit) 和生成的代码时,我得到了另一个合适的:i.stack.imgur.com/O9USJ.png。你能检查你使用生成的代码的数据吗?
标签: matlab curve-fitting