# Initial conditions in a fitting

5 views (last 30 days)
Richard Wood on 26 Feb 2023
Answered: Walter Roberson on 26 Feb 2023
Hello everyone
I'm trying to figure out the impact of boundary conditions on a fitting. The file where my fitting is defined looks like:
function [fitresult,gof]=Fitting(space,magnetization,initial_magnetization,initial_conditions)
if round(initial_magnetization(1),1)==0.7 && round(initial_magnetization(end),1)==0.7
fitting_function='(1/sqrt(2))*(sin(atan(exp(Q*(x-x0)/Delta)))+cos(atan(exp(Q*(x-x0)/Delta))))';
elseif round(initial_magnetization(1),1)==-0.7 && round(initial_magnetization(end),1)==-0.7
fitting_function='-(1/sqrt(2))*(sin(atan(exp(Q*(x-x0)/Delta)))+cos(atan(exp(Q*(x-x0)/Delta))))';
elseif round(initial_magnetization(1),1)==-0.7 && round(initial_magnetization(end),1)==0.7
fitting_function='(1/sqrt(2))*(sin(atan(exp(Q*(x-x0)/Delta)))-cos(atan(exp(Q*(x-x0)/Delta))))';
elseif round(initial_magnetization(1),1)==0.7 && round(initial_magnetization(end),1)==-0.7
fitting_function='-(1/sqrt(2))*(sin(atan(exp(Q*(x-x0)/Delta)))-cos(atan(exp(Q*(x-x0)/Delta))))';
end
ft=fittype(fitting_function,'independent','x','dependent','y');
opts=fitoptions('Method','NonlinearLeastSquares');
opts.Display='Off';
opts.StartPoint=initial_conditions;
[fitresult,gof]=fit(space,magnetization,ft,opts);
On the other hand, in the file where I process the data I have the following:
% Initial conditions
Initial_Conditions=[Delta0 x0_Q Topological_Charge(1)];
% Let's apply the fitting to the mx component
for j=1:length(Time)
[fitresult,gof]=Fitting(Spatial_Grid,mx(:,j),mx(:,1),Initial_Conditions);
Delta_Q(j)=fitresult.Delta; % m
x0_Q(j)=fitresult.x0; % m
Initial_Conditions=[Delta_Q(j) x0_Q(j) Topological_Charge(j)];
end
I was wondering on which order I have to write the initial conditions (Delta0, x0_Q, Topological_Charge), to match what it is defined in the function Fitting. It is important to note that Delta0/Delta_Q corresponds to Delta, x0_Q to x0, and Topological_Charge to Q in the fitting function environment. That is, as, for example:
Initial_Conditions=[Delta_Q(j) x0_Q(j) Topological_Charge(j)];
Initial_Conditions=[x0_Q(j) Delta_Q(j) Topological_Charge(j)];
...

Walter Roberson on 26 Feb 2023
coeffnames(ft)
will tell you the names of the model coefficients. The order they are displayed in is the same order as you should use for the initial values.