issue with polyval and polyfit measuring time

4 views (last 30 days)
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 Comments
Rik
Rik on 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.

Sign in to comment.

Accepted Answer

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

Sign in to comment.

More Answers (1)

Jon
Jon on 10 Mar 2020
Edited: Jon on 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 Comments
Ashley Sullivan
Ashley Sullivan on 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

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!