- Do you receive warning and/or error messages? If so the full and exact text of those messages (all the text displayed in orange and/or red in the Command Window) may be useful in determining what's going on and how to avoid the warning and/or error.
- Does it do something different than what you expected? If so, what did it do and what did you expect it to do?
- Did MATLAB crash? If so please send the crash log file (with a description of what you were running or doing in MATLAB when the crash occured) to Technical Support using the telephone icon in the upper-right corner of this page so we can investigate.

# Fit with an integral involved

4 views (last 30 days)

Show older comments

Hi everybody,

i have some problem in fitting my experimental data with a function involving an integral in it.

My fitting function is the subsequent:

where

and

where E1,E2,T^* and T^' are my fitting parameters, while T is my abscissa.

I have tried defining a unique function in lsqcurvefit but it does not work. I have some difficulties in defining the integral in the fitting function.

Can someone help me with the code?

Thaks a lot.

### Accepted Answer

Star Strider
on 3 Mar 2021

Try this:

function y = integralfit(b,Tv,R,nu,T0)

% % % MAPPING: b(1) = E1, b(2) = E2, b(3) = Tstar, b(4) = Tdot

for k = 1:numel(Tv)

k3 = @(T) exp(-b(2).*(1./T - 1./b(3))./R);

K = @(T) exp(-b(1).*(1./T - 1./b(4))./R);

y(k) = (b(1)/nu) .* k3(Tv(k)).* K(Tv(k)) .* exp(-(1./nu).*integral(@(T) k3(T).*K(T), T0, Tv(k), 'ArrayValued',1));

end

end

T = 1:10; % Use Actual Vector

y = rand(size(T)); % Use Actual Vector

R = 9; % Use Actual Value

nu = 7; % Use Actual Value

T0 = T(1); % Use Actual Value

B = lsqcurvefit(@(b,T)integralfit(b,T,R,nu,T0), rand(4,1), T, y)

figure

plot(T, y, 'p')

hold on

plot(T, integralfit(B,T,R,nu,T0), '-r')

hold off

grid

It runs without error and appears to produce appropriate results on a random vector and random extra parameters. I obviously cannot test it with your data to see if it is producing appropriate parameter estimates.

It is somewhat slow because of the loop, however there is no way to correct for that since ‘T’ is the upper limit of integration, and that must be a scalar for integral to work correctly.

Remember to save the ‘integralfit’ function as integralfit.m on your MATLAB search path before you use it.

##### 7 Comments

Star Strider
on 3 Mar 2021

As always, my pleasure!

I am glad that you were able to estimate the parameters correctly, since they eluded me, even using the genetic algorithm.

### More Answers (0)

### See Also

### Community Treasure Hunt

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

Start Hunting!