IRR Result
Enter values to see the IRR.
function calculateIRR() {
var initialInvestment = parseFloat(document.getElementById("initialInvestment").value);
var cashFlow1 = parseFloat(document.getElementById("cashFlow1").value);
var cashFlow2 = parseFloat(document.getElementById("cashFlow2").value);
var cashFlow3 = parseFloat(document.getElementById("cashFlow3").value);
var cashFlow4 = parseFloat(document.getElementById("cashFlow4").value);
var cashFlow5 = parseFloat(document.getElementById("cashFlow5").value);
if (isNaN(initialInvestment) || isNaN(cashFlow1) || isNaN(cashFlow2) || isNaN(cashFlow3) || isNaN(cashFlow4) || isNaN(cashFlow5)) {
document.getElementById("irrResult").innerHTML = "Please enter valid numbers for all fields.";
return;
}
// IRR calculation is an iterative process. A common approach is to use a numerical method like Newton-Raphson.
// For simplicity and broad compatibility, we'll implement a basic iterative search.
// This implementation finds the IRR by trying different discount rates.
var cashFlows = [-initialInvestment, cashFlow1, cashFlow2, cashFlow3, cashFlow4, cashFlow5];
var maxIterations = 1000;
var tolerance = 0.0001;
var guess = 0.1; // Initial guess for IRR
function calculateNPV(rate, flows) {
var npv = 0;
for (var i = 0; i < flows.length; i++) {
npv += flows[i] / Math.pow(1 + rate, i);
}
return npv;
}
var irr = NaN;
var rate = guess;
for (var i = 0; i < maxIterations; i++) {
var npv = calculateNPV(rate, cashFlows);
if (Math.abs(npv) < tolerance) {
irr = rate;
break;
}
// Using Newton-Raphson method for improved convergence
// Derivative of NPV with respect to rate: sum of -i * cashFlow[i] / (1 + rate)^(i+1)
var derivative = 0;
for (var j = 1; j < cashFlows.length; j++) {
derivative += -j * cashFlows[j] / Math.pow(1 + rate, j + 1);
}
if (derivative === 0) {
// Avoid division by zero, try a small increment
rate += 0.001;
continue;
}
rate = rate – npv / derivative;
// Handle cases where the rate might become too high or too low, or non-real
if (isNaN(rate) || !isFinite(rate) || rate < -1) {
// If rate goes negative or becomes invalid, we can't converge with this guess.
// A more robust solver might try different initial guesses or methods.
// For this example, we'll stop if it becomes invalid.
break;
}
}
if (!isNaN(irr)) {
document.getElementById("irrResult").innerHTML = "IRR: " + (irr * 100).toFixed(2) + "%";
} else {
document.getElementById("irrResult").innerHTML = "Could not calculate IRR with the given cash flows. Try adjusting values or using a different tool for complex scenarios.";
}
}
.calculator-container {
font-family: 'Arial', sans-serif;
max-width: 600px;
margin: 20px auto;
padding: 20px;
border: 1px solid #ddd;
border-radius: 8px;
background-color: #f9f9f9;
}
.calculator-container h2 {
text-align: center;
color: #333;
margin-bottom: 20px;
}
.calculator-form .form-group {
margin-bottom: 15px;
}
.calculator-form label {
display: block;
margin-bottom: 5px;
font-weight: bold;
color: #555;
}
.calculator-form input[type="number"] {
width: calc(100% – 22px);
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 1em;
}
.calculator-form button {
width: 100%;
padding: 12px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 1.1em;
transition: background-color 0.3s ease;
}
.calculator-form button:hover {
background-color: #0056b3;
}
.calculator-result {
margin-top: 25px;
padding: 15px;
border-top: 1px solid #eee;
background-color: #fff;
border-radius: 4px;
}
.calculator-result h3 {
margin-top: 0;
color: #333;
}
#irrResult {
font-size: 1.2em;
font-weight: bold;
color: #28a745; /* Green for success */
text-align: center;
}
#irrResult.error {
color: #dc3545; /* Red for error */
}