Matlab中级数求和的性能分析 [英] Performance analysis of series summation in Matlab

查看:145
本文介绍了Matlab中级数求和的性能分析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Matlab程序以通过级数求和来计算pi

I'm writing a Matlab program to compute pi through the summation of series

A = Sum of a_i from i=1 to N

其中

pi/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 ...

要通过级数求和来计算pi,建议的方法是设置

To compute pi through the series summation, the suggested approach is to set

a_i = (-1)^(i+1)/(2i-1)

为此,我在下面编写了程序

In order to do this, I wrote the program below

n=100;
f=[];
for jj=1:n
    ii=1:jj;
    f=[f 4*sum( ((-1).^(ii+1))./(2.*ii-1)  )];
end;
hold on
plot(f)
title('Computing of \pi using a finite sum')
xlabel('Number of summations') 
ylabel('Estimated value of \pi') 
plot(1:size(f,2),ones(size(f))*pi)

此程序显示,在N=80附近,序列近似值有些精确.

This program shows that the series approximation is somewhat accurate near N=80.

我现在正在尝试调整程序,以使y-axis displays total calculation time T_Nx-axis displays N (the number of summations).随着N的增加,总的计算时间T_N应该增加.理想情况下,我希望图形显示的内容接近T(N)N

I am now attempting to adjust my program so that the y-axis displays total calculation time T_N and the x-axis displays N (the number of summations). The total calculation time T_N should increase as N increases. Ideally, I am looking to have the graph display something close to a linear relationship between T(N) and N

为此,我对原始程序进行了如下调整

To do this, I have adjusted my original program as follows

n=100;
f=[];
tic
for jj=1:n
    ii=1:jj;
    f=[f 4*sum( ((-1).^(ii+1))./(2.*ii-1)  )];
end;
hold on
plot(f)
title('Time it takes to sum \pi using N summations')
xlabel('Number of summations (N)') 
ylabel('Total Time (T_N)') 
plot(1:size(f,2),toc)
slope = polyfit(1:size(f,2),toc,1);

这看起来是错误的.我一定不正确地应用了Matlab中的内置计时功能(tic和toc).因此,我将分析我的代码并提出两个问题-

This looks wrong. I must have incorrectly applied the built-in timing functions in Matlab (tic and toc). So, I am going to analyze my code and ask two questions -

  1. 如何调整上面的代码,以便y轴正确显示每个总和N的总计算时间?看来我在plot(1:size(f,2),toc)中做错了.

获得y-axis以显示正确的total calculation time (T_N)之后,我应该能够使用polyfit命令查找T(N)/N的斜率.这将给我T(N) and N之间的线性关系.然后,我可以使用slope = polyfit(1:size(f,2),toc,1)的值进行计算

After I get the y-axis to display the correct total calculation time (T_N), I should be able to use the polyfit command to find the slope of T(N)/N. This will give me a linear relationship between T(N) and N. I could then use the value of slope = polyfit(1:size(f,2),toc,1) to compute

t_N = a + b*N

其中,t_N是针对每个N值计算的,而b是通过polyfit命令计算的斜率.

where t_N is computed for every value of N and b is the slope calculated through the polyfit command.

我认为正确显示y-axis并正确引用polyfit命令后,应该能够找到values of a and b.

I think that I should be able to find the values of a and b after I correctly display the y-axis and correctly reference the polyfit command.

推荐答案

如果我正确理解了您的问题,我认为这里有两个不同的问题.首先,绘制结果函数,然后绘制经过时间,该时间比pi小几个数量级:

If I understand your problem correctly, I think there are two different issues here. First, you plot your result function then the elapsed time which is several orders of magnitude smaller than pi:

 hold on
 plot(f)  % <---- Comment me out!
 ...stuff...
 plot(1:size(f,2),toc)

第二,您需要存储循环每次通过的执行时间:

Secondly, you need to store the execution time of each pass of the loop:

n=100;
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(f)
title('Time it takes to sum \pi using N summations')
xlabel('Number of summations (N)') 
ylabel('Total Time (T_N)') 
plot(1:n,telapsed)
slope = polyfit(1:n,telapsed,1);

请注意新的polyfit表达式的执行时间斜率.有帮助吗?

Note the new polyfit expression for slope of the execution time. Does that help?

这篇关于Matlab中级数求和的性能分析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