# issue with polyval and polyfit measuring time

2 ビュー (過去 30 日間)
Ashley Sullivan 2020 年 3 月 10 日
コメント済み: Rena Berman 2020 年 5 月 14 日
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 件のコメント

Matt J 2020 年 3 月 11 日
Ashley Sullivan's original question, prior to its deletion, was that the following resulted in a computed time that was unexpectedly small. Where is the bug?
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;
xlog = log(x);
tlog = log(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
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.
Rena Berman 2020 年 5 月 14 日

サインインしてコメントする。

### 採用された回答

Matt J 2020 年 3 月 10 日

xlog = log10(x);
tlog = log10(y);

#### 2 件のコメント

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 2020 年 3 月 10 日
No problem... You might have gotten there first anyway.

サインインしてコメントする。

### その他の回答 (1 件)

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.
xlog = log10(x);
tlog = log10(y);

#### 2 件のコメント

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
Ashley Sullivan 2020 年 3 月 10 日
Nevermind you edited it! Thanks!

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!