# Curve fitting with custom function

6 views (last 30 days)
Da125 on 17 Nov 2021
Commented: Star Strider on 18 Nov 2021
Hello community,
I am trying to do a curve fitting on some experimental data with a custom function. I am more specifically trying to bring closer my function to the data. The function is the following one: Every parameters of the function, except the shear rate ( ) , can vary in order to fit best the data.
Here is a graph with the data and the function plotted with initial values: The initial values are the following ones:
muInf=0.0035 %[Pa.s]
mu0=0.108; %[Pa.s]
lambda=8.2;
n=0.3;
a=0.64;
viscCar=muInf+(mu0-muInf)*(1+(lambda.*shearRate).^a).^((n-1)/a);
loglog(shearRate,viscCar,'k');
I already tried to use the curve fitting toolbox but i wasn't able to keep the look of the function.
Does anyone know how i can do that ?
Thank you for the help !

Star Strider on 17 Nov 2021
It would help to have the data.
I would use the fitnlm function for this —
shearRate = logspace(-3, 4, 100); % Create Data
muv = 0.5-tanh(shearRate)*0.01 + randn(size(shearRate))*0.01; % Create Data
shearRate = shearRate(:);
muv = muv(:);
% muInf=0.0035; %[Pa.s]
% mu0=0.108; %[Pa.s]
% lambda=8.2;
% n=0.3;
% a=0.64;
B0 = [0.0035; 0.108; 8.2; 0.3; 0.64];
viscCar = @(muInf,mu0,lambda,a,n,shearRate) muInf+(mu0-muInf)./(1+(lambda.*shearRate).^a).^((n-1)/a);
viscCarfcn = @(b,shearRate) viscCar(b(1),b(2),b(3),b(4),b(5),shearRate);
mumdl = fitnlm(shearRate,muv, viscCarfcn, B0)
Warning: Rank deficient, rank = 4, tol = 1.142224e+00.
Warning: Rank deficient, rank = 4, tol = 1.739422e+00.
Warning: Rank deficient, rank = 4, tol = 2.345437e+00.
Warning: Rank deficient, rank = 4, tol = 3.085596e+00.
Warning: Rank deficient, rank = 4, tol = 3.036782e+00.
Warning: Rank deficient, rank = 4, tol = 4.094796e+00.
Warning: Rank deficient, rank = 4, tol = 5.447783e+00.
Warning: Rank deficient, rank = 3, tol = 5.383975e+00.
Warning: Rank deficient, rank = 1, tol = 6.115407e+01.
Warning: Some columns of the Jacobian are effectively zero at the solution, indicating that the model is insensitive to some of its parameters. That may be because those parameters are not present in the model, or otherwise do not affect the predicted values. It may also be due to numerical underflow in the model function, which can sometimes be avoided by choosing better initial parameter values, or by rescaling or recentering. Parameter estimates may be unreliable.
mumdl =
Nonlinear regression model: y ~ viscCar(b1,b2,b3,b4,b5,shearRate) Estimated Coefficients: Estimate SE tStat pValue _________ __________ __________ ________ b1 0.23921 3.7712e-33 6.3429e+31 0 b2 0.45235 2.3787e-32 1.9017e+31 0 b3 1.391e-17 5.3563e-18 2.5969 0.010839 b4 0.036323 5.3121e-32 6.8378e+29 0 b5 0.97281 3.2215e-32 3.0197e+31 0 Number of observations: 100, Error degrees of freedom: 99 Root Mean Squared Error: 0.014 R-Squared: -0.682, Adjusted R-Squared -0.682 F-statistic vs. zero model: 1.25e+05, p-value = 2.2e-155
Beta = mumdl.Coefficients.Estimate
Beta = 5×1
0.2392 0.4523 0.0000 0.0363 0.9728
figure
loglog(shearRate,viscCarfcn(Beta,shearRate),'k');
hold on
plot(shearRate, muv, '.b')
hold off
grid This works, and would actually make sense with the actual data.
.
Star Strider on 18 Nov 2021
As always, my pleasure!
.

### More Answers (1)

Alex Sha on 18 Nov 2021
It is hard to get stable and unique result for Da125's problem, especially for parameters of "lambda" and "n". refer to the result below:
Root of Mean Square Error (RMSE): 0.00032178294087402
Sum of Squared Residual: 2.89923930905092E-6
Correlation Coef. (R): 0.998376698597668
R-Square: 0.996756032302778
Parameter Best Estimate
---------- -------------
muinf -0.0902678665303079
mu0 0.00320052355322768
lambda 207505339.640542
a -0.504386079091155
n -1252.71593136491
##### 1 CommentShowHide None
Star Strider on 18 Nov 2021
Negative parameters are apparently not permittted. This was a problem noted in a previous Comment.
.

R2020b

### Community Treasure Hunt

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

Start Hunting!