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
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?
채택된 답변
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
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
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 Center 및 File Exchange에서 Particle & Nuclear Physics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!