Does anyone knows how to apply boundary condition to the parameters during the fitting using the function nlinfit?
I know that this could be done using the function fit, but i need to use the function nlinfit. (in that regards see the post in the link attached)
Thank you very much in advance for the help.
I don't have time right now to try to craft an example, but I think one way to handle this would be to put a term in your fit that is like
Inf*(F > 50)^2 + Inf*(F < 0)^2
which would automatically make the fit horrible if the parameter F were outside the range [0,50].
You might need to use some huge (but finite) number in place of Inf.
I'll try to make an example if I can.
EDIT TO INCLUDE EXAMPLE:
So, here is an example of including a boundary condition on the parameter F(3) of my model. (I zero out the entire function if F(3) is not within the bounds.)
I think you need to well understand what your function looks like to do this, and I am not 100% sure this is the best way. You will notice that this example spits out warning about the Jacobian being ill-conditioned. I expect this is because F(3) does end up being at the border. The fit does do what I would expect, compared to the unconstrained fit.
% Here is an example of using nlinfit(). For simplicity, none of % of the fitted parameters are actually nonlinear!
% Define the data to be fit x=(0:1:10)'; % Explanatory variable y = 5 + 3*x + 7*x.^2; % Response variable (if response were perfect) y = y + 2*randn((size(x)));% Add some noise to response variable
% Define function that will be used to fit data % (F is a vector of fitting parameters) % f = @(F,x) F(1) + F(2).*x + F(3).*x.^2; f = @(F,x) (F(1) + F(2).*x + F(3).*x.^2) * (1 - (F(3) > 4)|(F(3) < 0)); F_fitted = nlinfit(x,y,f,[1 1 1]);
% Display fitted coefficients disp(['F = ',num2str(F_fitted)])
% Plot the data and fit figure plot(x,y,'*',x,f(F_fitted,x),'g'); legend('data','fit')