3

Here I have a little problem. Create something from this formula:

enter image description here

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);
}
5
  • 1
    I didn't check the loop, but the final result calculation is wrong. You don't want to return (1+cf)/(1-cf). The formula computes log((1+z)/(1-z)). So if you want log(x), you have to figure out what value of z gives you x = (1+z)/(1-z), then compute in terms of z (as I assume you've done and the formula shows) and return that result as-is.
    – lurker
    Commented Nov 21, 2015 at 23:51
  • 1
    The loop formula doesn't look right at all. Somehow, you need a z*z in there somewhere, but I don't see it. And you have b = i + i - 2 which is the same as b = 2*(i - 1) so your b is always even (I see a sequence of odd numbers in the formula).
    – lurker
    Commented Nov 21, 2015 at 23:55
  • Your continued fraction doesn't match the one given by Wikipedia. I can't tell for sure if they're actually different, but are you sure your displayed one is correct?
    – MicroVirus
    Commented Nov 22, 2015 at 0:25
  • Yeah, there are apparently more ways to compute natural logarithm with continued fractions:) Commented Nov 22, 2015 at 0:40
  • Earlier today, an almost identical question was posted by user3838673. I answered it, but it was subsequently deleted by the OP. High rep users can see it here. Maybe you're in the same class...
    – Floris
    Commented Nov 24, 2015 at 17:24

2 Answers 2

2

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.

6
  • How did you decide on cf = 1.0 as the start? My first intuition would be 2*n+1, but I don't really have a good reason for it, and I'm really curious what would be the correct start value.
    – MicroVirus
    Commented Nov 22, 2015 at 0:20
  • Recursions not needed, however it is a nice approach to this problem (as it looks like it calculates the result in the same order you read it), the recursive solution is really short if you calculate the first term outside of the recursion. Commented Nov 22, 2015 at 0:22
  • Oh it's really nice. And it works just fine, thanks! I added z = (z - 1) / (z + 1 at the beginning of the function and now its perfect); Commented Nov 22, 2015 at 0:36
  • @MicroVirus True that cf = 1.0; was a simple attempt. 2*n+1 may be better. I will ponder other starting values. Nice to leave something for OP. Commented Nov 22, 2015 at 1:04
  • 1
    Nice to see you trying it out :) Thanks for the response.
    – MicroVirus
    Commented Nov 22, 2015 at 11:38
0

Here's a solution to the problem that does use recursion (if anyone is interested):

#include <math.h>
#include <stdio.h>

/* `i` is the iteration of the recursion and `n` is
   just for testing when we should end. 'zz' is z^2 */
double recursion (double zz, int i, int n) {
  if (!n)
    return 1;

  return 2 * i - 1 - i * i * zz / recursion (zz, i + 1, --n);
}

double contFragLog (double z, int n) {
  return 2 * z / recursion (z * z, 1, n);
}

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;
}

The output is identical to the solution above:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.