Model fit using fminunc based on measured data

15 views (last 30 days)
Hi all,
I have measurement data that I would like to use to fit a model. I want to use the command "fminunc" for the fit, as I will later have a model with several coefficients (9) and variables (5). However, in order to get my script to run at all, I have reduced it and you can find it below as a minimal example.
These coefficients (in my minimal example these are A0 and EA) are to be fitted in such a way that the function including these fitted coefficients represents the entire result of my experiments as best as possible (each coefficient is a constant for the entire experimental data set). In my minimal example, I had four test runs at different temperatures (x1), which resulted in different lifetimes (x2).
Code:
kB = 8.617 * 1e-5; % in eV/K
x1 = [233; 264; 295; 326]; % temperatures
x2 = [420000; 970000; 3800000; 10000000]; % lifetimes
% Function to solve
fun = @(A0,EA) ((A0 .* exp(EA/kB./x1)) - x2).^2;
x0 = [2.7 * 1e10, 0.220]; % initial values
% Solve
[p, fval] = fminunc(fun,x0);
However, I only get an error message. Can anyone help me find out where my error is?
Many thanks in advance!
BR, Seb

Accepted Answer

Bruno Luong
Bruno Luong on 25 Oct 2022
kB = 8.617 * 1e-5; % in eV/K
x1 = [233; 264; 295; 326]; % temperatures
x2 = [420000; 970000; 3800000; 10000000]; % lifetimes
% Function to solve
fun = @(x) sum((x1 .* exp(x(2)/kB./x1) - x2).^2); % Change here
x0 = [2.7 * 1e10, 0.220]; % initial values
% Solve
[p, fval] = fminunc(fun,x0);
Local minimum possible. fminunc stopped because it cannot decrease the objective function along the current search direction.
  7 Comments
Bruno Luong
Bruno Luong on 25 Oct 2022
I only fix your original code.
I won't comment on general case beside what I told you: fitting exponantial is well known to have local minima, so you have to work through to overcome this and not throw the function and first guess without care.
SebK
SebK on 25 Oct 2022
Now it works. Great job!!!. Thank you two ;-)

Sign in to comment.

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!