Matlab curve fitting tool, cftool, code function generation does not give the same match
I am using Matlab's Curve Snap Tool cftool
to fit the set of points I have. The problem I'm running into is that the generated function of the code won't give me the same thing as in cftool
.
This is not what I want because I want to get data from the residual plot. I could just copy the function from cftool
and do it manually. But I don't understand why the generated code won't just give me the same shape.
Session file cftool
: http://dl.dropbox.com/u/20782274/test.sfit
Generated code from 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
Curve I get with the generated code: http://i.stack.imgur.com/65d1P.jpg
Curve I need: http://i.stack.imgur.com/p3Egp.jpg
So does anyone know what went wrong?
-edit- And Velocity and WhPerkm data file: http://dl.dropbox.com/u/20782274/data.mat
source to share
RE: I want to be able to extract data from the residual graph
One way to do it:
- Select "Save to Workspace ..." from the Fit menu
- Make sure the "Save appropriate output for MATLAB structure with name" checkbox is checked.
- Pay attention to the variable name. The default is
output
. - Click OK to send the data to the MATLAB workspace.
In the MATLAB workspace, the leftovers will be in output.residuals
. For example, you can display the leftovers via, for example,
>> plot( Velocity, output.residuals, '.' )
source to share