Advertisement
Guest User

Untitled

a guest
May 30th, 2013
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.55 KB | None | 0 0
  1. <?php
  2.  
  3. class RPSN
  4. {
  5.     public $A, $B, $n;
  6.  
  7.     public function setLoan($A)
  8.     {
  9.         $this->A = $A;
  10.     }
  11.  
  12.     public function setRepayment($B)
  13.     {
  14.         $this->B = $B;
  15.     }
  16.  
  17.     public function setNumRepayment($n)
  18.     {
  19.         $this->n = $n;
  20.     }
  21.  
  22.     public function getValue($in)
  23.     {
  24.         $A = $this->A;
  25.         $B = $this->B;
  26.         $n = $this->n;
  27.  
  28.         return $A - ($B/pow(1+$in, 1/12)*((pow(1+$in, -1*$n/12)-1)/(pow(1+$in, -1/12)-1)));
  29.     }
  30.    
  31.     public function calculate()
  32.     {
  33.         $l = 0.000001;
  34.         $r = 2;
  35.         $cOld = -1;
  36.         for ($i = 0; $i <= 50; $i++)
  37.         {
  38.             $c = ($l + $r)/2;
  39.             $lv = $this->getValue($l);
  40.             $rv = $this->getValue($r);
  41.             $cv = $this->getValue($c);
  42.  
  43.             if ($lv < 0 && $cv < 0 && $rv > 0)
  44.             {
  45.                 $l = $c;
  46.             }
  47.             elseif ($lv < 0 && $cv > 0 && $rv > 0)
  48.             {
  49.                 $r = $c;
  50.             }
  51.             else
  52.             {
  53.                throw new Exception("Invalid interval");
  54.             }
  55.  
  56.             if (round($c, 7) == round($cOld, 7))
  57.             {
  58.                 return round($c, 7);
  59.             }
  60.             else
  61.             {
  62.                 $cOld = $c;
  63.             }
  64.         }
  65.         throw new Exception("Can't solve");
  66.     }
  67. }
  68.  
  69. $bs = new RPSN;
  70. $bs->setLoan(50000); // půjčka
  71. $bs->setRepayment(949); // měsíční splátka
  72. $bs->setNumRepayment(84); // počet měsíčních splátek
  73. echo $bs->calculate()*100, " %";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement