Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class RPSN
- {
- public $A, $B, $n;
- public function setLoan($A)
- {
- $this->A = $A;
- }
- public function setRepayment($B)
- {
- $this->B = $B;
- }
- public function setNumRepayment($n)
- {
- $this->n = $n;
- }
- public function getValue($in)
- {
- $A = $this->A;
- $B = $this->B;
- $n = $this->n;
- return $A - ($B/pow(1+$in, 1/12)*((pow(1+$in, -1*$n/12)-1)/(pow(1+$in, -1/12)-1)));
- }
- public function calculate()
- {
- $l = 0.000001;
- $r = 2;
- $cOld = -1;
- for ($i = 0; $i <= 50; $i++)
- {
- $c = ($l + $r)/2;
- $lv = $this->getValue($l);
- $rv = $this->getValue($r);
- $cv = $this->getValue($c);
- if ($lv < 0 && $cv < 0 && $rv > 0)
- {
- $l = $c;
- }
- elseif ($lv < 0 && $cv > 0 && $rv > 0)
- {
- $r = $c;
- }
- else
- {
- throw new Exception("Invalid interval");
- }
- if (round($c, 7) == round($cOld, 7))
- {
- return round($c, 7);
- }
- else
- {
- $cOld = $c;
- }
- }
- throw new Exception("Can't solve");
- }
- }
- $bs = new RPSN;
- $bs->setLoan(50000); // půjčka
- $bs->setRepayment(949); // měsíční splátka
- $bs->setNumRepayment(84); // počet měsíčních splátek
- echo $bs->calculate()*100, " %";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement