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

2 views (last 30 days)

Show older comments

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 Comments

Star Strider
on 28 May 2024

### Accepted Answer

Star Strider
on 28 May 2024

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 Comments

Star Strider
on 28 May 2024

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.

.

### More Answers (1)

SAI SRUJAN
on 28 May 2024

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!

### See Also

### Community Treasure Hunt

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

Start Hunting!