how do i fit an arbitrary function to data using lsqnonlin in optimization toolbox_ - matlab answers...

2

Click here to load reader

Upload: bhuniakanishka

Post on 19-Jul-2016

17 views

Category:

Documents


3 download

DESCRIPTION

matlab

TRANSCRIPT

Page 1: How Do I Fit an Arbitrary Function to Data Using LSQNONLIN in Optimization Toolbox_ - MATLAB Answers - MATLAB Central

Search: Answers

Create Account Log In

File Exchange Answers Newsgroup Link Exchange Blogs Trendy Cody Contest MathWorks.com

How do I fit an arbitrary function to data using LSQNONLIN in Optimization Toolbox?Asked by MathWorks Support Team on 12 Jun 2012Accepted Answer by MathWorks Support TeamI would like to fit the following arbitrary function to data I have using LSQNONLIN.

f = A + B*exp(C*x) + D*exp(E*x)

X and Y are data sets where Y is the expected output given X.

0 Comments

Show all comments

Tags

function

lsqnonlin

Products

Optimization Toolbox

1 Answer

Answer by MathWorks Support Team on 12 Jun 2012Accepted answerNote: LSQCURVEFIT can be used to solve nonlinear curve-fitting (data-fitting) problems in least-squares sense. The LSQCURVEFITfunction uses the same algorithm as LSQNONLIN, but simply provides a convenient interface for data-fitting problems.

To do this, create the following function named fit_simp.m which uses the X and Y data, both of which are passed into lsqnonlin as optionalinput arguments. Use the X data to calculate values for f, and subtract the original Y data from this. The results are the differences betweenthe experimental data and the calculated values. The lsqnonlin function will minimize the sum of the squares of these differences.

function diff = fit_simp(x,X,Y)

% This function is called by lsqnonlin.

% x is a vector which contains the coefficients of the

% equation. X and Y are the option data sets that were

% passed to lsqnonlin.

A=x(1);

B=x(2);

C=x(3);

D=x(4);

E=x(5);

diff = A + B.*exp(C.*X) + D.*exp(E.*X) - Y;

The following script is an example of how to use fit_simp.m:

% Define the data sets that you are trying to fit the

% function to.

X=0:.01:.5;

Y=2.0.*exp(5.0.*X)+3.0.*exp(2.5.*X)+1.5.*rand(size(X));

% Initialize the coefficients of the function.

X0=[1 1 1 1 1]';

% Calculate the new coefficients using LSQNONLIN.

x=lsqnonlin(@fit_simp,X0,[],[],[],X,Y);

% Plot the original and experimental data.

Y_new = x(1) + x(2).*exp(x(3).*X)+x(4).*exp(x(5).*X);

How do I fit an arbitrary function to data using LSQNONLIN in Optimiz... http://www.mathworks.com/matlabcentral/answers/97439-how-do-i-fit-a...

1 of 2 7/24/2014 11:19 AM

Page 2: How Do I Fit an Arbitrary Function to Data Using LSQNONLIN in Optimization Toolbox_ - MATLAB Answers - MATLAB Central

Site Help Patents Trademarks Privacy Policy Preventing Piracy Terms of Use

Featured MathWorks.com Topics: New Products Support Documentation Training Webinars Newsletters MATLAB Trials Careers

plot(X,Y,'+r',X,Y_new,'b')

0 Comments

Show all comments

LinkDirect link to this answer:

http://www.mathworks.com/matlabcentral/answers/97439#answer_106788

Contact us

© 1994-2014 The MathWorks, Inc.

How do I fit an arbitrary function to data using LSQNONLIN in Optimiz... http://www.mathworks.com/matlabcentral/answers/97439-how-do-i-fit-a...

2 of 2 7/24/2014 11:19 AM