我以前问过this问题,该问题引用了对一系列求和以通过公式计算pi的问题
pi/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 ...
在计算中,
A = Sum of a_i from i=1 to N
和
a_i = (-1)^(i+1)/(2i-1)
我正在编写此计算,以分析每N个求和计算pi所需时间的性能。我想了解随着N的增加,T_N和N之间的关系。
T_N = the total elapsed time it takes to do N summations.
N = the number of summations.
我的Matlab程序将T_N绘制为N的函数。在该程序中,我定义了变量以查找以下线性方程的a和b的值(其中a是y轴截距,b是斜率)>
T_N=a + b*N
为此,我在下面编写了以下Matlab程序(初始测试为N = 1000)
clear all;
n=1000;
f=[];
telapsed = zeros(1,n);
tic
for jj=1:n
ii=1:jj;
f=[f 4*sum( ((-1).^(ii+1))./(2.*ii-1) )];
telapsed(jj) = toc;
end
hold on
plot(1:n,telapsed)
title('Time it takes to sum \pi using N summations')
xlabel('Number of summations (N)')
ylabel('Total Time (T_N)')
p = polyfit(1:n,telapsed,1);
slope=p(1) % slope of t_N = a + b*N or b
intercept=p(2) % y-intercept or a
运行该程序将产生以下图形
因此,此代码返回N = 1000的a和b的值。该代码返回
a = slope = 9.3447e-06
b = intercept = 0.0011
所以,在N = 1000时,我发现了
T_n = a + b*N = 9.3447e-06 + 0.0011*N
我现在正在尝试从绘图中调整代码
T_N as a function of N
绘图
(T_N − a)/N as a function of N
为此,我进行了以下调整
clear all;
n=1000;
f=[];
g=[];
telapsed = zeros(1,n);
telapsed2 = zeros(1,n);
tic
% The original for loop computes T_N as a function of N
for jj=1:n
ii=1:jj;
f=[f 4*sum( ((-1).^(ii+1))./(2.*ii-1) )];
telapsed(jj) = toc;
end
% I think that I need to make a new for loop to compute (T_N − a)/N as a function of N
for jj=1:n
ii=1:jj;
g=[g 4*sum( ((-1).^(ii+1))./(2.*ii-1) )];
telapsed2(jj) = toc/jj; % This must be wrong, I think that I need to adjust to compute (T_N − a)/N
end
hold on
plot(1:n,telapsed) % the plot for T_N as a function of N
plot(1:n,telapsed2) % the plot for (T_N − a)/N as a function of N
title('Time it takes to sum \pi using N summations')
xlabel('Number of summations (N)')
ylabel('Total Time (T_N)')
legend('T_N as a function of N','(T_N − a)/N as a function of N')
p = polyfit(1:n,telapsed,1);
slope=p(1) % slope of t_N = a + b*N or b
intercept=p(2) % y-intercept or a
p2 = polyfit(1:n,telapsed2,1);
slope2=p2(1) % Adjusted slope
intercept2=p2(2) % Adjusted y-intercept
产生以下图形
这不是将(T_N − a)/ N绘制为N的函数的正确方法。我在定义
时必须有一个错误for jj=1:n
ii=1:jj;
g=[g 4*sum( ((-1).^(ii+1))./(2.*ii-1) )];
telapsed2(jj) = toc/jj; % This must be wrong, I think that I need to adjust to compute (T_N − a)/N
end
为了计算
(T_N − a)/N as a function of N
我可以对上面的Matlab代码进行调整,以便在该行计算(T_N-a)/ N
telapsed2(jj) = toc/jj;
我不确定如何更正我的代码,以便此行计算
slope = slope of computation per N summations
telapsed(jj)= (toc-slope)/n
我也许可以在f的第一个for循环中添加它,但是我不确定如何添加。
问题:如何调整Matlab代码,以便可以成功地从
更改计算T_N as a function of N
到
(T_N − a)/N as a function of N
答案 0 :(得分:0)
我认为我找到了部分解决方案。我最初编写了以下代码,将T_N绘制为N的函数。
clear all;
n=50;
f=[];
telapsed = zeros(1,n);
tic
for jj=1:n
ii=1:jj;
f=[f 4*sum( ((-1).^(ii+1))./(2.*ii-1) )];
telapsed(jj) = toc;
end
hold on
plot(1:n,telapsed)
title('Time it takes to sum \pi using N summations')
xlabel('Number of summations (N)')
ylabel('Total Time (T_N)')
p = polyfit(1:n,telapsed,1);
slope=p(1) % slope of t_N = a + b*N or b
intercept=p(2) % y-intercept or a
运行该程序将产生以下图形
其中
slope = 1.3802e-05
intercept = 0.0021
所以
T_N = a + b * N = 0.0021 + 1.3802e-05 * N
为了将(T_N − a)/ N绘制为N的函数,我进行了以下更改
clear all;
n=50;
f=[];
telapsed = zeros(1,n);
tsum = zeros(1,n);
tic
for jj=1:n
ii=1:jj;
f=[f 4*sum( ((-1).^(ii+1))./(2.*ii-1) )];
telapsed(jj) = toc;
psum = polyfit(jj,toc,n);
pintercept = psum(2);
tsum(jj) = (telapsed(jj)-pintercept)/jj;
end
hold on
plot(1:n,telapsed) % the plot for T_N as a function of N
plot(1:n,tsum) % the plot for (T_N - a)/N as a function of N
title('Time it takes to sum \pi using N summations')
xlabel('Number of summations (N)')
ylabel('Total Time (T_N)')
legend('T_N as a function of N','(T_N - a)/N as a function of N')
p = polyfit(1:n,telapsed,1);
slope=p(1) % slope of t_N = a + b*N or b
intercept=p(2) % y-intercept or a
产生以下情节
我期望(T_N − a)/ N的值接近一个常数。从上图可以清楚地看到,该值接近y截距。数学会告诉我
T_N = a + b*N => (T_N-a)/N = b
因此,似乎已对
进行了调整for jj=1:n
ii=1:jj;
f=[f 4*sum( ((-1).^(ii+1))./(2.*ii-1) )];
telapsed(jj) = toc;
psum = polyfit(jj,toc,n);
pintercept = psum(2);
tsum(jj) = (telapsed(jj)-pintercept)/jj;
end
是正确的。