Simultaniously fitting 4 functions: two functions share all parameters, all four functions share one parameter
3 views (last 30 days)
I am trying to fit four data sets with four functions f1,f2,f3,f4. Functions f1 and f2 share all fitting between each other parameters, f3 and f4 share all fitting parameters between each other aswell. But all four functions share one parameter. Meaning:
So far I was able fit two functions simultaniously that share all parameters with the help of this thread https://de.mathworks.com/matlabcentral/answers/419277-simultaneous-fitting-of-two-plots-with-two-functions or using this function https://de.mathworks.com/matlabcentral/fileexchange/40613-multiple-curve-fitting-with-common-parameters-using-nlinfit?s_tid=srchtitle
The letter is slightly better suited for my application, since I have NaN values in some data sets.
Now I don't know how to simultaniously fit all four functions. If I fit them seperatly, I get vastly different values for the one shared parameter x(2).
Any help is much appreciated!
Bjorn Gustavsson on 2 Sep 2022
function errtot = four_errorfcns(pars1to5,Xall,Yall,sigmaYall)
err1 = sum( ( f1(pars1to5(1:3),Xall(1,:))-Yall(1,:) ).^2/sigmaYall(1,:)^2);% Include ignoernans etc
err2 = sum( ( f1(pars1to5(1:3),Xall(2,:))-Yall(2,:) ).^2/sigmaYall(2,:)^2);% as you see fit. This
err3 = sum( ( f1(pars1to5([4 2 5]),Xall(3,:))-Yall(3,:) ).^2/sigmaYall(3,:)^2);% is a simplest
err4 = sum( ( f1(pars1to5([4 2 5]),Xall(4,:))-Yall(4,:) ).^2/sigmaYall(4,:)^2);% example
errtot = err1 + err2 + err3 + err4;
Then you can simply call fminsearch or any of its siblings. If you modify the function to return the normalized residuals you can use lsqnonlin. I also realised that your functions are written with 3 scalar inputs and I wrote the input-arguments as 1-by-3 arrays, but you can adapt that easily. If you dont have the sigmaYall standard deviations of the 4 data-sets you fit to you will have to settle for similar wheigts and remove that input parameter.