<html>
<body>
<script>
var par=100;
var price=120;
var call_price=105;
var nper=6;
var c_rate=0.04;
var guess=0.1;
var pmt;
var ytc5;
var ytc10;
pmt=par*c_rate;
ytc5 = Math.round(nr(guess, nper, pmt, price, call_price)*20000)/100;
nper = 16;
call_price=103;
ytc10 = Math.round(nr(guess, nper, pmt, price, call_price)*20000)/100;
document.write("YTC after 5 = "+ytc5+"% <br/> YTC after 10 = "+ytc10+"%");
function nr(guess, nper, pmt, price, call_price)
{
var x,x0,f,f_prime;
var i;
x0 = guess;
for (i=0;i<100;i++)
{
f = -price + pmt * pvifa(x0,nper) + call_price * pvif(x0,nper);
f_prime = pmt * pvifa_prime(x0,nper) + call_price * pvif_prime(x0,nper);
if(f_prime==0)
return "#DIV0";
x = x0 - f/f_prime;
if(Math.abs(x-x0)<0.000001)
return x;
x0 = x;
}
return "#NUM!";
}
function pvif(i,n)
{
if (i==0)
return 1;
if (n==0)
return 1;
return Math.pow(1+i,-n);
}
function pvif_prime(i,n)
{
if (i==0)
return 0;
if (n==0)
return 0;
return -n*Math.pow(1+i,-n-1);
}
function pvifa(i,n)
{
if (i==0)
return 1;
if (n==0)
return 0;
return (1 - pvif(i,n))/i;
}
function pvifa_prime(i,n)
{
if (i==0)
return 0;
if (n==0)
return 0;
return ( i*-pvif_prime(i,n) - 1 + pvif(i,n) ) / Math.pow(i,2);
}
</script>
</body>
</html>