趙濬祺
<head>
<style>
h1 {
margin: 0;
padding: 20px 0;
color: #000000;
text-shadow: 5px 5px 2px yellow;
}
.Takming {
border: 20px outset red;
background-color: green;
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 class="Takming">趙濬祺向劉任昌學習程式設計程式碼如下</h1><div><div><head></div><div><style></div><div>h1 {</div><div> margin: 0;</div><div> padding: 20px 0; </div><div> color: #000000;</div><div> text-shadow: 5px 5px 2px yellow;</div><div>}</div><div>.Takming {</div><div> border: 20px outset red;</div><div> background-color: green;</div><div> color: white;</div><div> text-align: center;}</div><div>.pmt {</div><div> width: 60pt;</div><div> height: 20pt;</div><div> background-color: coral;</div><div> color: white;</div><div> text-align: right;}</div><div></style></div><div></head></div><div><body></div><div><table border="1"></div><div><tr align="center"><td>躉繳</td><td>第1期</td><td>第2期</td><td>第3期</td></tr></div><div><tr><td><input class="pmt" type="number" /></td></div><div> <td><input class="pmt" type="number" /></td></div><div> <td><input class="pmt" type="number" /></td></div><div> <td><input class="pmt" type="number" /></td></tr></div><div></table><br /></div><div>注意,包含首期躉繳的現金流量都大於0。</div><div><p><input onclick="IRR()" type="button" value="計算內部報酬率" />輸出:</p> <!-- 按下 button 去執行 JavaScript IRR( )--></div><div><p>報酬率:<b id="irr"></b></p> </div><div><p>淨現值:<b id="npv"></b></p> </div><div><p>迴圈次數:<b id="loopNumber"></b></p> </div><div></body></div><div> <script></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> var a = 0;</div><div> var b = 1;</div><div> var c;</div><div> var f;</div><div> var gap=10;</div><div> var loopNumber = 0;</div><div> for (var i=0; i<period; i++)</div><div> payment[i]=document.getElementsByClassName("pmt")[i].value;</div><div> f = npv(a);</div><div> if ( f==0 )</div><div> document.getElementById("irr").innerHTML = "內部報酬率等於0.";</div><div> else if ( f<0 )</div><div> document.getElementById("irr").innerHTML = "內部報酬率小於0.";</div><div> else</div><div> { while ( gap > maxerror && Math.abs(f) > maxerror && loopNumber < 100)</div><div> { </div><div> loopNumber++;</div><div> c = (a+b)/2;</div><div> f = npv(c);</div><div> if ( Math.abs(f) > maxerror && gap > maxerror)</div><div> { if ( f>0 )</div><div> a = c;</div><div> else</div><div> b = c;</div><div> gap = b-a;</div><div> }</div><div> } //迴圈while結束</div><div> }</div><div> document.getElementById("irr").innerHTML= c;</div><div> document.getElementById("npv").innerHTML= f;</div><div> document.getElementById("loopNumber").innerHTML = loopNumber;</div><div>} </div><div>var npv=function(rate)</div><div> { </div><div> var y= -payment[0]; </div><div> for (var j=1; j<period; j++)</div><div> y = y + payment[j]/(1+rate)**j;</div><div> return y;</div><div> }</div><div></script></div><div><h1 class="Takming">趙濬祺向劉任昌學習程式設計程式碼如下</h1></div></div>
留言
張貼留言