Effacer les filtres
Effacer les filtres

Curve fitting with fails misarebly

5 vues (au cours des 30 derniers jours)
Yotam
Yotam le 8 Jan 2012
I'm trying to fit a curve in matlab using fit in command line. The input data is:
X =
1
2
4
5
8
9
10
13
Y =
1.0e-04 *
0.1994
0.0733
0.0255
0.0169
0.0077
0.0051
0.0042
0.0027
And the target function is
Y = 1/(kappa*X.^a)
I am using fittype, fitoptions, and fit as follow:
model1 = fittype('1/(kappa*x.^pow)');
opt1 = fitoptions(model1);
opt1.StartPoint = [1e-5 -2];
[fit1,gof1] = fit(X,Y.^-1,model1,opt1)
I get results with rsquare of roughly -450 which are vaguely in the same direction as the measurement.
A plot of Y as a function of X in a log log scale, yield an almost linear plot. The resulting fit however, has a smaller slope (closer to 0) and is higher than any of the data points (almost twice as high as highest data point). It is needless to say that the line doesn't cross any of the data point.
How can I generate better quality of data fitting?
Edit:
When I try to fit the results to
kappa*x.^pow
I get a good result with pow being roughly -1.5.
But if I try to fit to:
1/(kappa*x.^pow)
I get a poor result. Why is that so?

Réponses (2)

Richard Willey
Richard Willey le 9 Jan 2012
Couple comments:
Curve Fitting Toolbox includes a lot of nice code that will automatically choose "good" starting points for nonlinear regressions for supported model types. You're much better off using the library of standard models rather than hard coding your own model.
One of the nice things about cftool is that you can use the tool to generate a fit and then (automatically) create a function that with replicate this analysis without any need to open the GUI.
I attached some code that I auto generated from cftool.
The function had no trouble generating a good fit. I used a "power" model which looks to be the same as your custom equation.
function [fitresult, gof] = createFit1(X, Y)
%CREATEFIT1(X,Y)
% Create a fit.
%
% Data for 'untitled fit 1' fit:
% X Input : X
% Y Output: Y
% 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 09-Jan-2012 09:08:03
%%Fit: 'untitled fit 1'.
[xData, yData] = prepareCurveData( X, Y );
% Set up fittype and options.
ft = fittype( 'power1' );
opts = fitoptions( ft );
opts.Display = 'Off';
opts.Lower = [-Inf -Inf];
opts.StartPoint = [1.92785231569241e-005 -1.5782710696971];
opts.Upper = [Inf Inf];
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult, xData, yData );
legend( h, 'Y vs. X', 'untitled fit 1', 'Location', 'NorthEast' );
% Label axes
xlabel( 'X' );
ylabel( 'Y' );
grid on

Andrew Newell
Andrew Newell le 8 Jan 2012
I got a nice looking fit using cftool with the type of fit chosen to be "Power".
EDIT: I think your mistake is using Y.^-1 in the last line. Try this code:
model1 = fittype(@(kappa,pow,x) kappa*x.^pow);
opt1 = fitoptions(model1);
opt1.StartPoint = [2e-5 -1.5];
[fit1,gof1] = fit(X,Y,model1,opt1)
  2 commentaires
Yotam
Yotam le 8 Jan 2012
When I try this, I get an error:
Error using ==> fittype.fittype>fittype.fittype at 212
First input argument must be a string or cell array.
associated with the line
model1 = fittype(@(kappa,pow,x) kappa*x.^pow);
I think that the result I have posted (R^2 ~ -450) where derived while using Y and not Y.^-1. I can't use cftool since this code will be run on a computation server. I don't know exactly what it the matlab version I have but it is licensed to February, 2010.
Andrew Newell
Andrew Newell le 8 Jan 2012
Try substituting this line:
model1 = fittype('kappa*x.^pow','independent','x','coefficients',{'kappa','pow'});

Connectez-vous pour commenter.

Catégories

En savoir plus sur Get Started with Curve Fitting Toolbox dans Help Center et File Exchange

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by