How to control the number of fitted parameters when using 'lsqcurvefit'?

4 ビュー (過去 30 日間)
H Huang
H Huang 2016 年 9 月 23 日
移動済み: Matt J 2023 年 1 月 19 日
I plan to use 'lsqcurvefit' to do curve fitting. Its syntax goes as following: lsqcurvefit(fun,x0,xdata,ydata,lb,ub,options) where 'fun' is the fitting model function and 'x0' is the vector of initial values of fitted parameters.
My fitting model is quite complicated. Here is what I want to do. I hope I can control the number of fitted parameters, that is, I can try the fitting with some of parameters fixed while fitting the left parameters. Is there a convenient way to pass this information automatically to the defined fitting model function such that the function calculates corresponding Jacobians correctly? How to do this in 'lsqcurvefit'? Thanks.
HH

採用された回答

John D'Errico
John D'Errico 2016 年 9 月 23 日
編集済み: John D'Errico 2016 年 9 月 23 日
I would strongly recommend that you NOT try setting the lower and upper bounds equal. That will introduce a singularity, making your problem very ill-posed.
But, no, there is no direct way to tell lsqcurvefit not to vary some of the parameters. But who cares? Instead, there is a very simple solution.
Leave your objective function as it is, but add a wrapper function around it, that will take a list of the parameters to be held fixed, and at what values as additional arguments.
So, if fixed set is a boolean vector that is true where a variable is to be held fixed, and fixedvals is the set of values to be held fixed, then it might look like this:
function pred = funwrapper(xvary,data,fixedset,fixedvals)
x = zeros(size(fixedset));
x(fixedset) = fixedvals;
x(~fixedset) = xvary;
pred = fun(x,data);
end
Now, when you call lsqcurvefit, call funwrapper instead.
So, suppose you wish to solve a 4 variable problem, but with variables 2 and 3 fixed.
fixedset = [0 1 1 0];
fixedvals = [42, pi];
xvary0 = x0(~fixedset);
xvary = lsqcurvefit(@(x,data) funwrapper(x,data,fixedset,fixedvals),xvary0,...
You should get the basic idea. lsqcurvefit thinks it is solving a 2 variable problem. It never sees the fixed variables.
  3 件のコメント
Matt J
Matt J 2023 年 1 月 19 日
編集済み: Matt J 2023 年 1 月 19 日
I think it is no longer necessary to avoid setting upper bounds equal to lower bounds in recent Matlab lsqcurvefit is smart enough to reformulate the problem in terms of fewer unknowns automatically.
John D'Errico
John D'Errico 2023 年 1 月 19 日
移動済み: Matt J 2023 年 1 月 19 日
This was not always true for these tools, as I believe I recall it being an issue at some point in the past. However, when I check the current docs for lsqcurvefit, here:
I see the statement that you can indeed fix a parameter at a specific value. by specifying the bounds
x = lsqcurvefit(fun,x0,xdata,ydata,lb,ub) defines a set of lower and upper bounds on the design variables in x, so that the solution is always in the range lb x ub. You can fix the solution component x(i) by specifying lb(i) = ub(i).
And that means my previous answer is no longer correct at least in a current release. Yes, you can do as I suggested there. But at least in R2022b, you can just use the bounds. That is as it should be.

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by