Normally in a variable rate mortgage the payment would vary with the rate. However here is a formula for a fixed payment, (where, as the OP says, the rate adjustment is known in advance):
d = (p r1 (1 + r1)^m r2 (1 + r2)^n)/
(-r1 + (1 + r2)^n (r1 + (-1 + (1 + r1)^m) r2))
where
d is the periodic payment
p is the loan amount
r1 is the periodic rate for the first m periods
r2 is the periodic rate for the next n periods
Here is how the formula is derived.
First, taking a simplified problem to show the workings more clearly.
Let's say a £100,000 loan repaid by 5 annual payments. The first 2 years at 3% and the following 3 years at 4%.
p = 100,000
r1 = 0.03
m = 2
r2 = 0.04
n = 3
The loan amount is equal to the sum of the present value of the payments. These are the present values of the payments for each period, discounted by the interest rate(s):-
pv1 = d/(1 + r1)
pv2 = d/((1 + r1) (1 + r1))
pv3 = d/((1 + r1) (1 + r1) (1 + r2))
pv4 = d/((1 + r1) (1 + r1) (1 + r2) (1 + r2))
pv5 = d/((1 + r1) (1 + r1) (1 + r2) (1 + r2) (1 + r2))
And p = pv1 + pv2 + pv3 + pv4 + pv5
This can be expressed as a summation
and converted to a formula by induction:
p = ((1 + r1)^-m (1 + r2)^-n (-d r1 +
d (1 + r2)^n (r1 + (-1 + (1 + r1)^m) r2)))/(r1 r2)
Rearranging to give a formula for the payment:
d = (p r1 (1 + r1)^m r2 (1 + r2)^n)/
(-r1 + (1 + r2)^n (r1 + (-1 + (1 + r1)^m) r2))
∴ d = 22078.67
Amortization table for the above result showing figures and formulas
Returning to the OP's example for, say, a loan of one million, with the effective rate of interest at 3% for the first 5 years and 4% for the following 20 years.
p = 1,000,000
r1 = (1 + 0.03)^(1/12) - 1 = 0.00246627
m = 5*12 = 60
r2 = (1 + 0.04)^(1/12) - 1 = 0.00327374
n = (25 - 5)*12 = 240
The payment d = 5026.48
Note for the use of nominal rates
For nominal interest rates of 3% and 4% compounded monthly:
p = 1,000,000
r1 = 0.03/12 = 0.0025
m = 5*12 = 60
r2 = 0.04/12 = 0.00333333
n = (25 - 5)*12 = 240
The payment d = 5057.80