Global fit over several variables with one function respectively for shared parameters

4 views (last 30 days)
Hello,
I have data for 5 different variables L1,L2,U,I,N (ydata) according to some xdata. Each of them can be calculated by a function and all these functions share up to 6 parameters. Now I want to do a global fit for all my data to calculate the parameters. I tried a lot like this:
function x = fUn0o2( M )
% output: [mUI;mIU;mIN;mNI;kIU(0);kNI(0)]
% input: [xdata,ydata] (nx6-Mat)
xdata = M(:,1);
ydata = M(:,2:6);
xdata = xdata';
ydata = ydata';
x0 = [-2;0;-2;0;0;0.5];
options = optimset('MaxFunEvals',1e10,'MaxIter',1e3);
[x,resnorm] = lsqcurvefit(@fUnktIoN0o2,x0,xdata,ydata,[],[],options);
function L = fUnktIoN0o2( x,xdata )
% UIN; input: 6x1-Mat, output: lambda1,lambda2,U,I,N
kUI = 19.1.*exp(x(1).*xdata);
kIU = x(5).*exp(x(2).*xdata);
kIN = 203.6.*exp(x(3).*xdata);
kNI = x(6).*exp(x(4).*xdata);
S = kUI + kIU + kIN + kNI;
L1 = 0.5.*(S+((S.^2)-4.*(kIU.*kNI + kUI.*kNI + kUI.*kIN)).^0.5);
L2 = 0.5.*(S-((S.^2)-4.*(kIU.*kNI + kUI.*kNI + kUI.*kIN)).^0.5);
I = 1./(((kIN)./(kNI))+((kIU)./(kUI))+1);
U = ((kIU)./(kUI)).*I;
N = ((kIN)./(kNI)).*I;
L = [L1;L2;U;I;N];
end
I hope it is understandable, what I tried to do here, though it is a long text and not exactly clearly arranged. My main problem is, that by adding more formulas and data to my program to make my fit more accurate, I accomplish exactly the opposite and my fit gets worse and worse, so I hope someone can help me finding my mistake(s).
Thank you in advance,
Alex

Accepted Answer

Matt J
Matt J on 5 Nov 2013
Edited: Matt J on 5 Nov 2013
Nobody can troubleshoot your code without some way to run and reproduce the problems you're seeing.
Just as a guess, though, the ydata quantities L1 and L2 (the roots of some quadratic?) are not differentiable functions of the x(i) parameters, unless you impose a constraint that the discriminant
(S.^2)-4.*(kIU.*kNI + kUI.*kNI + kUI.*kIN)>= lb > 0
where lb is some strictly positive lower bound. For constraints like that, you would need fmincon.
Further, even if you used fmincon to do this, it is not clear to me that such a constrained region is contiguous over the space of x. You might need a very good initial guess to converge to the right region.
  1 Comment
Alexander
Alexander on 5 Nov 2013
Thanks for your answer.
I had just been worried I might have made obvious mistakes in my program, because I am not very experienced in working with MATLAB, but if it is just about good initial guesses and lower and upper bounds, I think I will be able to fix it.
Alex

Sign in to comment.

More Answers (1)

Alexander
Alexander on 5 Nov 2013
I have another question about my program.
Is there a way to let MATLAB use an algorithm, that is different from Least Squares? Again I am not even sure, if this is a reasonable question, because this is the first time I am doing fitting work at all, but if there is a way to make good initial guesses less important, that would be great!
Alex
  2 Comments
Matt J
Matt J on 5 Nov 2013
fmincon() can be applied to any (twice differentiable) cost function that you wish, so if you have a different fitting function in mind besides least squares, you could always use that.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!