MATLAB: Fit a function given by the derivative of a nonlinear function to data

curve fittingnonlinear data-fitting

I have a nonlinear function whose derivative I want to fit to some data. See the code below.
H = -200:0.1:200;
Ms1 = 1;
Hk1 = 50;
L1 = 40;
T1 = 10;
alpha1 = pi/2;
Ms2 = 1;
Hk2 = 0;
L2 = 1;
T2 = 1;
alpha2 = 0;
phimin = -4*pi;
phimax = 4*pi;
alpha1i = alpha1+pi;
alpha2i = alpha2+pi;
for i = 1:length(H)
fun1 = @(phi) 1/2*Ms1*Hk1*sin(phi)^2-(H(i)+L1)*Ms1*cos(alpha1-phi)-T1*Ms1*sin(alpha1-phi);
phi0_1(i) = fminbnd(fun1,phimin,phimax);
M_1(i) = Ms1*cos(phi0_1(i)-alpha1);
fun1i = @(phi) 1/2*Ms1*Hk1*sin(phi)^2-(H(i)-L1)*Ms1*cos(alpha1i-phi)-T1*Ms1*sin(alpha1i-phi);
phi0_1i(i) = fminbnd(fun1i,phimin,phimax);
M_1i(i) = Ms1*cos(phi0_1i(i)-alpha1i);
fun2 = @(phi) 1/2*Ms2*Hk2*sin(phi)^2-(H(i)+L2)*Ms2*cos(alpha2-phi)-T2*Ms2*sin(alpha2-phi);
phi0_2(i) = fminbnd(fun2,phimin,phimax);
M_2(i) = Ms2*cos(phi0_2(i)-alpha2);
fun2i = @(phi) 1/2*Ms2*Hk2*sin(phi)^2-(H(i)-L2)*Ms2*cos(alpha2i-phi)-T2*Ms2*sin(alpha2i-phi);
phi0_2i(i) = fminbnd(fun2i,phimin,phimax);
M_2i(i) = Ms2*cos(phi0_2i(i)-alpha2i);
end
M_tot = M_1+M_2;
M_toti = M_1i+M_2i;
Where M_tot and M_toti gives the two branches of a hystersis curve. This I wish to fit to an experimental hysteresis curve, by optimizing Ms1, Ms2, Hk1, Hk2, L1, L2, T1, T2, Hk1, and Hk2. I can limit the number of free variables by educated guesses.
My main question is if there is a function or a technique of fiting a function given by the numerical derivative of a symbolic function to data. I have seen functions such as lsqcurvefit, but it seems to require a symbolic function as input. Please correct me if I am wrong.

Best Answer

If you are fitting a hysteresis curve, see if the approach in how to find ascending and descending of hysteresis loop? (link) does what you want.
The lsqcurvefit function can fit multiple dependent variables (as well as using multiple independent variables). It does not require symbolic functions. Your anonymous functions will work with it, although you probably have to combine them in an appropriate matrix to fit a matrix of dependent variables.