Problems with algorithm to calculate NPV and TIR

1

I have a logic problem to calculate the NPV and IRR.

NPV formula:

TIRFormula:

Fromtheseformulas,Icameupwiththefollowingmethod:

publicdouble[]calcularVPLTIR(){if(fluxosProducao==null)fluxosProducao=fluxoCaixaProducaoDAO.listar(cenario.getIdCenario());//CálculodoVPLdoublevpl=0;intsize=fluxosProducao.size();for(intt=0;t<size;t++){System.out.println("Fluxo ano "+t+" = "+fluxosProducao.get(t).getFluxoCaixa());
        vpl += fluxosProducao.get(t).getFluxoCaixa() / Math.pow((1 + tributosParametros.getTaxaDesconto()/100), (t+1));
    }

    System.out.println("DESCONTO = "+tributosParametros.getTaxaDesconto()/100);
    System.out.println("VPL ANTES DO ANO 0 = "+vpl);

    vpl -= 1261306.64;//cenario.getSaldoFinalFluxoExploracao(); 

    //Cálculo da TIR
    double tir = 0.0;
    double aux;

    do {
        aux = 0;
        tir += 0.01;

        for(int i = 0; i < size; i++)
            aux += fluxosProducao.get(i).getFluxoCaixa()/Math.pow((1 + tir), (i+1));
        System.out.println("VPL - TIR = "+(aux-vpl));
    } while((aux-vpl) < 0);

    System.out.println("TIR = "+String.valueOf(tir));
    return new double[]{vpl, tir};
}

A VPL I think I've already been able to do. The one in the TIR is creating me some problems because the value I need is this IRR present in the formula and within the summation.

    
asked by anonymous 14.11.2015 / 21:07

1 answer

-1

I was able to solve it!

public double[] calcularVPLTIR () {
        double[] fc = new double[] {-6000000,2300000,2500000,2500000,2000000,3000000};

        //Cálculo do VPL
        double vpl = 0;
        int size = fc.length;
        double prepot = (1 + 0.2);

        for(int t = 0; t < size; t++) {
            double pot = Math.pow(prepot, t);
            vpl += fc[t] / pot;
        }

        System.out.println("DESCONTO = 0.2");
        System.out.println("VPL = "+vpl);

        //Cálculo da TIR    
        double tir = 0.0;
        double aux; 

        do {
            aux = 0;
            tir += 0.01;

            for(int i = 0; i < size; i++) 
                aux += fc[i] / Math.pow((1+tir/100), i);
            System.out.println("AUX = "+aux);
        } while(aux > 0);

        System.out.println("TIR = "+String.valueOf(tir));

        return new double[]{vpl, tir};
    }
    
17.11.2015 / 19:23