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

8 次查看(过去 30 天)
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 个评论
Star Strider
Star Strider 2024-5-28
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 2024-5-28
编辑:Jack 2024-5-28
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 ω)

请先登录,再进行评论。

采纳的回答

Star Strider
Star Strider 2024-5-28
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 个评论
Jack
Jack 2024-5-28
编辑:Jack 2024-5-28
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
Star Strider
Star Strider 2024-5-28
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.
.

请先登录,再进行评论。

更多回答(1 个)

SAI SRUJAN
SAI SRUJAN 2024-5-28
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!

类别

Help CenterFile Exchange 中查找有关 Particle & Nuclear Physics 的更多信息

标签

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by