Problem with fitting a loglog plot
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Wissem-Eddine KHATLA
el 17 de En. de 2024
Comentada: Star Strider
el 23 de En. de 2024
Hello everyone,
I have some problems trying to obtain the best stratight line possible in order to fit a loglog data set. I keep having an error related to polyfit :
Warning: Polynomial is badly conditioned. Add points with distinct X values, reduce the degree of the polynomial, or try centering
and scaling as described in HELP POLYFIT.
I tried to find some other alternaitves but I am stuck to find a reliable solution to this problem. Any clue how I can solve it ?
The data used for the script is provided attached to my post.
Thanks for your help,
SCRIPT :
data_exp_high = load("data_Q_1e-8.txt");
t_exp_high = data_exp_high(:,1);
R_exp_high = data_exp_high(:,2);
% Ajustement linéaire des logarithmes
log_t_exp = log(t_exp_high);
log_R_exp = log(R_exp_high);
% Ajustement linéaire en utilisant polyfit
coefficients = polyfit(log_t_exp, log_R_exp, 1);
% Récupération des paramètres alpha et beta
alpha_estime = exp(coefficients(2));
beta_estime = coefficients(1);
% Affichage des résultats
disp(['Paramètre alpha estimé : ', num2str(alpha_estime)]);
disp(['Paramètre beta estimé : ', num2str(beta_estime)]);
% Tracé des courbes
figure;
loglog(t_exp_high, R_exp_high, 'o', 'DisplayName', 'Données expérimentales');
hold on;
loglog(t_exp_high, alpha_estime * t_exp_high.^beta_estime, 'r-', 'DisplayName', 'Ajustement linéaire en log-log');
0 comentarios
Respuesta aceptada
Mathieu NOE
el 17 de En. de 2024
hello
I knew even before opening your data that there would be a zero somewhere ... bingo !
so it works better once you have removed zeros (or negative data if that may arise once)
data_exp_high = load("data_Q_1e-8.txt");
t_exp_high = data_exp_high(:,1);
R_exp_high = data_exp_high(:,2);
% eliminer les valeurs nulles
ind = (t_exp_high>0 & R_exp_high>0);
t_exp_high = t_exp_high(ind);
R_exp_high = R_exp_high(ind);
% Ajustement linéaire des logarithmes
log_t_exp = log(t_exp_high);
log_R_exp = log(R_exp_high);
% Ajustement linéaire en utilisant polyfit
coefficients = polyfit(log_t_exp, log_R_exp, 1);
% Récupération des paramètres alpha et beta
alpha_estime = exp(coefficients(2));
beta_estime = coefficients(1);
% Affichage des résultats
disp(['Paramètre alpha estimé : ', num2str(alpha_estime)]);
disp(['Paramètre beta estimé : ', num2str(beta_estime)]);
% Tracé des courbes
figure;
loglog(t_exp_high, R_exp_high, 'o', 'DisplayName', 'Données expérimentales');
hold on;
loglog(t_exp_high, alpha_estime * t_exp_high.^beta_estime, 'r-', 'DisplayName', 'Ajustement linéaire en log-log');
Más respuestas (2)
Alan Stevens
el 17 de En. de 2024
polyfit doesn't like the log of zero. One option is to remove the first row of the data:
data_exp_high = load("data_Q_1e-8.txt");
data_exp_high(1,:) = [];
t_exp_high = data_exp_high(:,1);
R_exp_high = data_exp_high(:,2);
% Ajustement linéaire des logarithmes
log_t_exp = log(t_exp_high);
log_R_exp = log(R_exp_high);
% Ajustement linéaire en utilisant polyfit
coefficients = polyfit(log_t_exp, log_R_exp, 1);
% Récupération des paramètres alpha et beta
alpha_estime = exp(coefficients(2));
beta_estime = coefficients(1);
% Affichage des résultats
disp(['Paramètre alpha estimé : ', num2str(alpha_estime)]);
disp(['Paramètre beta estimé : ', num2str(beta_estime)]);
% Tracé des courbes
figure;
loglog(t_exp_high, R_exp_high, 'o', 'DisplayName', 'Données expérimentales');
hold on;
loglog(t_exp_high, alpha_estime * t_exp_high.^beta_estime, 'r-', 'DisplayName', 'Ajustement linéaire en log-log');
Star Strider
el 17 de En. de 2024
Using a linear regression on logarithmically-transformed data creates multiplicative errors, not additive errors that least squares approaches require (and assume).
Use a nonlinear approach, and no data-editing is required, and the results are more accurate —
M1 = readmatrix('data_Q_1e-8.txt')
x = M1(:,1);
y = M1(:,2);
loglogfcn = @(b,x) x.^b(1) .* exp(b(2));
[B,fv] = fminsearch(@(b) norm(y - loglogfcn(b,x)), randn(2,1))
figure
plot(x, y, 'pb', 'MarkerFaceColor','b')
hold on
plot(x, loglogfcn(B,x), '-r', 'LineWidth',2)
hold off
grid
xlabel('x')
ylabel('y')
title('Linear Scale')
text(25, 0.0325, sprintf('$f(x) = x^{%.3f}\\cdot%.5f$', B(1),exp(B(2))), 'Interpreter','latex')
Results = table(x, y, loglogfcn(B,x), y-loglogfcn(B,x), 'VariableNames',{'X','Y','Fitted Data','Error'})
RMS_Error = rmse(Results.Y, Results.('Fitted Data'))
figure
loglog(x, y, 'pb', 'MarkerFaceColor','b')
hold on
plot(x, loglogfcn(B,x), '-r', 'LineWidth',2)
hold off
grid
xlabel('x')
ylabel('y')
title('‘loglog’ Scale')
text(2.5, 0.02, sprintf('$f(x) = %.3f\\cdot log(x) %+.3f$', B), 'Interpreter','latex')
Lv = x>0;
B = polyfit(log(x(Lv)), log(y(Lv)), 1)
yfit = exp(polyval(B, log(x(Lv))));
figure
loglog(x, y, 'pb', 'MarkerFaceColor','b')
hold on
plot(x(Lv), yfit, '-r', 'LineWidth',2)
hold off
grid
xlabel('x')
ylabel('y')
title('‘loglog’ Scale')
text(2.5, 0.02, sprintf('$f(x) = %.3f\\cdot log(x) %+.3f$', B), 'Interpreter','latex')
Results = table(x(Lv), y(Lv), yfit, y(Lv)-yfit, 'VariableNames',{'X','Y','Fitted Data','Error'})
RMS_Error = rmse(Results.Y, Results.('Fitted Data'))
.
2 comentarios
Star Strider
el 23 de En. de 2024
My pleasure!
I should have named ‘loglogfcn’ as ‘powerfcn’ since that would be correct.
Using nonlinear techniques to fit a power function (or a log-log plot) is correct, and would produce the most robust parameter estimates. (Here, the errors are relatively small, however even so, the errors of the linear log-transformed fit are about 4½% larger than the errors of the nonlinear fit.) The choice of regression model depends entirely on the data and the process that created the data, however linearising data is definitely not something I encourage. (That might once have been acceptable — before computers came into wide use — however it currently is not.)
Also, using fminsearch here is reasonably robust, since it uses a derivative-free optimisation method, however for more difficult problems, a global optimisation method such as ga would be more likely to succeed in initially estimating the best parameter set, and the parameters then can be ‘tuned’ using a hybrid approach with a gradient-descent optimisation technique. It depends on the data and the complexity of the problem.
So to answer your question:
‘Do you think that it can be sensitive if we use another dataset for example ?’
Yes, if the data are similar to those presented here, and the power function model is appropriate to the data.
err_powerfcn = 1.2739e-04;
err_logloglinear = 1.3317e-04;
rel_err = err_logloglinear / err_powerfcn
.
Ver también
Categorías
Más información sobre Interpolation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!