issue with polyval and polyfit measuring time

1 visualizzazione (ultimi 30 giorni)
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 Commenti
Rik
Rik il 11 Mar 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 il 14 Mag 2020
(Answers Dev) Restored edit

Accedi per commentare.

Risposta accettata

Matt J
Matt J il 10 Mar 2020
Modificato: Matt J il 10 Mar 2020
xlog = log10(x);
tlog = log10(y);
  2 Commenti
Jon
Jon il 10 Mar 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 il 10 Mar 2020
No problem... You might have gotten there first anyway.

Accedi per commentare.

Più risposte (1)

Jon
Jon il 10 Mar 2020
Modificato: Jon il 10 Mar 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 Commenti
Ashley Sullivan
Ashley Sullivan il 10 Mar 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 il 10 Mar 2020
Nevermind you edited it! Thanks!

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by