循环这些迭代

发布于 2024-09-18 09:12:17 字数 2105 浏览 3 评论 0 原文

我正在尝试循环下面的迭代,代码在下面,我也会尝试将其分解。

<?php

    function intr($LoanRequired, $TermMonths, $MonthlyPayment, $rate) {
     $intr= $LoanRequired * pow((1 + $rate), $TermMonths) + $MonthlyPayment * ((pow((1 + $rate), $TermMonths) - 1) / $rate);
     return $intr;
    }
    $x0=0.008;
    $x1=0.025;
    $LoanRequired=20000;
    $TermMonths=120;
    $MonthlyPayment=-271.09;
    $x2=$x0-(((intr($LoanRequired,$TermMonths,$MonthlyPayment,$x0))*($x0-$x1))/(intr($LoanRequired,$TermMonths,$MonthlyPayment,$x0)-intr($LoanRequired,$TermMonths,$MonthlyPayment,$x1)));
    print_r($x2);
    $intr = $LoanRequired * pow((1 + $x2), $TermMonths) + $MonthlyPayment * ((pow((1 + $x2), $TermMonths) - 1) / $x2);
    while( $intr>=-1 || $intr<=1 ) {

    return ;
    }
    ?>

$x0=0.008; 这不会改变,$x0 是第一个猜测 $x1=0.025; 这不会改变 $x1 是第二个猜测

$LoanRequired=20000;
$TermMonths=120;
$MonthlyPayment=-271.09;

该函数中来自表单的所有变量(所有这些都将根据用户输入的内容而改变)

