How do I set coefficient variables in Fittype that was generated in Fitting toolbox?

30 views (last 30 days)
Hello.
I am trying to fitting some equations to my data and here I am using Fittype which was generated by Curve Fitting Application. The fitting function is below.
I am wondering how I can set coefficient on this equation.
For example, the current equation is
A1*exp(B1*-2.7)*exp(-1*Ea1/8.62e-5*(1/(273+105)))*log(1+C*x)+A2*exp(B2*-2.7)*exp(-Ea2/8.62e-5*(1/(273+105)))*x^n1
but I wanted to change this to
A1*exp(B1*V)*exp(-1*Ea1/8.62e-5*(1/(273+T)))*log(1+C*x)+A2*exp(B2*V)*exp(-Ea2/8.62e-5*(1/(273+T)))*x^n1
The values of V and T will be given in the script. V = [-1.8 -2.7 -3.0] and T = [30 70 105]
The fitting function as of now, if I wanted to run with different V (voltage), I need to generate another fitting function.
I would really appreciate if you provide any input to go through it. Thank you!
======================================================
function [fitresult, gof] = testfitstress(ts, y_Ts)
%% Fit: 'fitstress'.
[xData, yData] = prepareCurveData( ts, y_Ts );
% Set up fittype and options.
ft = fittype( 'A1*exp(B1*-2.7)*exp(-1*Ea1/8.62e-5*(1/(273+105)))*log(1+C*x)+A2*exp(B2*-2.7)*exp(-Ea2/8.62e-5*(1/(273+105)))*x^n1', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [0.538342435260057 0.996134716626885 0.106652770180584 0.86869470536351 0.0781755287531837 0.0844358455109103 0.399782649098896 0.259870402850654];
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
======================================================
  3 Comments
Matt J
Matt J on 29 Jul 2022
Edited: Matt J on 29 Jul 2022
Note that expressions like this,
A1*exp(B1*V)*exp(-1*Ea1/8.62e-5*(1/(273+T)))
and,
A2*exp(B2*V)*exp(-Ea2/8.62e-5*(1/(273+T)))
do not involve the independent variable x and can take on any value by suitable selection of A1 and A2. They can therefore be replaced with single coefficients Q1 and Q2, simplifying the model to
y = Q1*log(1+C*x)+ Q2*x^n1
Not to do so gives you a highly over-parametrized problem.
Once you have fitted Q1 and Q2, you can explore valid values for the original parameters by considering the equations,
A1*exp(B1*V)*exp(-1*Ea1/8.62e-5*(1/(273+T))) = Q1
A2*exp(B2*V)*exp(-Ea2/8.62e-5*(1/(273+T))) = Q2
for different values of T and V. However, because this is a system of 2 equations in 6 unknowns, you should expect that many possible solutions exist.
Jasmine
Jasmine on 30 Jul 2022
Thanks for the advice. I have tried it. After the first fitting including Q1 and Q2, I needed to put Q1 and V or T inputs as a new x in the second fitting stage. Here, I could separate coefficients in front of V or T with Q1 or Q2 mathematically so this method was not applied to me. But I really appreiate to provide another point of view in this fitting. Thank you!

Sign in to comment.

Accepted Answer

Matt J
Matt J on 29 Jul 2022
Edited: Matt J on 29 Jul 2022
Instead of a fitType object, use an anonymous function like in this example,
Vset = [-1.8 -2.7 -3.0]; Tset = [30 70 105]
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [0.538342435260057 0.996134716626885 0.106652770180584 0.86869470536351 0.0781755287531837 0.0844358455109103 0.399782649098896 0.259870402850654];
for i=1:numel(Vset)
V=Vset(i); T=Tset(i)
fitFun=@(A1,B1,A2,B2...,x) A1*exp(B1*V)*exp(-1*Ea1/8.62e-5*(1/(273+T)))*...
log(1+C*x)+A2*exp(B2*V)*exp(-Ea2/8.62e-5*(1/(273+T)))*x.^n1;
[fitresult{i}, gof{i}] = fit( xData, yData, fitFun, opts );
end

More Answers (1)

Steven Lord
Steven Lord on 31 Jul 2022
On the documentation page for the fittype function, see the "Use Anonymous Functions to Work with Problem Parameters and Workspace Variables" example that has a problem-dependent parameter c (which is not a coefficient to be fit) and how that problem-dependent parameter is assigned a value when the fit is performed.

Community Treasure Hunt

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

Start Hunting!