[英]Self-defined zeta function never terminates
I am trying to build a program to compare the partial sum of the Riemann Zeta function to the built-in Matlab function zeta(s).我正在尝试构建一个程序来比较黎曼 Zeta 函数的部分和与内置的 Matlab 函数 zeta(s)。 I want the function to output the minimum number of terms to achieve an accuracy of 0.1 percent.
我希望函数输出最少的项数,以达到 0.1% 的准确度。 I thought a while loop would be the best approach, but my program is running so slow;
我认为 while 循环是最好的方法,但我的程序运行得很慢; I have yet to get a result from it.
我还没有得到结果。
function[n] = riemannzeta(s)
error = 1; n = 1; an = 1; S = an;
while error >= 0.1
an = 1/n^s;
S = S + an;
n = n + 1;
z = zeta(s);
error = ((S - z)/z)*100;
end
end
I call it with:我称之为:
riemannzeta(3)
The main problem is that your definition of the zeta function is wrong because you initialize the value of the sum to 1 and then add 1 in the first step.主要问题是你对zeta函数的定义是错误的,因为你在第一步中将sum的值初始化为1然后加1。 You either need to initialize at 0 or start the loop at
1/2^s
.您要么需要在 0 处初始化,要么在
1/2^s
处开始循环。 You also need to take the absolute value of the error.您还需要取误差的绝对值。
Here is the start-at-two version:这是两个开始的版本:
function n = riemannzeta(s)
error = 1; n = 1; an = 1; S = 1;
z = zeta(s);
while error >= 0.001
n = n + 1;
an = 1/n^s;
S = S + an;
error = abs(S - z)/z;
end
end
If I run riemannzeta(3)
I get a value of 20.如果我运行
riemannzeta(3)
我得到的值为 20。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.