Matlab's polyfit gives different results when the independent variable is integer than when the independent variable is floating point. Why is that so?
21 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
John Williams
el 17 de Nov. de 2025 a las 21:29
I have a function that measures the ratio of two sets of electron particle flux. It varies wildly in time. My time is given in datenums. I understand that date numbers are generally frowned upon but polyfit does not accept datetime objects). The particle flux ratio should be close to one (that is the slope of the function should be zero) and I am interested in finding the intercept (the intercept will give the ratio of the two different particle fluxes), and checking that the slope is zero or as close as is numerically possible. So I have invoked the Matlab function polyfit. Here is the code :
[weightedFluxRatioPolyFit, weightedS1] = polyfit(dateNumberDays, weightedFluxRatio, 1);
where dateNumberDays = [0, 0.000694444403052, 0.001388888922520, ... 18.999305555596948];
The number of elements in dateNumberMinutes is 27360(1440 minutes per day*19 days). The units of datNumberDays are days. My results come out in weightedFluxRatioPolyFit as :
weightedFluxRatioPolyFit = [0.0049913, 0.1395071]; where 0.0049913 = slope (units = 1/day*(1 day)/(1440 minutes) = 3.466e-6(1/minutes)) and 0.1395071 = intercept(units here are arbitrary, since the input is a ratio of fluxes and we want m*x to have the same units.).
Next I tried the following function call.
[weightedFluxRatioPolyFit, weightedS1] = polyfit(numDataPointsMinutes, weightedFluxRatio, 1);
where numDataPointsMinutes = [1,2,3, ... 27360]; The number of elements are 27360 but here the units are minutes. My results come out in weightedFluxRatioPolyFit as :
weightedFluxRatioPolyFit = [ 0.0000035 0.1595025]; where 0.0000035 = slope (units = 1/minutes) and 0.1595025 = intercept.
My question is why is Matlab's polyfit function giving different results when the independent variable is different. The inputs, dateNumberMinutes and numDataPointsSeconds have the different time intervals(days and minutes, respectively), and the delta time interval between the any element of the vectors is the same(minutes). When I convert the two slope to the equivalent units I get pretty much the same result. The problem is that when I compare the two intercepts I get different results (0.1395071 and 0.1595025) and these (I think) do not need a conversion factor. The issue could be in the implementation of the polyfit algorithm or in numerical overflow or some other numerical error but it comes down to having to pick one of the intercepts and I am not sure which one is correct. Does anyone have some guidance?
0 comentarios
Respuestas (1)
Torsten
el 17 de Nov. de 2025 a las 21:56
Editada: Torsten
el 17 de Nov. de 2025 a las 22:02
To be consistent, you must either use
numDataPointsMinutes = [0,1,2,3, ... 27359];
instead of
numDataPointsMinutes = [1,2,3, ... 27360];
or
dateNumberDays = [0.000694444403052, 0.001388888922520, ... 19];
instead of
dateNumberDays = [0, 0.000694444403052, 0.001388888922520, ... 18.999305555596948];
At the moment, you use
dateNumberDays = [0, 0.000694444403052, 0.001388888922520, ... 18.999305555596948];
in comparison with
numDataPointsMinutes = [1,2,3, ... 27360];
which is incorrect.
0 comentarios
Ver también
Categorías
Más información sobre MATLAB Coder 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!