function intr($LoanRequired, $TermMonths, $MonthlyPayment, $rate) {
         $intr= $LoanRequired * pow((1 + $rate), $TermMonths) + $MonthlyPayment * ((pow((1 + $rate), $TermMonths) - 1) / $rate);
         return $intr;

$rate 变为首先是 $x0,然后是 $x1 和 $x2 等。

$intr = $LoanRequired * pow((1 + $x0), $TermMonths) + $MonthlyPayment * ((pow((1 + $x0), $TermMonths) - 1) / $x0);

首先需要输入上面的公式(首先是 $x0,然后是 $x1,因为它们是两个静态的)如果结果为 <1 或 >-1(

如果前两个静态) ,则整个过程停止猜测不符合执行此操作所需的标准

$x2=$x0-(((intr($LoanRequired,$TermMonths,$MonthlyPayment,$x0))*($x0-$x1))/( intr($LoanRequired,$TermMonths,$MonthlyPayment,$x0)-intr($LoanRequired,$TermMonths,$MonthlyPayment,$x1))); 在循环中。 在这种情况下 $x2 将 =0.0082002592938519 然后将其放入 $intr = $LoanRequired * pow((1 + $x0), $TermMonths) + $MonthlyPayment * ((pow((1 + $x0), $TermMonths) - 1) / $x0); 再次,如果不满足条件,则继续循环,直到满足 $x3、$x4 等条件,

之后需要输入此公式 $apr=((pow(($x0+1), 12))-1)*100; ($x0 显然会更改为 $x1、$x2、$x3 等,具体取决于哪个值是 <1 和 >-1。

我试图把这个进入 while 循环(或任何与此相关的循环),但有一些困难。

I am trying to loop the below iteration, the code is below and i will try and break it down aswell.

<?php

    function intr($LoanRequired, $TermMonths, $MonthlyPayment, $rate) {
     $intr= $LoanRequired * pow((1 + $rate), $TermMonths) + $MonthlyPayment * ((pow((1 + $rate), $TermMonths) - 1) / $rate);
     return $intr;
    }
    $x0=0.008;
    $x1=0.025;
    $LoanRequired=20000;
    $TermMonths=120;
    $MonthlyPayment=-271.09;
    $x2=$x0-(((intr($LoanRequired,$TermMonths,$MonthlyPayment,$x0))*($x0-$x1))/(intr($LoanRequired,$TermMonths,$MonthlyPayment,$x0)-intr($LoanRequired,$TermMonths,$MonthlyPayment,$x1)));
    print_r($x2);
    $intr = $LoanRequired * pow((1 + $x2), $TermMonths) + $MonthlyPayment * ((pow((1 + $x2), $TermMonths) - 1) / $x2);
    while( $intr>=-1 || $intr<=1 ) {

    return ;
    }
    ?>

$x0=0.008; this doesn't change, $x0 is the first guess
$x1=0.025; this doesn't change $x1 is the second guess

$LoanRequired=20000;
$TermMonths=120;
$MonthlyPayment=-271.09;

All variables that will come from a form (all of these will change depending on what the user inputs)

function intr($LoanRequired, $TermMonths, $MonthlyPayment, $rate) {
         $intr= $LoanRequired * pow((1 + $rate), $TermMonths) + $MonthlyPayment * ((pow((1 + $rate), $TermMonths) - 1) / $rate);
         return $intr;

in this function $rate becomes $x0 at first, then $x1 and $x2 etc.

$intr = $LoanRequired * pow((1 + $x0), $TermMonths) + $MonthlyPayment * ((pow((1 + $x0), $TermMonths) - 1) / $x0);

first it needs to go into this formula above, ($x0 first and then $x1 as they are the two static ones) the whole process stops if the result turns out to be <1 or >-1

if the first two static guesses do not meet the criteria it then needs to do this

$x2=$x0-(((intr($LoanRequired,$TermMonths,$MonthlyPayment,$x0))*($x0-$x1))/(intr($LoanRequired,$TermMonths,$MonthlyPayment,$x0)-intr($LoanRequired,$TermMonths,$MonthlyPayment,$x1))); in a loop.
In this case $x2 will =0.0082002592938519 which will then be put into
$intr = $LoanRequired * pow((1 + $x0), $TermMonths) + $MonthlyPayment * ((pow((1 + $x0), $TermMonths) - 1) / $x0); again and if it does not meet the criteria it continues the loop until it does with $x3, $x4 etc

after that it needs to go into this formula $apr=((pow(($x0+1),12))-1)*100; (the $x0 would obviously change to $x1, $x2, $x3 etc depending on which value was <1 and >-1.

I am trying to put this into a while loop,(or any loop for that matter) but having some difficulty.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风筝在阴天搁浅。 2024-09-25 09:12:17

让我们从设置开始

$i0 = intr($LoanRequired,$TermMonths,$MonthlyPayment,$x0);
$i1 = intr($LoanRequired,$TermMonths,$MonthlyPayment,$x1);

然后 x2 变为

$x2=$x0-($i0*($x0-$x1)/($i0-$i1));

现在

$i2 = intr($LoanRequired,$TermMonths,$MonthlyPayment,$x2);

,如果有下一步,则不再需要当前的 $x0/$i0 。当前的 $x1/$i1 变成 $x0/$i0 , $x2/$i2 变成 $x1/$i1

$x0 = $x1; $i0=$i1;
$x1 = $x2; $i1=$i2;

你这样做 while ($i2 <= -1 || $i2 >= 1) ,即while (abs($i2) >= 1)

<?php
function intr($LoanRequired, $TermMonths, $MonthlyPayment, $rate) {
  $intr= $LoanRequired * pow((1 + $rate), $TermMonths) + $MonthlyPayment * ((pow((1 + $rate), $TermMonths) - 1) / $rate);
  return $intr;
}

$LoanRequired=20000;
$TermMonths=120;
$MonthlyPayment=-271.09;
$x0=0.008;
$x1=0.025;
$i0 = intr($LoanRequired,$TermMonths,$MonthlyPayment,$x0);
$i1 = intr($LoanRequired,$TermMonths,$MonthlyPayment,$x1);

echo "f($x0)=", $i0, "\n";
echo "f($x1)=", $i1, "\n";

$dbgcnt = 0;
do {
  if ( 100 < ++$dbgcnt ) {
    die('bailout');
  }

  $x2=$x0-($i0*($x0-$x1)/($i0-$i1));
  $i2 = intr($LoanRequired,$TermMonths,$MonthlyPayment,$x2);
  echo "f($x2)=", $i2, "\n";

  $x0 = $x1; $i0=$i1;
  $x1 = $x2; $i1=$i2;
} while (abs($i2) > 1);

echo "----\nx=$x2";

打印

f(0.008)=-2242.1586767476
f(0.025)=188094.563138
f(0.0082002592938519)=-1736.2922063647
f(0.0083539185016984)=-1336.4282682791
f(0.0088674794645362)=76.76523796426
f(0.0088395826270916)=-3.1241157534314
f(0.0088406735477872)=-0.0068921130805393
----
x=0.0088406735477872

但是如果这有任何重要性,您应该在 https://mathoverflow.net/ 此方法是否可以安全地用于 IEEE 浮点数(我的猜测是不是)。

Let's start with setting

$i0 = intr($LoanRequired,$TermMonths,$MonthlyPayment,$x0);
$i1 = intr($LoanRequired,$TermMonths,$MonthlyPayment,$x1);

Then x2 becomes

$x2=$x0-($i0*($x0-$x1)/($i0-$i1));

and

$i2 = intr($LoanRequired,$TermMonths,$MonthlyPayment,$x2);

Now, if there is a next step you don't need the current $x0/$i0 any more. The current $x1/$i1 become $x0/$i0 and $x2/$i2 become $x1/$i1

$x0 = $x1; $i0=$i1;
$x1 = $x2; $i1=$i2;

And you do that while ($i2 <= -1 || $i2 >= 1), i.e. while ( abs($i2) >= 1 )

<?php
function intr($LoanRequired, $TermMonths, $MonthlyPayment, $rate) {
  $intr= $LoanRequired * pow((1 + $rate), $TermMonths) + $MonthlyPayment * ((pow((1 + $rate), $TermMonths) - 1) / $rate);
  return $intr;
}

$LoanRequired=20000;
$TermMonths=120;
$MonthlyPayment=-271.09;
$x0=0.008;
$x1=0.025;
$i0 = intr($LoanRequired,$TermMonths,$MonthlyPayment,$x0);
$i1 = intr($LoanRequired,$TermMonths,$MonthlyPayment,$x1);

echo "f($x0)=", $i0, "\n";
echo "f($x1)=", $i1, "\n";

$dbgcnt = 0;
do {
  if ( 100 < ++$dbgcnt ) {
    die('bailout');
  }

  $x2=$x0-($i0*($x0-$x1)/($i0-$i1));
  $i2 = intr($LoanRequired,$TermMonths,$MonthlyPayment,$x2);
  echo "f($x2)=", $i2, "\n";

  $x0 = $x1; $i0=$i1;
  $x1 = $x2; $i1=$i2;
} while (abs($i2) > 1);

echo "----\nx=$x2";

prints

f(0.008)=-2242.1586767476
f(0.025)=188094.563138
f(0.0082002592938519)=-1736.2922063647
f(0.0083539185016984)=-1336.4282682791
f(0.0088674794645362)=76.76523796426
f(0.0088395826270916)=-3.1241157534314
f(0.0088406735477872)=-0.0068921130805393
----
x=0.0088406735477872

But if this is of any importance you should ask over at https://mathoverflow.net/ whether this method is safe to use with IEEE floats (my guess is it's not).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文