第12週李昀諠教你金融計算

躉繳第1期第2期第3期

注意,包含首期躉繳的現金流量都大於0。

輸出:

報酬率:

淨現值:

迴圈次數:

李昀諠向劉任昌學程式設計學程式設計

李昀諠向劉任昌學程式設計學程式設計

<head>
<style>
h1 {
  margin: 0;
  padding: 20px 0;    
  color: #00539F;
  text-shadow: 2px 2px 1px yellow;}
.Takming {
  border: 20px outset red;
  background-color: pink;
  color: white;
  text-align: center;}
.pmt {
  width: 60pt;
  height: 20pt;
  background-color: coral;
  color: white;
  text-align: right;}
</style>
</head>
<body>
<table border="1">
<tr align="center"><td>躉繳</td><td>第1期</td><td>第2期</td><td>第3期</td></tr>
<tr><td><input class="pmt" type="number" /></td>
   <td><input class="pmt" type="number" /></td>
   <td><input class="pmt" type="number" /></td>
   <td><input class="pmt" type="number" /></td></tr>
</table><br />
注意,包含首期躉繳的現金流量都大於0。
<p><input onclick="IRR()" type="button" value="計算內部報酬率" />輸出:</p> <!-- 按下 button 去執行 JavaScript IRR( )-->
<p>報酬率:<b id="irr"></b></p> 
<p>淨現值:<b id="npv"></b></p> 
<p>迴圈次數:<b id="loopNumber"></b></p> 
</body>
  <script>
