I don't think you're far off. If you have m fits of n parameters each, with one parameter the same across all m fits, then you have a total of 1 + m*(n-1) parameters. So in the example you posted, x0 should have 1 + 2*(8-1) = 15 elements. You should modify fitfun slightly, too
x2 = x([9 2 10 11 12 13 14 15]);
otherwise the 10th element is left out of the fit entirely.
To keep yourself sane as you extend to 5 fits, it might help to reorder/renumber the parameters. I'd even be tempted to change voigt1 to be a function of two inputs v (scalar) and x (7-element vector). Then fitfun would look something like
function error = fitfun(x)
v = x(1);
x1 = x(2:8);
x2 = x(9:15);
y1 = voigt1(v,x1);
y2 = voigt1(v,x2);
error = sum((ydata1-y1).^2 + (ydata2-y2).^2);
end
This would be more easily adapted to 5 (or whatever) fits.
It looks like there might be an issue with passing xdata and ydata into the functions. You can use anonymous function handles to help with that, by making the function file accept an extra input, then making an anonymous function handle with the extra input fixed:
function error = fitfun(x,ydata1,ydata2)
...
end
x0 = ...
ydata1 = ...
ydata2 = ...
f = @(x) fitfun(x,ydata1,ydata2)
fminsearch(f,x0)
Best Answer