0

I have got a calculation written in php, its meant to calculate the total balance (echo $vf;) after 3 months.

The answer I get is 603.00

The answer I should get is 606.02

Somewhere I have gone wrong that it is losing 3 the math, any idea ?

I have been working on this for weeks and am so close, any help will be very much appreciated.

Code

$p = 0;
$i = 0.06;
$c = 12;
$n = 3/12;
$r = 200;

$x = $i / $c;
$y = pow((1 + $x), ($n * $c));

$vf = $p * $y + ($r * ($y - 1) / $x); 
echo $vf;

Sand box if needed https://3v4l.org/FigRr

Thanks

UPDATE

have been informed my math may be wrong, here is the original formula bellow, can anyone see how I have written the php wrong ?

enter image description here

UPDATE

What I am after is to get the compound of 200 after 3 months, when I have used online calculators I get 606. for example http://www.thecalculatorsite.com/finance/calculators/compoundinterestcalculator.php

Update have tried:

$i = 0.06;
$c = 12;
$n = 3;
$r = 200;

    $x = $i / $c;
    $z = $n / 12;
    $y = pow((1 + $x), ($z * $c));

    $vf =  $y + ($r * ($y - 1) / $x);

     echo $vf;

has improved the answer and am getting 604, but still not getting the 606

spazm
  • 4,399
  • 31
  • 30
Beep
  • 2,737
  • 7
  • 36
  • 85
  • Did the math manually and got 603.005 – WheatBeak Mar 08 '16 at 21:36
  • Hmm ok, I will add the formula im trying to convert to PHP to see if the math i have written is wrong – Beep Mar 08 '16 at 21:38
  • there seem to be some inconsistencies with the naming of your variables n and z but it looks to me like you've converted the formula to php properly. – WheatBeak Mar 08 '16 at 21:47
  • I have changed the php a bit, il revert to my original code for you. 2 mins – Beep Mar 08 '16 at 21:50
  • Are your values correct? I tried solving the formula by hand and I get 603.005 – Neobugu Mar 08 '16 at 22:09
  • @Rhopercy the formula in php or the image formula ? – Beep Mar 08 '16 at 22:10
  • @Rhopercy hmm, when I enter the variables into this calculator it seems to give 606 http://www.thecalculatorsite.com/finance/calculators/compoundinterestcalculator.php – Beep Mar 08 '16 at 22:12
  • 2
    Now I understand, php is making $p*$y=0*$y which is equal 0, assuming that values outside the ((1+i/c)^n*c) are ignored the result is aproximating to 605 – Neobugu Mar 08 '16 at 22:23

2 Answers2

0

php is making $p*$y=0*$y which is equal 0, assuming that values outside the ((1+i/c)^n*c) are ignored the result is aproximating to 605

You should make an ifclause to evaluate $p!=0 kinda like this

if($p!=0)
{
//regular formula
$FV = ($p(1+($i/$c)^($n*$c))) + ($R((1+($i/$c)^($n*$c)-1)*($i/$c)))
}else{
//normal formula without $p
$FV = (1+($i/$c)^($n*$c)) + ($R((1+($i/$c)^($n*$c)-1)*($i/$c)))
}
Neobugu
  • 333
  • 6
  • 15
0
<?php

 $p = 200; // Starting amount
    $i = 0.05; // Interest rate
    $c = 12; // compound frequency set to monthly
    $n = 3/12; // Current time invested set to 6 months
    $r = 200; // Monthly investment is 200

    $x = $i / $c;
    $y = pow((1 + $x), ($n * $c));

     $vf = $p * $y + ($r * (1 + $x) * ($y - 1) / $x);



    echo $vf;
Beep
  • 2,737
  • 7
  • 36
  • 85