issue with polyval and polyfit measuring time

4 次查看(过去 30 天)
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 个评论
Rik
Rik 2020-3-11
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.

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2020-3-10
编辑:Matt J 2020-3-10
xlog = log10(x);
tlog = log10(y);
  2 个评论
Jon
Jon 2020-3-10
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 2020-3-10
No problem... You might have gotten there first anyway.

请先登录,再进行评论。

更多回答(1 个)

Jon
Jon 2020-3-10
编辑:Jon 2020-3-10
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 个评论
Ashley Sullivan
Ashley Sullivan 2020-3-10
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by