const period=4;
const maxerror = 0.0000001; //要求的精確度
var payment = new Array(period);
function IRR()
{
  var a = 0;
  var b = 1;
  var c;
  var f;
  var gap=10;
  var loopNumber = 0;
  for (var i=0; i<period; i++)
    payment[i]=document.getElementsByClassName("pmt")[i].value;
  f = npv(a);
  if ( f==0 )
      document.getElementById("irr").innerHTML = "內部報酬率等於0.";
  else if ( f<0 )
    document.getElementById("irr").innerHTML = "內部報酬率小於0.";
  else
   { while ( gap > maxerror && Math.abs(f) > maxerror && loopNumber < 100)
     { 
      loopNumber++;
      c = (a+b)/2;
      f = npv(c);
      if ( Math.abs(f) > maxerror && gap > maxerror)
     { if ( f>0 )
        a = c;
       else
        b = c;
       gap = b-a;
      }
    }  //迴圈while結束
   }
  document.getElementById("irr").innerHTML= c;
  document.getElementById("npv").innerHTML= f;
  document.getElementById("loopNumber").innerHTML = loopNumber;
var npv=function(rate)
  {   
    var y= -payment[0]; 
    for (var j=1; j<period; j++)
      y = y + payment[j]/(1+rate)**j;
    return y;
  }
</script>
<h1>李昀諠向劉任昌學程式設計學程式設計</h1>
<h1 class="Takming">李昀諠向劉任昌學程式設計學程式設計</h1><div><div>&lt;head&gt;</div><div>&lt;style&gt;</div><div>.Takming {</div><div>&nbsp; border: 20px outset red;</div><div>&nbsp; background-color: pink;</div><div>&nbsp; color: white;</div><div>&nbsp; text-align: center;}</div><div>.pmt {</div><div>&nbsp; width: 60pt;</div><div>&nbsp; height: 20pt;</div><div>&nbsp; background-color: coral;</div><div>&nbsp; color: white;</div><div>&nbsp; text-align: right;}</div><div>&lt;/style&gt;</div><div>&lt;/head&gt;</div><div>&lt;body&gt;</div><div>&lt;table border="1"&gt;</div><div>&lt;tr align="center"&gt;&lt;td&gt;躉繳&lt;/td&gt;&lt;td&gt;第1期&lt;/td&gt;&lt;td&gt;第2期&lt;/td&gt;&lt;td&gt;第3期&lt;/td&gt;&lt;/tr&gt;</div><div>&lt;tr&gt;&lt;td&gt;&lt;input class="pmt" type="number" /&gt;&lt;/td&gt;</div><div>&nbsp; &nbsp;&lt;td&gt;&lt;input class="pmt" type="number" /&gt;&lt;/td&gt;</div><div>&nbsp; &nbsp;&lt;td&gt;&lt;input class="pmt" type="number" /&gt;&lt;/td&gt;</div><div>&nbsp; &nbsp;&lt;td&gt;&lt;input class="pmt" type="number" /&gt;&lt;/td&gt;&lt;/tr&gt;</div><div>&lt;/table&gt;&lt;br /&gt;</div><div>注意,包含首期躉繳的現金流量都大於0。</div><div>&lt;p&gt;&lt;input onclick="IRR()" type="button" value="計算內部報酬率" /&gt;輸出:&lt;/p&gt; &lt;!-- 按下 button 去執行 JavaScript IRR( )--&gt;</div><div>&lt;p&gt;報酬率:&lt;b id="irr"&gt;&lt;/b&gt;&lt;/p&gt;&nbsp;</div><div>&lt;p&gt;淨現值:&lt;b id="npv"&gt;&lt;/b&gt;&lt;/p&gt;&nbsp;</div><div>&lt;p&gt;迴圈次數:&lt;b id="loopNumber"&gt;&lt;/b&gt;&lt;/p&gt;&nbsp;</div><div>&lt;/body&gt;</div><div>&nbsp; &lt;script&gt;</div><div>const period=4;</div><div>const maxerror = 0.0000001; //要求的精確度</div><div>var payment = new Array(period);</div><div>function IRR()</div><div>{</div><div>&nbsp; var a = 0;</div><div>&nbsp; var b = 1;</div><div>&nbsp; var c;</div><div>&nbsp; var f;</div><div>&nbsp; var gap=10;</div><div>&nbsp; var loopNumber = 0;</div><div>&nbsp; for (var i=0; i&lt;period; i++)</div><div>&nbsp; &nbsp; payment[i]=document.getElementsByClassName("pmt")[i].value;</div><div>&nbsp; f = npv(a);</div><div>&nbsp; if ( f==0 )</div><div>&nbsp; &nbsp; &nbsp; document.getElementById("irr").innerHTML = "內部報酬率等於0.";</div><div>&nbsp; else if ( f&lt;0 )</div><div>&nbsp; &nbsp; document.getElementById("irr").innerHTML = "內部報酬率小於0.";</div><div>&nbsp; else</div><div>&nbsp; &nbsp;{ while ( gap &gt; maxerror &amp;&amp; Math.abs(f) &gt; maxerror &amp;&amp; loopNumber &lt; 100)</div><div>&nbsp; &nbsp; &nbsp;{&nbsp;</div><div>&nbsp; &nbsp; &nbsp; loopNumber++;</div><div>&nbsp; &nbsp; &nbsp; c = (a+b)/2;</div><div>&nbsp; &nbsp; &nbsp; f = npv(c);</div><div>&nbsp; &nbsp; &nbsp; if ( Math.abs(f) &gt; maxerror &amp;&amp; gap &gt; maxerror)</div><div>&nbsp; &nbsp; &nbsp;{ if ( f&gt;0 )</div><div>&nbsp; &nbsp; &nbsp; &nbsp; a = c;</div><div>&nbsp; &nbsp; &nbsp; &nbsp;else</div><div>&nbsp; &nbsp; &nbsp; &nbsp; b = c;</div><div>&nbsp; &nbsp; &nbsp; &nbsp;gap = b-a;</div><div>&nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; }&nbsp; //迴圈while結束</div><div>&nbsp; &nbsp;}</div><div>&nbsp; document.getElementById("irr").innerHTML= c;</div><div>&nbsp; document.getElementById("npv").innerHTML= f;</div><div>&nbsp; document.getElementById("loopNumber").innerHTML = loopNumber;</div><div>}&nbsp;</div><div>var npv=function(rate)</div><div>&nbsp; {&nbsp; &nbsp;</div><div>&nbsp; &nbsp; var y= -payment[0];&nbsp;</div><div>&nbsp; &nbsp; for (var j=1; j&lt;period; j++)</div><div>&nbsp; &nbsp; &nbsp; y = y + payment[j]/(1+rate)**j;</div><div>&nbsp; &nbsp; return y;</div><div>&nbsp; }</div><div>&lt;/script&gt;</div><div>&lt;h1 class="Takming"&gt;李昀諠學程式設計&lt;/h1&gt;</div></div>

留言

這個網誌中的熱門文章

第15週HTML+CSS+JavaScript Word轉pdf

李昀諠期末考UNICODE建立對照表

第16週將WORD+EXCEL貼上網頁