How can I do non-linear fitting on a model defined implicitly?
4 views (last 30 days)
Show older comments
MathWorks Support Team
on 11 Apr 2014
Edited: MathWorks Support Team
on 2 Feb 2021
I would like to use the "nlinfit" function to do nonlinear regression on a model that is defined implicitly:
y = model(x,y,params)
The documentation for "nlinfit" explains how to fit a model defined explicitly:
y = model(x,params)
Is there a way to specify my model so that I can use "nlinfit" to estimate its parameters? Do you have other functions that I could use with my implicit model?
Accepted Answer
MathWorks Support Team
on 11 Apr 2014
1. Using "nlinfit"
The "nlinfit" function expects a response vector "Y" and a function of unknown parameters. Simply encapsulate the implicit model in a function of the form:
0 = y - model(x,y,beta)
The response vector to be passed to "nlinfit" becomes
Y = zeros(size(y));
The new predictor array now contains both the old predictor variables and the old responses packed together in one array:
X = [x,y];
and the new model is specified by the anonymous function
modelfun = @(beta,X) ( X(:,2) - model(X(:,1),X(:,2),beta) );
where "model" is the implicit model with inputs "x" and "y", output "y", and parameterized by the (unknown) parameter vector "beta".
Finally, use "nlinfit" with an initial guess "beta0" as follows:
beta = nlinfit(X,Y,modelfun,beta0)
2. Using "lsqnonlin"
Alternatively, you could achieve the same results with the "lsqnonlin" function of the Optimization Toolbox.
The "lsqnonlin" function tries to solve a least-squares equation of the form
min_x { f_1(x)^2 + f_2(x)^2 + ... + f_n(x)^2 }
for an unknown vector "x".
In the case of fitting an implicit model, the vector "x" would be the vector of unknown parameters "beta" of the implicit model and f_i(x) would be:
f_i(beta) = y_i - model(x_i,y_i,beta)
where "X=[x_i]" are the predictors and "Y=[y_i]" are the responses used to fit the model.
However, "lsqnonlin" takes as input argument a handle to a function of only 1 argument (the parameters). You would have to pass extra parameters (in this case, the predictors and responses) by using an anonymous function. Assume your implicit model is defined in a function file called "model". Then, "modelfun" is a handle to an anonymous function defined as:
modelfun = @(beta) ( Y - model(X,Y,beta) );
where "Y" is the vector of responses, "X" is the vector of predictors, and "beta" is the vector of unknown parameters.
With an initial guess "beta0", call "lsqnonlin" as follows:
beta = lsqnonlin(modelfun,beta0)
1 Comment
Abhishek Pandey
on 13 May 2016
Edited: MathWorks Support Team
on 2 Feb 2021
Hi Srikanth! If you are still facing issues with this, feel free to contact MathWorks Technical Support. They should be able to help you with this.
- Abhishek
More Answers (0)
See Also
Categories
Find more on Linear and Nonlinear Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!