Fitting data to integral2 function
Mostrar comentarios más antiguos
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 !!!
Respuestas (1)
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
Categorías
Más información sobre Common Operations en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!