Curve fit not spanning the whole data set

1 view (last 30 days)
Salvador
Salvador on 21 Dec 2024
Commented: Salvador on 22 Dec 2024
I have the following datasets:
L=1;
D=2*0.02;
V1=[0.00010, 0.00025, 0.00050, 0.00075, 0.00100, 0.00250, 0.00500, 0.00750];
V2=[0.01, 0.10, 0.50, 1.00, 3.00, 5.00, 7.00, 10.00];
V=[V1, V2];
Re=(1000*V*D)/(0.001);
lambda_E=(p*D)./(0.5*1000*L*V.^2);
lambda_HP=64./Re;
lambda_W=1.02*(log10(Re)).^(-2.5);
I want to draw a curve over these points, so I fitted them, using the Curve Fitting Toolbox, to a custom equation, that being: a*(log10(x)).^(-2.5), where 'a' is a coefficient. If there's any other way to create a smooth line that goes over these points please let me know about it. I then exported the corresponding code to a function I'd made earlier. Said code looks as follows:
[xData, yData] = prepareCurveData( Re, lambda_W );
% Set up fittype and options.
ft = fittype( 'a*(log10(x)).^(-2.5)', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonLinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = 1.02;
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
plot( Re, lambda_W, 'o', MarkerSize=3, MarkerFaceColor='k', MarkerEdgeColor='k' );
h = plot( fitresult );
set(h,LineWidth=1.3,Color='r');
However, when I run the original program in which I use the function, the curve I just mentioned (colored red in the picture) stops past a certain value of the 'Re' axis:
This is the full code of the program and the function, respectively:
L = 1;
D = 2*0.02;
V1 = [ 0.00010, 0.00025, 0.00050, 0.00075, 0.00100, 0.00250, 0.00500, 0.00750 ];
V2 = [ 0.01, 0.10, 0.50, 1.00, 3.00, 5.00, 7.00, 10.00 ];
V = [ V1, V2 ];
logV = log( V );
p1 = [ 0.00247, 0.00619, 0.01250, 0.01890, 0.02550, 0.06770, 0.14800, 0.24000 ];
p2 = [ 0.3400, 8.6500, 116.2070, 370.3552, 2442.9590, 5996.8000, 10924.6200, 21040.5100 ];
p = [ p1, p2 ];
logp = log( p );
fit_logV_logp( logV, logp );
%-------------------------------------------------------------------------------------------
Re = (1000*V*D)/(0.001);
lambda_E = (p*D)./(0.5*1000*L*V.^2);
lambda_HP = 64./Re;
lambda_W = 1.02*(log10(Re)).^(-2.5);
fit_lambda_Re( Re, lambda_E, lambda_HP, lambda_W );
function [fitresult, gof] = fit_lambda_Re(Re, lambda_E, lambda_HP, lambda_W)
%-------------------------------------------------------------------------------------------
% Plot data.
subplot( 1, 2, 2 );
plot( Re, lambda_E, 'o', MarkerSize=4, MarkerFaceColor='g' );
xscale('log');
yscale('log');
hold on;
%-------------------------------------------------------------------------------------------
[xData, yData] = prepareCurveData( Re, lambda_HP );
% Set up fittype and options.
ft = fittype( 'rat01' );
opts = fitoptions( 'Method', 'NonLinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [64, -1];
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
plot( Re, lambda_HP, 'o', MarkerSize=3, MarkerFaceColor='k', MarkerEdgeColor='k' );
h = plot( fitresult );
set(h,LineWidth=1.3,Color='b');
%-------------------------------------------------------------------------------------------
[xData, yData] = prepareCurveData( Re, lambda_W );
% Set up fittype and options.
ft = fittype( 'a*(log10(x)).^(-2.5)', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonLinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = 1.02;
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
plot( Re, lambda_W, 'o', MarkerSize=3, MarkerFaceColor='k', MarkerEdgeColor='k' );
h = plot( fitresult );
set( h, LineWidth=1.3, Color='r' );
hold off;
%-------------------------------------------------------------------------------------------
% Label axes
title('\lambda vs. Re');
xlabel( 'Re' );
ylabel( '\lambda' );
legend( 'Valores experimentales', 'Valores teóricos', 'Hagen-Poiseuille', '', 'White', 'Location', 'NorthEast', 'Interpreter', 'none' );
grid on;
I've double checked every single line of code of my program but nothing seems to be amiss. I also wasn't able to find anyone else with the same problem. Any help would be appreciated.
  7 Comments
Torsten
Torsten on 22 Dec 2024
Edited: Torsten on 22 Dec 2024
1.02*(log10(1)).^(-2.5) = Inf, and the next value for x in your list is 1+(1e6-1)/99999 which is approximately 11:
x = linspace( 1, 1e6, 100000 )
x = 1×100000
1.0000 11.0001 21.0002 31.0003 41.0004 51.0005 61.0005 71.0006 81.0007 91.0008 101.0009 111.0010 121.0011 131.0012 141.0013 151.0014 161.0014 171.0015 181.0016 191.0017 201.0018 211.0019 221.0020 231.0021 241.0022 251.0023 261.0023 271.0024 281.0025 291.0026
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
That's the first visible value for Re on the red curve.

Sign in to comment.

Answers (0)

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!