I have a website that has a page that does amortization schedules. It has to calculate the correct annual percentage rate (APR) and it has to do it just exactly as this tool:
https://www.ffiec.gov/examtools/FFIE.../#/accountdata

This question will not be answered off the "top of your head"....It will require some "work" on your part. If you don't like the work, I understand. Please don't bother posting in this thread if you don't like the requirements. It's sort of silly because you cannot write the code if you don't understand the math and how iteration works. The math is explained in a document which you can download through the help link at the tool. The general equation to compute the APR is at the bottom of the document under "other calculations". The document provides a sample loan with a single payment stream. This can be found around page 6 of the document. It's a $1000 loan with a $25 finance charge. So the amount financed is $975. There are 3 payments of $340. You plug these into the tool for the 1 payment stream and click the next button and the tool will calculate the APR at 27.48%.

Once again, this is a single stream of 3 payments of the same amount. ($340) I already have working code that does correctly compute this simple example in the document. The problem is to expand the code with a loop so that it can handle multiple payment streams. I have attempted to do this with an outer loop to feed in the payment streams. I have stored the payment amount and the number of payments into arrays. Here is the code that computes the APR for the one payment stream:


Code:
Public Overridable Function generalEquation(ByVal period As Integer, ByVal payment As Double, ByVal initialPeriods As Double, ByVal fractions As Double, ByVal rate As Double) As Double
        Dim retval As Double = 0
        For x As Integer = 0 To period - 1
            Dim jj As Decimal = (1.0 + fractions * rate)
            Dim kk As Decimal = Math.Pow(1 + rate, initialPeriods + x)
            retval += payment / ((1.0 + fractions * rate) * Math.Pow(1 + rate, initialPeriods + x))
        Next x
        Return retval
    End Function
    ''' 
    ''' <param name="amount"> The initial amount A </param>
    ''' <param name="payment"> The periodic payment P </param>
    ''' <param name="payments"> The total number of payments n </param>
    ''' <param name="ppy"> The number of payment periods per year </param>
    ''' <param name="APRGuess"> The guess to start estimating from, 10% is 0.1, not 0.001 </param>
    ''' <param name="odddays"> Odd days, as a fraction of a pay period.  10 days of a month is 0.33333... </param>
    ''' <param name="full"> Full pay periods before the first payment.  Usually 1. </param>
    ''' <returns> The calculated APR </returns>
    Public Overridable Function findAPRGEQ(ByVal amount As Double, ByVal payment As Double, ByVal payments As Integer, ByVal ppy As Double, ByVal APRGuess As Double, ByVal odddays As Double, ByVal full As Double) As Double
        Dim result As Double = APRGuess
        Dim tempguess As Double = APRGuess
        Do
            result = tempguess
            'Step 1
            Dim i As Double = tempguess / (100 * ppy)
            Dim A1 As Double = generalEquation(payments, payment, full, odddays, i)
            'Step 2
            Dim i2 As Double = (tempguess + 0.1) / (100 * ppy)
            Dim A2 As Double = generalEquation(payments, payment, full, odddays, i2)
            'Step 3
            tempguess = tempguess + 0.1 * (amount - A1) / (A2 - A1)
        Loop While Math.Abs(result * 10000 - tempguess * 10000) > 1
        Return result
    End Function

Here is the code as I tried to expand it with the outer loop:
Public Overridable Function generalEquation1(ByVal period As Integer, ByVal numpmts As ArrayList, ByVal payment As ArrayList, ByVal initialPeriods As Double, ByVal fractions As Double, ByVal rate As Double) As Double
Dim retval As Double = 0
Dim x As Integer = 0
Dim z As Integer = 0
For y = 0 To numpmts.Count - 1 'number of payment streams
For x = 0 To numpmts(y)
If x = numpmts(y) Then
x = 0
Exit For
End If
retval += payment(y) / ((1.0 + fractions * rate) * Math.Pow(1 + rate, initialPeriods + x))
z = z + 1
Next x
Next y
Return retval
End Function

'''
''' <param name="amount"> The initial amount A </param>
''' <param name="payment"> The periodic payment P </param>
''' <param name="payments"> The total number of payments n </param>
''' <param name="ppy"> The number of payment periods per year </param>
''' <param name="APRGuess"> The guess to start estimating from, 10% is 0.1, not 0.001 </param>
''' <param name="odddays"> Odd days, as a fraction of a pay period. 10 days of a month is 0.33333... </param>
''' <param name="full"> Full pay periods before the first payment. Usually 1. </param>
''' <returns> The calculated APR </returns>
Public Overridable Function findAPRGEQ1(ByVal amount As Double, ByVal payment As ArrayList, ByVal payments As ArrayList, ByVal ppy As Double, ByVal APRGuess As Double, ByVal odddays As Double, ByVal full As Double) As Double
Dim result As Double = APRGuess
Dim tempguess As Double = APRGuess
Dim totpmts As Integer = 0
Dim info As Decimal = 0
For i = 0 To payments.Count - 1
totpmts += payments(i)
Next
Do
result = tempguess
'Step 1
Dim i As Double = tempguess / (100 * ppy)
Dim A1 As Double = generalEquation1(totpmts, payments, payment, full, odddays, i)
'Step 2
Dim i2 As Double = (tempguess + 0.1) / (100 * ppy)
Dim A2 As Double = generalEquation1(totpmts, payments, payment, full, odddays, i2)
'Step 3
tempguess = tempguess + 0.1 * (amount - A1) / (A2 - A1)
Console.Write(tempguess)
info = Math.Abs(result * 10000 - tempguess * 10000) > 1
Loop While Math.Abs(result * 10000 - tempguess * 10000) > 1
Return result
End Function


Dim apr1 As Double = 0
Dim amount As Double = Convert.ToDouble(txtamtfin.Text)
'apr1 = findAPRGEQ1(amount, pmtarray, pmtterm, 12, (Noterate / 12) + 0.1, 0, 1)
'apr = findAPRGEQ(975, 340, 3, 12, 10, 0, 1) <------------If you run this line it will computer the APR at 27.48%.
txtapr.Text = String.Format("{0:#,##0.000}", apr1)

So how would you make the code work with multiple payment streams?