Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8954577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:20:26+00:00 2026-06-15T14:20:26+00:00

I am using Matlab’s curve fitting tool, cftool , to fit a set of

  • 0

I am using Matlab’s curve fitting tool, cftool, to fit a set of points which I have. The problem I am facing is that the generate code function will not give me the same fit as produced in the cftool.

This is not what I want because I want to be able to retrieve the data from the residual plot. I could also just copy the function from cftool and do it manually. But I do not understand why the generated code will not just give me the same curve fit.

The cftool session file: http://dl.dropbox.com/u/20782274/test.sfit

The 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

The curve I get with the generated code:
https://i.stack.imgur.com/65d1P.jpg

The curve I need:
https://i.stack.imgur.com/p3Egp.jpg

So does anyone know what goes wrong?

-edit-
And the Velocity and WhPerkm data file: http://dl.dropbox.com/u/20782274/data.mat

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:20:27+00:00Added an answer on June 15, 2026 at 2:20 pm

    RE: I want to be able to retrieve the data from the residual plot

    One way to do this is:

    1. Select “Save to Workspace…” from the Fit menu
    2. Ensure that “Save fit output to MATLAB struct named” is checked.
    3. Note the name of variable. By default, it is output.
    4. Click “OK” to send data to the MATLAB workspace.

    In the MATLAB workspace, the residuals will be in output.residuals. For your example, you can plot the residuals via, e.g.,

    >> plot( Velocity, output.residuals, '.' )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Matlab 7 and have a problem in creating a monoflop which shall
Using Matlab, how to generate a net of 3^10 points that are evenly located
I'm using MATLAB's deployment tool to compile a simple project which uses a mex
I have some differential equations that I need to solve using MATLAB's ODE solvers.
How can I generate a set of image projections using MATLAB ? I have
I want to set a rule using Matlab FIS EDITOR (Fuzzy Logic Toolbox) which
Using MATLAB, I have this code: value = input('>> Enter a value: '); and
I have been using Matlab 2011b and contourf/contourfm to plot 2D data on a
I have an assignment to create a GUI using MATLAB GUIDE and am having
For my project I need to generate a sine wave using matlab which has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.