劉任昌期末考Javascript與Python計算內部報酬率IRR

躉繳第1期第2期第3期

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

輸出:

報酬率:

淨現值:

迴圈次數:

劉任昌向項柏綱學習程式設計程式碼如下

<STYLE>
H1{margin: 0; padding: 20px 0; color: #000000; text-shadow: 2PX 2PX yellow;}
.Takming{border: 20px outset red;background-color: green;color: white;}
.pmt{width: 60pt; height: 20pt;background-color: GRAY;color: white;}
</STYLE>
<SCRIPT>//註解分享過程重點,貢獻,註解給人看,註解維護MAINTAIN程式碼
const period=4;//給定期數四期
const maxerror = 0.000000001; //要求的精確度
var payment = new Array(period);
function IRR()
{
  var a = 0;//TRY報酬率是否=0
  var b = 1;//TRY報酬率是否=1
  var c;//如果不是上面的a,b,就測試c=(a+b)/2
  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 )//如果淨現值f>0使用c取代下限a
        a = c;
       else
        b = c;//else否怎,淨現值f<0使用c取代上限b
       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>
<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> 

維基百科數值分析二分法

數值分析NUMERICAL ANALYSIS應用數學系,資訊工程或資訊科學數值方法NUMERICAL METHOD必修課,學程式設計求解。

P=PARAGRAPH, B=BOLD, H=HEADER, SRC=SOURCE

留言

  1. https://acdhdwstzws.blogspot.com/2025/06/h1-margin-0-padding-20px-0-color-000000.html

    回覆刪除
  2. https://lin-yujing.blogspot.com/2025/06/blog-post_16.html

    回覆刪除
  3. https://gbsddik.blogspot.com/2025/06/blog-post.html

    回覆刪除
  4. https://chiuerya0515.blogspot.com/2025/06/javascriptpythonirr.html

    回覆刪除
  5. https://danny0976790127.blogspot.com/2025/06/blog-post.html

    回覆刪除
  6. https://lu-cheng-yin-2024.blogspot.com/2025/06/617.html

    回覆刪除
  7. https://chenyenhsun.blogspot.com/2025/06/irr.html

    回覆刪除
  8. https://huuiiiiman.blogspot.com/2025/06/javascriptpythonirr.html

    回覆刪除
  9. https://tzuchenchu.blogspot.com/2025/06/javascriptpythonirr-6-16-2025.html

    回覆刪除
  10. https://yuweiiiiiiiiiiii.blogspot.com/2025/06/javascriptpythonirr.html

    回覆刪除
    回覆
    1. 嵌入維基百科優秀https://yuweiiiiiiiiiiii.blogspot.com/2025/06/javascriptpythonirr.html
      610.spider截圖,但沒有放程式碼。截圖太大,沒有重點。https://yuweiiiiiiiiiiii.blogspot.com/2025/06/pythonexcel-pv.html

      刪除
  11. https://chuyuchi.blogspot.com/2025/06/javascriptpythonirr.html

    回覆刪除
    回覆
    1. 還欠影片作業。https://chuyuchi.blogspot.com/2025/06/javascriptpythonirr.html

      刪除
  12. https://jiachiangkuo.blogspot.com/2025/06/617.html

    回覆刪除
    回覆
    1. https://www.youtube.com/watch?v=MGB5tUfIg08

      刪除
  13. https://takuto20050803.blogspot.com/2025/06/takutojavascriptpythonirr.html

    回覆刪除
  14. https://koala111222.blogspot.com/2025/06/javascriptpythonirr.html

    回覆刪除
    回覆
    1. 應付!https://koala111222.blogspot.com/2025/04/blog-post.html

      刪除
  15. https://sccabout.blogspot.com/2025/06/h1margin-0-padding-20px-0-color-000000.html

    回覆刪除
  16. https://huutingyu.blogspot.com/2025/06/h1-margin-0-padding-20px-0-color-000000.html

    回覆刪除
    回覆
    1. 準時交但沒有Excel截圖https://huutingyu.blogspot.com/2025/06/h1-margin-0-padding-20px-0-color-000000.html
      610python自定函數計算很完美https://huutingyu.blogspot.com/2025/06/pythonexcel-pv.html
      603.https://huutingyu.blogspot.com/2025/06/h1border-30px-outset-red-background.html

      刪除
  17. https://yangyami.blogspot.com/2025/06/h1-margin-0-padding-20px-0-color-000000.html

    回覆刪除
    回覆
    1. 完美呈現維基百科與程式碼https://yangyami.blogspot.com/2025/06/h1-margin-0-padding-20px-0-color-000000.html
      完美呈現影片與Spyder.https://yangyami.blogspot.com/2025/06/def-pvrnmfrnmf-p-mr1-11rnf1rn-return-p.html

      刪除
  18. https://sincewayne.blogspot.com/2025/06/h1-margin-0-padding-20px-0-color-000000.html

    回覆刪除
  19. https://winnielee0311.blogspot.com/2025/06/javascriptirr.html

    回覆刪除
  20. https://pagestjing.blogspot.com/2025/06/javascriptpythonirr.html

    回覆刪除
    回覆
    1. 520.https://pagestjing.blogspot.com/2025/05/javascriptsincoscanvasfont.html
      513.https://pagestjing.blogspot.com/2025/05/javascript_12.html
      507.https://pagestjing.blogspot.com/2025/05/javascript.html
      422.https://pagestjing.blogspot.com/2025/04/htmlbuttononclick.html
      318.https://pagestjing.blogspot.com/2025/03/javascriptpython.html
      311.https://pagestjing.blogspot.com/2025/03/pythonlistarraytuplesetdict.html
      304.https://pagestjing.blogspot.com/2025/03/w3schoolspython.html
      225.https://pagestjing.blogspot.com/2025/02/chatgpt.html

      刪除
  21. https://mumeng-ru.blogspot.com/2025/06/h1-margin-0-padding-20px-0-color-000000.html

    回覆刪除
  22. https://zhuo-1005.blogspot.com/2025/06/blog-post.html

    回覆刪除
  23. https://zhang-1108.blogspot.com/2025/06/javascriptpythonirr.html

    回覆刪除
    回覆
    1. 地表上最帥的男人就是張東煌https://youtu.be/YnH5gFiLzK8?si=jiz-i7eIItAVvo2s

      刪除
  24. https://kiannc1201.blogspot.com/2025/06/javascriptpythonirr.html

    回覆刪除
    回覆
    1. https://www.youtube.com/watch?v=Y_8VaLcky7s&t=1s

      刪除
  25. https://chinenkohana.blogspot.com/2025/06/javascriptsincoscanvasfont.html

    回覆刪除
    回覆
    1. nice video made by you.https://chinenkohana.blogspot.com/2025/05/javascriptsincoscanvasfont.html

      刪除
  26. https://xu-bing-wei-0804.blogspot.com/2025/06/h1-margin-0-padding-20px-0-color-000000.html

    回覆刪除
  27. https://pei940922.blogspot.com/2025/06/h1margin-0-padding-20px-0-color-000000.html

    回覆刪除
    回覆
    1. sin cos.https://pei940922.blogspot.com/2025/05/javascriptfontfillstylefilltext.html

      刪除
  28. https://slu804387.blogspot.com/2025/06/h1margin-0-padding-20px-0-color-000000.html

    回覆刪除
    回覆
    1. 沒有EXCEL對照https://slu804387.blogspot.com/2025/06/h1margin-0-padding-20px-0-color-000000.html

      刪除
  29. https://xuan-ang.blogspot.com/2025/06/blog-post.html

    回覆刪除
  30. 沒有使用Excel對照https://ting100000.blogspot.com/2025/06/h1-margin-0-padding-20px-0-color-000000.html

    回覆刪除
  31. https://yangyoyo0125.blogspot.com/2025/06/javascriptpythonirr.html

    回覆刪除
    回覆
    1. 拷貝老師的截圖自己沒有做https://yangyoyo0125.blogspot.com/2025/06/pythonexcel-pv.html

      刪除
  32. https://d11117242.blogspot.com/2025/06/javascriptpythonirr.html

    回覆刪除
    回覆
    1. 只有貼上https://d11117242.blogspot.com/2025/06/javascriptpython.html

      刪除
  33. https://d11117315111.blogspot.com/2025/06/javascriptpythonirr.html

    回覆刪除

張貼留言

這個網誌中的熱門文章

劉任昌python串列List(陣列Array),元組Tuple,集合Set,字典Dict

2025年3月4日上課Python 陣列array串列的函數(方法)

劉任昌Javascript物件導向Python物件導向