如何实现自然对数与下继续几分之几? [英] How to implement natural logarithm with continued fraction in C?
问题描述
在这里,我有一个小问题。从这个公式创造的东西:
这是我应得的,但它不工作。永邦,我真不明白它应该如何工作的。我试图code用一些不好的说明。 N是迭代和分数的零件数量。我认为这会导致某种方式来递归,但不知道怎么办。
感谢您的帮助。
双contFragLog(双Z,INT N)
{
双CF = 2 * Z;
双A,B;
的for(int i = N; I> = 1;我 - )
{
A =平方(I - 2)*平方(Z);
B = 1 + I - 2;
比照=一个/(二 - 立方英尺);
}
返回(1 + CF)/(1 - 立方英尺);
}
中央循环就会混乱。重做。递归不需要任何。只要首先计算最深的术语和自己的方式工作了。
双contFragLog(双Z,INT N){
双ZZ = Z * Z;
双CF = 1.0; //重要的,这是不为0
的for(int i = N; I> = 1;我 - ){
CF =(2 * I-1) - I * I * ZZ / CF;
}
返回2 * Z / CF;
}
无效testln(双Z){
双Y =日志((1 + Z)/(1-Z));
双Y2 = contFragLog(Z,8);
的printf(%E%E%E \ N,Z,Y,Y2);
}
诠释的main(){
testln(0.2);
testln(0.5);
testln(0.8);
返回0;
}
输出
2.000000e-01 4.054651e-01 4.054651e-01
5.000000e-01 1.098612e + 00 1.098612e + 00
8.000000e-01 2.197225e + 00 2.196987e + 00
根据提示按@MicroVirus,我发现双CF = 1.88 * N - 0.95;
工作优于双CF = 1.0;
。随着越来越多的术语,所用的值使差小,但一个良好的初始 CF
需要一个很好的答案少来说,特别是对于 | Z |
接近0.5。更多的工作可以在这里完成,我研究了 0℃ z,其中= 0.5
。 的@MicroVirus建议2 * N + 1
可能接近我的建议,由于偏离情况的一个什么 N $ C $的C>是
这是根据逆运算,并指出的CF [N]
的值 N
增加。我很惊讶的种子值没有出现被一些不错的整方程。
Here I have a little problem. Create something from this formula:
This is what I have, but it doesn't work. Franky, I really don't understand how it should work.. I tried to code it with some bad instructions. N is number of iteration and parts of fraction. I think it leads somehow to recursion but don't know how.
Thanks for any help.
double contFragLog(double z, int n)
{
double cf = 2 * z;
double a, b;
for(int i = n; i >= 1; i--)
{
a = sq(i - 2) * sq(z);
b = i + i - 2;
cf = a / (b - cf);
}
return (1 + cf) / (1 - cf);
}
The central loop is messed. Reworked. Recursion not needed either. Just compute the deepest term first and work your way out.
double contFragLog(double z, int n) {
double zz = z*z;
double cf = 1.0; // Important this is not 0
for (int i = n; i >= 1; i--) {
cf = (2*i -1) - i*i*zz/cf;
}
return 2*z/cf;
}
void testln(double z) {
double y = log((1+z)/(1-z));
double y2 = contFragLog(z, 8);
printf("%e %e %e\n", z, y, y2);
}
int main() {
testln(0.2);
testln(0.5);
testln(0.8);
return 0;
}
Output
2.000000e-01 4.054651e-01 4.054651e-01
5.000000e-01 1.098612e+00 1.098612e+00
8.000000e-01 2.197225e+00 2.196987e+00
[Edit]
As prompted by @MicroVirus, I found double cf = 1.88*n - 0.95;
to work better than double cf = 1.0;
. As more terms are used, the value used makes less difference, yet a good initial cf
requires fewer terms for a good answer, especially for |z|
near 0.5. More work could be done here as I studied 0 < z <= 0.5
. @MicroVirus suggestion of 2*n+1
may be close to my suggestion due to an off-by-one of what n
is.
This is based on reverse computing and noting the value of CF[n]
as n
increased. I was surprised the "seed" value did not appear to be some nice integer equation.
这篇关于如何实现自然对数与下继续几分之几?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!