Using nlinfit to fit more than two list
6 views (last 30 days)
Show older comments
I am trying to fit four lists to an equation, to estimate the value of the constances of the equation.
The way that I've tried to get around the problem of fitting more than two lists, is to create a matrix from three of the lists and then call the lists from the matrix. I also slightly rearranged my equation so that on of the list (alpha) would stand alone on one side.
nl=[theta, omega, t]
a=alpha % To make my life a bit easier
beta0=[1 0.10 1/5 1 1]
beta=nlinfit(a,nl,@fitfunc,beta0)
function a = fitfunc(nl,beta)
a = beta(1)*cosd(beta(2)*nl(:,3))-beta(3)*nl(:,2)-beta(4)*nl(:,1)-beta(5)*(nl(:,1)).^3
end
But when I run it, i get this:
Not enough input arguments.
Error in projektdel21>fitfunc (line 6)
a=beta(1)*cosd(beta(2)*nl(:,3))-beta(3)*nl(:,2)-beta(4)*nl(:,1)-beta(5)*(nl(:,1)).^3
Error in (THe name of the script) (line 4)
beta=nlinfit(a,nl,fitfunc,beta0)
Can anyone tell me what I'm doing wrong?
And What the error message means, because I can't see what I'm doing wrong
0 Comments
Answers (1)
Star Strider
on 28 Apr 2019
Edited: Star Strider
on 28 Apr 2019
If I understand your objective function correctly, you have the arguments reversed. The parameter vector (that I assume is ‘beta’) is always the first argument, and the independent variable is always the second. (This is true for all Toolboxes that do nonlinear parameter estimation.)
Try this:
fitfcn = @(beta,nl) beta(1)*cosd(beta(2)*nl(:,3))-beta(3)*nl(:,2)-beta(4)*nl(:,1)-beta(5)*(nl(:,1)).^3;
Also note that you can code it as an anonymous function in one line, as I did here.
EDIT —
If you use the anonymous function, your nlinifit call changes to:
beta=nlinfit(nl,a,fitfunc,beta0)
because it already exists as a function handle.
I also noticed that you have the independent and dependent variable argument order reversed in your nlinfit call. I corrected that here as well.
2 Comments
Star Strider
on 28 Apr 2019
My pleasure.
If my Answer helped you solve your problem, please Accept it!
See Also
Categories
Find more on Linear Least Squares 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!