-1

How to solve nth degree equations in PHP

Example:

1/(1+i)+1/(1+i)2+...1/(1+i)n=k

While k is the constant,I'd like to find value of i.

How can I achieve this in PHP?

pguetschow
  • 5,176
  • 6
  • 32
  • 45
user3797053
  • 497
  • 1
  • 8
  • 18

2 Answers2

2

First of all, your expression on the left is a geometric sum, so you can rewrite it as (using x=1+i)

1/x*(1+...+1/x^(n-1)) = 1/x * (1-1/x^n)/(1-1/x) = (1-x^(-n))/(x-1)

and consequently the equation can be rewritten as

(1 - pow( 1+i, -n))/i = k

Now from the original expression one knows that the left side as a sum of convex monotonically decreasing functions is equally so, thus any of bisection, regula falsi variants or secant method will work sufficiently well.

Use

(1+i)^(-n)=1 - n*i + (n*(n+1))/2*i^2 +...

to get the approximative equation and first approximation

1-(n+1)/2*i = k/n  <=> i = (1-k/n)*2/(n+1)

so that you can start bracketing method with the interval from 0 to twice this i.

Lutz Lehmann
  • 25,219
  • 2
  • 22
  • 51
1

Try something like this....

$n = 5;
$i = 2;
$k = null;

for ($x = 1; $x <= $n; $x++) {
    $k += 1 / pow((1 + $i), $x);
}

echo $k; //Answer --> 0.49794238683128 
Shadab Mehdi
  • 604
  • 10
  • 23