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

10 views (last 30 days)
Jasmine on 29 Jul 2022
Edited: Jasmine on 31 Jul 2022
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 );
======================================================
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!

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
##### 2 CommentsShowHide 1 older comment
Matt J on 30 Jul 2022
You're quite welcome, but please Accept-click the answer to indicate that it worked.

### More Answers (1)

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.
##### 1 CommentShowHide None
Jasmine on 31 Jul 2022
Edited: Jasmine on 31 Jul 2022
Hi, this would work as well! Really appreciate for the input!