Algorithm problems for calculating NPV and IRR

Asked

Viewed 2,149 times

1

I have a logic problem to calculate NPV and IRR.

Formula for the NPV:

inserir a descrição da imagem aqui

Formula for the IRR:

inserir a descrição da imagem aqui

From these formulas I arrived at the following method:

public double[] calcularVPLTIR () {
    if(fluxosProducao == null)
        fluxosProducao = fluxoCaixaProducaoDAO.listar(cenario.getIdCenario());

    //Cálculo do VPL
    double vpl = 0;
    int size = fluxosProducao.size();

    for(int t = 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};
}

The VPL I think I’ve already done. The IRR is creating me some problems because the value I need is this IRR present in the formula and within the sum.

  • 1

    When you say logic problem, are you referring to not knowing how to solve the mathematical part, or is it some problem in the implementation? Here is a reference of the problematization of calculus, it would be good to give a read: https://pt.wikipedia.org/wiki/Taxa_interna_de_retorno

  • 1

    The mathematical question itself. I have no idea how to get the IRR out of the summation.

  • 2

    Viva Keynes and formulas that have become political hypotheses defended with hammer and sickle (by people who do not know economy :)). Follow code to get the IRR used by Apache POI (by / Newton Method as in Excel): Code

1 answer

-1

I managed to solve!

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};
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.