nonlinear curve fitting with multiple variables and parameters

2 views (last 30 days)
Hi all,
I hope someone out there can help me solve the problem. I have been reading posts and trying it out myself for sometime now but haven't yet been successful.
I have a data set (measurement), i.e., a table containing x and y values. I want to fit this data set with a model. The model contains another data set (calculation or ideal result) of x and y values and three fitting parameters. The ideal data set contains delta functions of differeing amplitude (given by y values) at different x values.
The problem I have is due to the fact that the model itself has an integration in it with respect to the parameter 'x' from zero to infinity. And the nonlinear fit is to be done with respect to the parameter 'r'.
Below is the model:
Y_fun = (quad('a.*IdealAmp.*dirac(x-IdealX)*exp(-(x-r(i))^2/(4*b^2))-1', 0, inf))*exp(-r/esp)+1;
I tried to do the fit using fminsearch, but to no avail. I would be very thankful if some of you could lend me a hand to solve this problem.
Here is the code I wrote with the help of a code I found on the web:
load Ideal.txt
data1 = Ideal;
IdealX = data1(:,1);
IdealAmp = data1(:,2);
load Experiment.txt
data2 = Experiment;
ExperimentX = data2(:,1);
ExperimentAmp = data2(:,2);
x = ExperimentX;
y = ExperimentAmp;
starting = [1 1 1];
options = optimset('Display','iter');
bestcoeffs=fminsearch(@fun,starting,options,x,y);
yfit = quad('bestcoeffs(1).*IdealX.*dirac(x-IdealX)*exp(-(x-r)^2/(4*bestcoeffs(2)^2))-1', 0, inf)*exp(-r/bestcoeffs(3))+1 ;
%the function fun is defined in a separate m file (in the same folder) as follows
///////////////////////////////
function out = fun(coeff,r,Y)
load Ideal.txt %I tried to load the data again becasue i do not %know how to pass the data to the function
data1 = Ideal;
IdealX = data1(:,1);
IdealAmp = data1(:,2);
a = coeff(1);
b = coeff(2);
eps = coeff(3);
for i = 1:1: length(r)
Y_fun = (quad('a.*IdealAmp.*dirac(x-IdealX)*exp(-(x-r(i))^2/(4*b^2))-1', 0, inf))*exp(-r/esp)+1;
end
DIFF = Y_fun - Y;
SQ_DIFF = DIFF.^2;
out = sum(SQ_DIFF);
//////////////////////////////
Thank you in advance for any helps!
Bests,
BCNin

Answers (0)

Community Treasure Hunt

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

Start Hunting!