User defined function for curve fitting, but the defined function is complicated

Hi all,
I am working on this project whihc requires me to fit my experimental data using a complicated function (function is attached in the picture below). May I ask how can I create this fitting function using matlab codes?
Information about function:
function (2):
dependent: A_CC
independent: \hbar*\omega
parameters: A_1, E_b, E_g, \Gamma
function (3):
dependent: A_EC
independent: \hbar*\omega
parameters: A_2, E_b, E_g, \Gamma
Please note that any over lapping terms in both functions should have the same value. For instance, \Gamma exist in both (2) and (3), and hence they need to have the same value.
thank you!

2 comentarios

Shouldn’t the actual ‘independent variable’ be ω rather than ? As I am sure you are aware, is the Planck constant. Is there some specific reason that you want to scale ω by it?
Jack
Jack el 28 de Mayo de 2024
Editada: Jack el 28 de Mayo de 2024
yep, it is the reduced plack's constant (to be more precise). The equation wrote it as since it is a model. Experimentally, the energy is the variable that we vary, and hence I wrote together as one. And the plot in the end is a plot of A against energy (a more meanigful physicall quantity, rather than ω)

Iniciar sesión para comentar.

 Respuesta aceptada

I thought it had something to do with scaling ω. That would depend on the magnitude of ω since it might be difficult to fit extremely large or small values. Is the ξ function also provided somewhere?
For the integral in , consider using the integral function with 'ArrayValued',1. Your data should have three vectors, those being ω, , and . I would be tempted to use lsqcurvefit for this, however there are sevaral options in different Toolboxes, including the Global Opttimization Toolbox that mightt be an intermediate step if there are several local minima (to choose the best parameter set that could then be improved using a gradient-descnent function).

4 comentarios

Hi star strider,
yes, my apologies for missing out the definition of ξ. it is defined as: ξ=
where R is a fitting parameter and the other parameters/ variables follows the definition from the original post.
Thank you for the solution provided, but may I ask regarding the purpose of 'ArrayValued',1?
Thank you!
My pleasure!
With respect to the ξ function, is R a parameter to be estimated, the Gas constant, or something else?
The 'ArrayValued' name-value pair tells integral that one (or more) of its arguments are matrices or vectors (if more than one, they must all have the same size dimensions). It then iterates appropriately across the provided arrays. Since arguments in parameter estimation problems are usually vectors, with 'ArrayValued' set to true, integral then produces an array of the appropriate size as its output.
.
Jack
Jack el 28 de Mayo de 2024
Editada: Jack el 28 de Mayo de 2024
yes, R is a parameter to be estimated (aka fitting parameter)
Thank you for the explanation provided on 'ArrayValued'. I now have a better understanding of it. but I am still a little unsure of what are the stuff that should go into the 'ArrayValued' function? Are the stuff that goes into the 'ArrayValued' function the paramters of the fitting function?
Also, does matlab also accept Boolean inputs? aka, 1 for true and 0 for false
My pleasure!
One way to code the functions —
xi = @(R,E,Eg) 1 + 10*R(E-Eg) + 126*R.^2*(E - Eg).^2;
Acc = @(Eb,Eg,E,A1,R,hw) (A1.*2.*pi.*sqrt(Eb)./hw) .* integral(@(E) sech((hw.-E)./gamma) .* (xi(R,E,Eg))./(1-exp(-2*pi*sqrt(Eb./(E-Eg)))), Eg, Inf, 'ArrayValued',1);
S_fcn = @(Eb,Eg,gamma,hw) 1.0./(cosh(hw+(Eb./4.0-Eg)./gamma).*8.0)+1.0./(cosh(hw+(Eb./9.0-Eg)./gamma).*2.7e+1)+1.0./(cosh(hw+(Eb./1.6e+1-Eg)./gamma).*6.4e+1)+1.0./(cosh(hw+(Eb./2.5e+1-Eg)./gamma).*1.25e+2)+1.0./(cosh(hw+(Eb./3.6e+1-Eg)./gamma).*2.16e+2)+1.0./(cosh(hw+(Eb./4.9e+1-Eg)./gamma).*3.43e+2)+1.0./cosh(hw+(Eb-Eg)./gamma);
Aec = @(Eb,Eg,A2,hw) A2.*2*pi*Eb.^(3/2)/hw .* S_fcn(Eb,Eg,gamma,hw);
All of this is untested, so be sure to check it.
I went offlilne to create and calculate ‘S_fcn’, that being:
syms j hw Eb Eg gamma
S = symsum((1/j^3) * sech((hw - ((Eg - Eb/j^2))/gamma)), j, 1, 7)
S = simplify(S, 500)
S_fcn = matlabFunction(S)
because the Symbolic Math Toolbox is better at such things than I am.
I’m including it here so you can check it for any errors.
To use lsqcurvefit (and pretty much every other such function outside the Curve Fitting Toolbox that has its own conventions), the functions to fit would have this syntax:
fcn = @(parameter_vector, independent_variable_array) ...
so for example ‘Acc’ would be coded as:
Acc_fcn = @(b(1),b(2),b(3),b(4),b(5),hw) Acc(Eb,Eg,E,A1,R,hw);
and a call to it in lsqcurvefit would be:
B0 = rand(5,1);
[B,rn] = lsqcurvefit(Acc_fcn(b, hw), B0, hw, Acc)
or something similar.
It will be necessary for you to experiment with it. It could take a bit of time to get it working correctly, especially since I could have made coding errors, so please check that.
.

Iniciar sesión para comentar.

Más respuestas (1)

Hi Jack,
I understand that you are facing an issue with fitting a function in MATLAB.
To fit experimental data using custom functions in MATLAB, we can use the 'fittype' and 'fit' functions from MATLAB's Curve Fitting Toolbox.
Please follow the below code sample to proceed further,
% Define the fitting functions with shared parameters
fitFuncCC = fittype('(A1 * exp(-((x - Eg)^2) / (2*Gamma^2))) + Eb', 'independent', 'x', 'coefficients', {'A1', 'Eg', 'Gamma', 'Eb'});
fitFuncEC = fittype('(A2 * exp(-((x - Eg)^2) / (2*Gamma^2))) + Eb', 'independent', 'x', 'coefficients', {'A2', 'Eg', 'Gamma', 'Eb'});
% load or compute required data/variables
% Fit the first dataset
[fitresultCC, gofCC] = fit(hbarOmega, ACCData, fitFuncCC);
% Fit the second dataset
[fitresultEC, gofEC] = fit(hbarOmega, AECData, fitFuncEC);
For a comprehensive understanding of the 'fittype' and 'fit' functions in MATLAB, please refer to the following documentation.
I hope this helps!

1 comentario

Hi Sai Srujan,
Thank you for the comprehensive answer and the code provided. I will definetely give it a try.
Appreciate it!

Iniciar sesión para comentar.

Categorías

Más información sobre Linear and Nonlinear Regression en Centro de ayuda y File Exchange.

Productos

Versión

R2024a

Etiquetas

Preguntada:

el 28 de Mayo de 2024

Comentada:

el 28 de Mayo de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by