# Solving poor convergence issues using lsqcurvefit

3 views (last 30 days)
Cole on 30 May 2021
Edited: Matt J on 30 May 2021
I am struggling to fit a non-linear equation using the lsqcurvefit tool, searching for two variables. Although a local minimum is found, the fit is, as can be seen, quite poor. I have tried to use different search algoriths (levenberg-marquardt, versus trust-region-reflective). I have used a rougher fit initially to try and inform my initial guess for the more rigorous fit. Yet, the final form of the fit is indeed quite poor, as can be seen from the output graph below.
Can anyone tell me what I might be missing in properly using lsqcurvefit, and if nothing, where they suggest I go from there?
clear all
clc
%Input constants
R=8.3145; %J/molK
b=3.04E-10; %m
D_ov_Li=3.1E-5; %m2/s
D_ob_Li=4.3E-14; %m3/s
Q_ob_Li=33700;%J/mol
Q_ov_Li=56100;%J/mol
G_0=3.68E3; % MPa
G_300=3.03E3; % MPa
delta=sqrt(5/2)*b;
k_b=1.38064852E-23;
%Temperature dependent functions
E=@(T) (-((G_0-G_300)/300)*T+10050).*1E6; %Pa
D_v_Li=@(T) D_ov_Li*exp(-Q_ov_Li/(R*T)); %m2/s
delt_D_b_Li=@(T) D_ob_Li*exp(-Q_ob_Li/(R*T)); %m3/s
D_eff_PL_Li=@(x,T) D_v_Li(T).*(1+(20*(delta)./(b^2)).*((x).^2)*(delt_D_b_Li(T)./(D_v_Li(T)))); %m2/s
%Raw data to be fit
Ys=[680688.3365,697514.3403,673040.153,1424063.116,1439842.209,1428007.89,1005917.16,970414.2012,1025641.026];
Temp = 274.15;
SR = [0.0001,0.0001,0.0001,0.01,0.01,0.01,0.001,0.001,0.001];
%Conduct rough fit to equation SR = A_1 * (x ^ n_1)
x_val_nfit=Ys/E(Temp);
p_Ys=polyfit(log(x_val_nfit),log(SR),1);
n_1=p_Ys(1);
A_1=exp(p_Ys(2));
%Conduct detailed fit: SR = A_2 * D_eff_PL_Li(x,T) *(E(T)*b/k_b*T) * x^n2
pref=@(xdata) D_eff_PL_Li(xdata,Temp).*E(Temp)*b./(k_b.*Temp); % Group D_eff_PL_Li(x,T) *(E(T)*b/k_b*T) into prefactor called pref
x_data=Ys./E(Temp);
y_data=SR;
SR_fn=@(x,xdata) x(1).*pref(xdata).*(xdata.^x(2)); %Objective function for fit
x0=[(A_1./mean(pref(x_data))),n_1-2]; %Initial guesses for fit
[x,resnorm,~,exitflag,output]=lsqcurvefit(SR_fn,x0,x_data,y_data,[0,2.5],[inf,6]); %Set upper and lower bound for n2 as 2.5 to 6 based on physical knowledge of system.
A_2 = x(1);
n_2 = x(2);
%Plot results
figure
hold on
scatter(x_data,SR_fn(x,y_data),'b')
scatter(x_data,y_data,'r');
set(gca, 'YScale', 'log')
set(gca, 'XScale', 'log')
box on
xlabel('x vals: \sigma_y / E')
ylabel('y vals: SR ')
legend('Fit Estimation','Experimental Data')

Matt J on 30 May 2021
Edited: Matt J on 30 May 2021
There's nothing wrong with the fit. You're just plotting it incorrectly,
scatter(x_data,SR_fn(x,x_data),'b') %<-- should be x_data, not y_data

### Categories

Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange

R2020a

### Community Treasure Hunt

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

Start Hunting!