# Simultaniously fitting 4 functions: two functions share all parameters, all four functions share one parameter

1 view (last 30 days)
Roland on 1 Sep 2022
Commented: Roland on 12 Oct 2022
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:
f1(x(1),x(2),x(3))
f2(x(1),x(2),x(3))
f3(x(4),x(2),x(5))
f4(x(4),x(2),x(5))
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;
end
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.
HTH
Roland on 12 Oct 2022
Thanks, this helped!

### Categories

Find more on Fit Postprocessing in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!