Fitting data to integral2 function

Hello everybody,
I need to fit my datas which are (xdata,ydata) by the following equation :
B is the independant variable (corresponding to xdata), M is the dependant variable (corresponding to ydata),
Msat, NmB/kT and Ea/kT are the fitting parameters (called x(1), x(2) and x(3) in the following)
I managed to fit manualy my data with the function by choosing manually x(1), x(2) and x(3), using the following code :
x=[0.23,5.5,10.5];
fun=@(xdata)arrayfun(@(xdata)x(1).*integral2(@(th,ph)sin(th).*cos(th).*exp(x(2).*xdata.*cos(th)+x(3).*(sin(th0).*sin(th).*cos(ph)+cos(th0).*cos(th)).^2),0,pi,0,2*pi)./integral2(@(th,ph)sin(th).*exp(x(2).*xdata.*cos(th)+x(3).*(sin(th0).*sin(th).*cos(ph)+cos(th0).*cos(th)).^2),0,pi,0,2*pi),xdata)
ydatafit=fun(xdata);
plot(xdata,ydatafit,'-r')
this gives :
My question is how can I fit automatically ?
I tried this code :
fun=@(x,xdata)arrayfun(@(xdata)x(1).*integral2(@(th,ph)sin(th).*cos(th).*exp(x(2).*xdata.*cos(th)+x(3).*(sin(th0).*sin(th).*cos(ph)+cos(th0).*cos(th)).^2),0,pi,0,2*pi)./integral2(@(th,ph)sin(th).*exp(x(2).*xdata.*cos(th)+x(3).*(sin(th0).*sin(th).*cos(ph)+cos(th0).*cos(th)).^2),0,pi,0,2*pi),xdata)
x0 = [0.23,5.5,10.5];
x = lsqcurvefit(fun,x0,xdata,ydata)
plot(xdata,ydata,'ko',xdata,fun(x,xdata),'b-')
And I get these errors :
warning: quad2d: Maximum number of sub-tiles reached without convergence
warning: called from
quad2d at line 329 column 7
integral2 at line 230 column 12
@<anonymous> at line 9 column 168
@<anonymous> at line 9 column 15
nonlin_curvefit>@<anonymous> at line 84 column 14
__nonlin_residmin__ at line 585 column 9
nonlin_curvefit at line 83 column 18
lsqcurvefit at line 231 column 19
error: dimensions of observations and values of model function must match
error: called from
__nonlin_residmin__ at line 590 column 7
nonlin_curvefit at line 83 column 18
lsqcurvefit at line 231 column 19
>> plot(xdata,ydata,'ko',xdata,fun(x,xdata),'b-')
error: 'x' undefined near line 1 column 33
Thank you in advance for your answer !!!

2 comentarios

Torsten
Torsten el 28 de Nov. de 2018
Write a function in which you calculate M. Then it's easier for you to debug what your problem is.
rom3413
rom3413 el 28 de Nov. de 2018
That's what I have done, the M function is called fun in my code.
I can plot it as a function of xdata by fixing manually paremeters x(1), x(2) and x(3).
But I didn't manage to obtain x(1), x(2) and x(3) by fitting fun to my datas

Iniciar sesión para comentar.

Respuestas (1)

Torsten
Torsten el 28 de Nov. de 2018
Editada: Torsten el 28 de Nov. de 2018

0 votos

x0 = [0.23,5.5,10.5];
th0 = ...;
x = lsqcurvefit(@(x,xdata)fun(x,xdata,th0),x0,xdata,ydata)
function res = fun(x,xdata,th0)
for i = 1:numel(xdata)
xdata_actual = xdata(i);
numerator = x(1)*integral2(@(th,ph)sin(th).*cos(th).*exp(x(2).*xdata_actual.*cos(th)+x(3).*(sin(th0).*sin(th).*cos(ph)+cos(th0).*cos(th)).^2),0,pi,0,2*pi);
denominator = integral2(@(th,ph)sin(th).*exp(x(2).*xdata_actual.*cos(th)+x(3).*(sin(th0).*sin(th).*cos(ph)+cos(th0).*cos(th)).^2),0,pi,0,2*pi);
res(i) = numerator/denominator;
end
end

2 comentarios

rom3413
rom3413 el 28 de Nov. de 2018
It returns the same error...
warning: quad2d: Maximum number of sub-tiles reached without convergence
warning: called from
quad2d at line 329 column 7
integral2 at line 230 column 12
fun at line 4 column 15
@<anonymous> at line 1 column 27
nonlin_curvefit>@<anonymous> at line 84 column 14
__nonlin_residmin__ at line 585 column 9
nonlin_curvefit at line 83 column 18
lsqcurvefit at line 231 column 19
error: dimensions of observations and values of model function must match
error: called from
__nonlin_residmin__ at line 590 column 7
nonlin_curvefit at line 83 column 18
lsqcurvefit at line 231 column 19
Maybe you can reproduce the error, thus here attached is the ydata file (MH60.txt)
I add an offset of 0.012 to have roughly ydata (-inf)=-ydata(+inf)
ydata=MH60(:,1)+0.012;
xdata=linspace(-6.4,6.4,129);
rom3413
rom3413 el 28 de Nov. de 2018
you also need th0
th0=70*pi/180

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 28 de Nov. de 2018

Comentada:

el 28 de Nov. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by