issue with polyval and polyfit measuring time

4 visualizaciones (últimos 30 días)
Ashley Sullivan
Ashley Sullivan el 10 de Mzo. de 2020
Comentada: Rena Berman el 14 de Mayo de 2020
Hi!
So I'm working on a code that evaluates how fast a loop can evaluate a varying range of numbers. I put these numbers into an array called x and then the times taken into an array called y. Then I used polyfit and polyval to try to see how long it would take to evaluate numbers between 1 and 1000000000, but when I get my answer, it is very small. I'd expect it to increase like all the others so I think I did something wrong but I am not sure what.
figure(6)
x = [ 1000, 10000, 100000, 1000000]; % represents the tested arrays
y = [ 0.076243, 0.251277, 3.053055, 64.876395]; % represents the time taken
loglog(x,y,'bo--');
title('Time Needed to Calculate the Prime Numbers in an Array');
xlabel('Array length [ - ]');
ylabel('Time taken [ in seconds ]');
grid off;
% d.
xlog = log(x);
tlog = log(y);
coefficients = polyfit(xlog,tlog,1) % which yields [ 0.9874 -9.8979 ]
% Given these coefficients with the polyfit function, the line of best
% fit can be represented by the equation --
% y = 0.9874x - 9.8979 where x is the logarithm of the array length
% and y is the logarithm of the time taken
% e. Polyval may also further be used to find fitted data when given the
% coefficients and the logarithm of 1000000000, which equals 9, as an
% input.
seconds = 10.^polyval(coefficients, 9); % which yields the total seconds
% needed to calculate the logarithm of 1000000000, though this must
% be expressed in hours.
time = seconds./3600 % yields a very small time that doesn't make sense!
  3 comentarios
Rik
Rik el 11 de Mzo. de 2020
Why do you delete your question? It is very rude to do that. And as you see, it isn't very effective either. Please don't give people more work trying to restore the original text.
If you want private consultation: hire a consultant.
Rena Berman
Rena Berman el 14 de Mayo de 2020
(Answers Dev) Restored edit

Iniciar sesión para comentar.

Respuesta aceptada

Matt J
Matt J el 10 de Mzo. de 2020
Editada: Matt J el 10 de Mzo. de 2020
xlog = log10(x);
tlog = log10(y);
  2 comentarios
Jon
Jon el 10 de Mzo. de 2020
Sorry Matt, I didn't refresh my view, and so I didn't see that you had already provided the same answer
Matt J
Matt J el 10 de Mzo. de 2020
No problem... You might have gotten there first anyway.

Iniciar sesión para comentar.

Más respuestas (1)

Jon
Jon el 10 de Mzo. de 2020
Editada: Jon el 10 de Mzo. de 2020
One problem may be that you think that log(x) gives log to the base 10 of x but it is the natural log.
So use instead
xlog = log10(x);
tlog = log10(y);
  2 comentarios
Ashley Sullivan
Ashley Sullivan el 10 de Mzo. de 2020
Would correcting it to this solve my problem?
figure(6)
x = [ 1000, 10000, 100000, 1000000]; % represents the tested arrays
y = [ 0.076243, 0.251277, 3.053055, 64.876395]; % represents the time taken
loglog(x,y,'bo--');
title('Time Needed to Calculate the Prime Numbers in an Array');
xlabel('Array length [ - ]');
ylabel('Time taken [ in seconds ]');
grid off;
% d.
xlog = log10(x);
tlog = log10(y);
coefficients = polyfit(xlog,tlog,1) % which yields [ 0.9874 -4.2986 ]
% Given these coefficients with the polyfit function, the line of best
% fit can be represented by the equation --
% y = 0.9874x - 4.2986 where x is the logarithm of the array length
% and y is the logarithm of the time taken
% e. Polyval may also further be used to find fitted data when given the
% coefficients and the logarithm of 1000000000, which equals 9, as an
% input.
seconds = 10.^polyval(coefficients, 9); % which yields the total seconds
% needed to calculate the logarithm of 1000000000, though this must
% be expressed in hours.
time = seconds./3600 % yields 10.7622 hours
Ashley Sullivan
Ashley Sullivan el 10 de Mzo. de 2020
Nevermind you edited it! Thanks!

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by