# Problems while trying to fit a 4-variable function with lsqcurvefit

1 view (last 30 days)
Fabio Costa on 17 May 2022
Edited: Matt J on 17 May 2022
Dear Matlab colleagues,
I'm currently trying to write a piece of code able to model some experimental data according to voigt functions, such as implemented in the Origin software. As this function is defined as an integral, the usual "fit" function does not seem to be the right tool for the job. Based on some similar cases posted before, I'm trying to perform this task with the lsqcurvefit tool.
Before trying to annalyze the actual exp. data, I'm using the following code to generate a peak based on some input parameters, and afterwards run the lsqcurvefit in this artificial data, trying to extract the initial parameters. This code was adapted from the simple sxponential fit example.
xdata=linspace(-100,100,200)
voigt_func =@(x,xdata) x(1).*(2.*log(2)./pi.^(3./2)).*(x(2)./x(3).^2)...
.*integral(@(t) (exp(-t.^2))./((sqrt(log(2)).*(x(2)./x(3))).^2...
+(sqrt(4.*log(2)).*((xdata-x(4))./x(3))-t).^2),-Inf,Inf);
input_coeffs=[20,10,8,32];
for i=1:200
ydata(i)=voigt_func(input_coeffs,xdata(i));
end
coeff = lsqcurvefit(voigt_func,input_coeffs,xdata,ydata)
Even though I tried to make several adaptations based on the example and some previous posts, the attempts always end with several errors. The current error message is as follows:
It is not very intuitive, as the arrays generated by the code apperently have the same dimmensions.
Hopefully someone with more experienced eyes could spot the problem with this approach and give me some advice on what to do. Thanks in advance for any support.
Fábio

Matt J on 17 May 2022
Edited: Matt J on 17 May 2022
voigt_func =@(x,xdata) x(1).*(2.*log(2)./pi.^(3./2)).*(x(2)./x(3).^2)...
.*integral(@(t) (exp(-t.^2))./((sqrt(log(2)).*(x(2)./x(3))).^2+...
(sqrt(4.*log(2)).*((xdata-x(4))./x(3))-t).^2),-Inf,Inf,'Arr',1);
xdata=linspace(-100,100,200);
input_coeffs=[20,10,8,32];
ydata=voigt_func(input_coeffs,xdata);
coeff = lsqcurvefit(voigt_func,input_coeffs+5,xdata,ydata)
Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance.
coeff = 1×4
20.0000 10.0000 8.0000 32.0000
Fabio Costa on 17 May 2022
Many thnaks for your quick repply!