Matlab-如何将1/x最优值的1到1E10系列求和(几分钟)-Matlab变慢 [英] Matlab - How to sum 1 to 1E10 serie of 1/x optimaly (in a few minutes) - matlab slows down

查看:263
本文介绍了Matlab-如何将1/x最优值的1到1E10系列求和(几分钟)-Matlab变慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对matlab有问题.我需要将1/x的数学序列从1到1E10求和.我在Matlab中有一些包含循环的代码-循环的第一步很快就可以了),但是在循环的第二步中它变慢了,Matlab几乎冻结了,所以我无法在适当的时间进行计算.

I have a problem with matlab. I need to sum math serie 1/x from 1 to 1E10. I have some code in Matlab which contains loop - first loop step is ok very fast), but on second step in loop it slows down and Matlab is almost freezed, so I cannot calculate this in appropriate time.

您能帮我吗?

对于较小的范围,它可以正常工作(例如1E06),但是我需要计算整个范围.我试图将其分离到较小的范围,但是仍然存在循环,并且matlab的速度非常慢.

For smaller range it works OK (for example 1E06), but I need to calculate for the whole range. I have tried to separate to smaller range, but there is still loop and matlab is very slowed.

这看起来像是matlab和for循环的问题,速度变慢了.在第一个循环步骤后,RAM已满,但在第二个循环步骤中,RAM仍已满,因此速度变慢.我不知道为什么Matlab不释放RAM.

It looks like a problem with matlab and for loop, which slows down. After first loop step the RAM is full but for the second loop step the RAM is still full so it slows down. I don´t know why the Matlab does not free the RAM.

谢谢您的帮助!

弗拉基米尔

推荐答案

您要获取第1e10个调和数.在符号工具箱中,有一个函数叫做 harmonic ,而且非常快:

You want to obtain the 1e10-th harmonic number. In the Symbolic Toolbox there is a function for that, called harmonic, and it's very fast:

>> format long %// to see more decimals
>> n = 1e10;
>> harmonic(n)
ans =
  23.603066594891992

之所以如此之快,是因为harmonic函数利用了关系在谐波数之间, Euler-Mascheroni常数 digamma函数:

The reason why it's so fast is that the harmonic function exploits the relationship between harmonic numbers, the Euler-Mascheroni constant and the digamma function:


其中"psi"是数字函数, H n n 是第 n 次谐波数,"gamma"是"是Euler-Mascheroni常数.所以你也可以使用


where "psi" is the digamma function, Hn is the n-th harmonic number, and "gamma" is the Euler-Mascheroni constant. So you could also use

>> n = 1e10;
>> vpa(psi(n+1) + eulergamma)
ans =
    23.603066594891987434787570068504

如果没有符号工具箱,仍然可以:

If you don't have the Symbolic Toolbox, you can still do:

>> g = 0.5772156649015328606065120900824; %// Euler-Mascheroni constant
>> n = 1e10;
>> psi(n+1) + g
ans =
  23.603066594891988

这篇关于Matlab-如何将1/x最优值的1到1E10系列求和(几分钟)-Matlab变慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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