Curve Fitting in MATLAB

2 views (last 30 days)
LM
LM on 21 Jan 2022
Edited: LM on 18 Apr 2023
Hi guys!
I am trying to using HC model, recorgnized as non-linear curve fitting for 2D data sets. However, it always made error in there. I am wondering if you guys have any suggestions on the "complex function" can be used in curve fitting?
  1 Comment
Alex Sha
Alex Sha on 21 Jan 2022
It will be much better for getting other's help if the full code is posted

Sign in to comment.

Accepted Answer

Bjorn Gustavsson
Bjorn Gustavsson on 21 Jan 2022
For this type of problems I learnt (before the arrival of the gui-curve-fitting tools) to use the standard function-minimization-functions (fminsearch, lsqnonlin etc) and once you know them this becomes reasonably straightforward:
1, write a function for your model-function:
function epsilon_f = epsilon_f_of_sigma(sigma, theta, pars)
b0 = pars(1);
gamma = pars(2);
c = pars(3);
theta_bar = pars(4);
epsilon_p = pars(5);
epsilon_0 = pars(6);
n = pars(7);
a = pars(8);
% Above you might simply replace the assignments of variable parameters to
% known values for any of these and reduce the size of the free parameter
% variable pars
if epsilon_p < epsilon_0
b = b0;
else
b = b0*(1 +gamma*log(epsilon_p/epsilon_0));
end
% and so on for f1, f2 and f3...
epsilon_f = b*(1+c).^(1./n).*((1/2^(1./a).*((f1-f2).^a + (f2-f3).^a + (f1-f3).^a).^(1./a)) + ...
c.*(2.*sigma+f1 + f3)).^(-1./n);
end
Then you write an error-function (for use with fminsearch) or a residual-function (for use with lsqnonlin). Here I've assumed that both sigma and theta are a pair of independent variables, the modification should be obvious.
function err = err_fcn(pars,data2fit2,weights4scaling,model_fcn,sigma_indep,theta_indep)
model_data = model_fcn(sigma_indep,theta_indep,pars);
err = sum((data2fit2(:) - model_data(:)).^2.*weights4scaling);
end
Then you fit your parameters to make the model fit your data:
par0 = [pi,sqrt(2),exp(1),pi/sqrt(2),3e8,139,2/(1+sqrt(5)),1.6e-19]; % you'll have to make some initial guess here
par = fminsearch(@(par) err_fcn(pars,...
data2fit2,...
ones(size(data2fit2)),...
@(s,t,p) epsilon_f_of_sigma(s,t,p),...
sig,...
theta),...
par0);
% Which should give you an estimate of the parameters giving the best fit.
% that you can then use to calculate the best fitting epsilon_f:
epsilon_f_modeled = epsilon_f_of_sigma(sig, theta, par);
HTH

More Answers (0)

Community Treasure Hunt

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

Start Hunting!