Model fit using fminunc based on measured data
15 views (last 30 days)
Show older comments
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
x0 = [2.7 * 1e10, 0.220]; % initial values
% Solve
However, I only get an error message. Can anyone help me find out where my error is?
Many thanks in advance!
BR, Seb
0 Comments
Accepted Answer
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);
7 Comments
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.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!