How can I calculate the optimal frequency of moving funds between two compound interest accounts? The Problem
I have two compound interest accounts, Account A, which has 0.5% yearly interest, but I have to keep \$30,000 there to unlock some bonus; and Account B, which has 2% yearly interest. Transferring between the two accounts costs \$0.5. I only want to keep \$30,000 in Account A to have the bonus and I want to transfer the rest to Account B. No additional money comes to Account A. Both accounts pay interest every minute.
I want to find the optimal times of moving funds from Account A to Account B that results in the most profit (a finite time horizon is imposed). The frequency can vary as time progresses. Here is a similar problem which has a solution that outputs the optimal times a transfer should happen.
What I've Tried
I've tried solving it using the compound interest formula, but got stuck at the point where the money gets transferred from one account to another, because calculating $\text{secondAmountAndInterest}$ yields incorrect results. I tried to make the calculation so it's transferring the money from one account into another (that's why I subtracted $B$ from $\text{firstAmountAndInterest}$ when calculating $\text{secondAmountAndInterest}$). Here's what I've tried:
Initial balance in USD: $B$ = 30000
First APR: $A_1 = 0.005$
Second APR: $A_2 = 0.02$
Transfer fee in USD: $T = 0.5$
\begin{align}
\text{firstAmountAndInterest} & = B \times \left(\frac {1+A_1} x\right) ^ x \\
\text{secondAmountAndInterest} & = (\text{firstAmountAndInterest} - B) \times \left(\frac {1+A_2} x\right) ^ x \\
y & = B-\text{secondAmountAndInterest} - x \times T
\end{align}
Here's a Python script I wrote that calculates the formula's maximum:
import scipy
from scipy import optimize


def f(x):
    balance_in_usd = 30000
    first_apr = 0.005
    second_apr = 0.02
    transfer_fee_in_usd = 0.5

    if x == 0:
        return 0

    first_amount_and_interest = balance_in_usd * (1 + first_apr / x) ** x
    second_amount_and_interest = (first_amount_and_interest - balance_in_usd) * (1 + second_apr / x) ** x

    return balance_in_usd + second_amount_and_interest - x * transfer_fee_in_usd


max_x = scipy.optimize.fmin(lambda x: -f(x), 0)

print("Frequency or no. of times you have to move funds annually:", max_x[0])
print("After how many days you have to move funds:", 365.25 / max_x[0])
print("After how many years you have to move funds:", 1 / max_x[0])

Output:
Optimization terminated successfully.
         Current function value: -30152.504308
         Iterations: 25
         Function evaluations: 50
Frequency or no. of times you have to move funds annually: 0.9070000000000009
After how many days you have to move funds: 402.70121278941525
After how many years you have to move funds: 1.1025358324145524

I've successfully calculated how often to compound fees when there is only one account, here's that as well. Somehow, I need to modify this so that it handles Account B as well.
Initial balance in USD: $B$ = 30000
First APR: $A_1 = 0.005$
Transfer fee in USD: $T = 0.5$
$$
y = B \times \left(\frac {1 + A_1} x\right)^x - x \times T
$$
 A: With interest being paid "every minute" it would be accurate to use the continuous compounding model.  Let's do some basic calculations for the case in which the time horizon is one year, and we make no transfers.
Continuous compounding says that the $30,000 principal in account A will become:
$$ 30000 \exp(0.005) = 30150.375625782 $$
while a more literal minute-by-minute compounding gives:
$$ 30000 \left(1 + \frac{0.005}{525600}\right)^{525600} = 30150.375624331 $$
This difference is tiny relative to the $0.50 cost of transfers, so we accept the continuous compounding formula for the sake of simplicity.
This computation also shows that at most 300 transfers from account A to account B could be done (in the one-year time interval), neglecting the possibility to use funds accruing in account B for such costs (they would certainly be counterproductive to maximizing profit).
This takes care of the case of not doing any transfers.  The number of transfers is discrete, a value between $0$ and $300$.  So instead of trying to model the "frequency" of transfers as a real number, we should explore the effect of doing a smallish number of transfers.  The timing of transfers need not be periodic.
Let's begin with the case of one transfer at time $t_1$ during the year interval.  The earliest we could make $t_1$ would be the point at which an accrued interest in account A reaches the transfer cost. That's clearly not optimal as nothing will actually be deposited in account B, so the profit will be less than leaving all the funds in account A until the end of the year, at which point the transfer if done at $t_1 = 1$ simply reduces the profit calculated above for no transfers by the transfer cost.
So we expect the optimal value of $t_1$ will occur between those extremes.  An expression can now be given:
$$ \left[30000 \left(\exp(0.005 t_1)-1 \right) - 0.5 \right] \exp(0.02(1-t_1))
+ 30000 \left(\exp(0.005 (1-t_1)) \right) $$
for the monies transferred from account A to account B at time $t_1$, there accruing interest at the higher rate for the remaining time $1-t_1$, plus the monies left in account A that accrue the lower rate of interest over that time.  This expression includes the original deposit of $30,000 as is consistent with the earlier calculations without transfers.
I used a spreadsheet to isolate which $t_1$ maximizes the profit, and within the limited precision of that, the best value of $t_1$ is very near $0.5$.  It gives an improvement (over no transfer) to $30150.43734 (so about a six cent increase).
Plan of Attack
I suspect the one transfer case, suitably generalized, admits a closed form solution, and this may allow us to solve multiple transfer problems by a dynamic programming approach.  So we begin by polishing the one transfer case.
A: Account A at each transfer $b_k$ to account B follows the recurrence
$$
\cases{\alpha_0 = A_1 \\
\alpha_k = \alpha_{k-1}e^{r_1\delta_k}-b_k}
$$
Account B grows also according to the recurrence
$$
\cases{\beta_0 = A_0e^{r_2\delta_1} \\
 \beta_k = (\beta_{k-1} + b_k-\lambda)e^{r_2\delta_{k+1}}}
$$
Here $\delta_k = t_k-t_{k-1}$. The restrictions are
$$
\mathcal{R}=\cases{b_k\ge \lambda \\ \delta_k\ge 0 \\ \alpha_k \ge A_1 \\ \sum_k\delta_k = t_{max}}
$$
Taking $k_{max}$ transfers we have the optimization setup:
$$
\max_{b,\delta}(\alpha_{k_{max}}+\beta_{k_{max}-1})\ \ \ \text{s. t.}\ \ \ \mathcal{R}
$$
Follows a MATHEMATICA script which implements this algorithm.
tmax = 0.5;
data = {lambda -> 0.5, A1 -> 30000, r1 -> 0.005, r2 -> 0.02, A0 -> 0}
alpha[0] = A1;
beta[0] = A0 Exp[r2 dt[1]];
vars = {};
restrs = {};
kmax = 10;

For[k = 1, k <= kmax, k++,
 If[k < kmax, alpha[k] = alpha[k - 1] Exp[r1 dt[k]] - b[k], alpha[k] = alpha[k - 1] Exp[r1 dt[k]]]; 
 beta[k] = (beta[k - 1] + b[k] - lambda) Exp[r2 dt[k + 1]]; 
 If[k < kmax, AppendTo[vars, {b[k], dt[k]}]]; 
 If[k < kmax, AppendTo[restrs, alpha[k] - A1]]
 ]

restrs1 = restrs;
AppendTo[restrs, Table[b[k] - lambda, {k, 1, kmax - 1}]];
AppendTo[restrs, Table[dt[k], {k, 1, kmax}]];
restrs0 = Thread[Flatten[restrs] > 0];
AppendTo[restrs0, tmax - Sum[dt[k], {k, 1, kmax}] == 0];
restrs0 = restrs0 /. data;
vars = Join[Table[b[k], {k, 1, kmax - 1}], Table[dt[k], {k, 1, kmax}]];
obj = alpha[kmax] + beta[kmax - 1] /. data;
sol = NMaximize[Join[{obj}, restrs0], vars, Method -> "DifferentialEvolution"]

According to the outcomes, for big $k_{max}$, a possible solution could give near optimal results with $\delta_k=\frac{t_{max}}{k_{max}}$: in this case, the recurrences for $\alpha_k,\beta_k$ have closed form.
NOTE
Considering $\delta_k = \frac{t_{max}}{k_{max}},\eta_1 = e^{r_1\frac{t_{max}}{k_{max}}},\eta_2 = e^{r_2\frac{t_{max}}{k_{max}}}$ the recurrences read
$$
\cases{\alpha_0 = A_1 \\
\alpha_k = \alpha_{k-1}\eta_1-b_k}
$$
$$
\cases{\beta_0 = A_0\eta_2 \\
 \beta_k = (\beta_{k-1} + b_k-\lambda)\eta_2}
$$
and the optimization problem reads:
$$
\max_{b}(\alpha_{k_{max}}+\beta_{k_{max}-1})\ \ \ \text{s. t.}\ \ \ \mathcal{R}_0
$$
with
$$
\mathcal{R}_0=\cases{b_k \le A_1\\ b_k\ge \lambda \\ \alpha_k \ge A_1 }
$$
which can be solved with a simplex (Linear Programming) approach.
Follows an attachable script which handles the Linear Programming issue.
obj0 = alpha[kmax] + beta[kmax - 1] /. Thread[Table[dt[k], {k, 1, kmax}] -> tmax/kmax] /. data;
varsb = Table[b[k], {k, 1, kmax - 1}];
coefs = Grad[obj0, varsb];
c0 = obj0 /. Thread[varsb -> 0];
AppendTo[restrs1, (beta[kmax] - A1) /. {b[kmax] -> 0}];
restrs10 = restrs1 /. Thread[Table[dt[k], {k, 1, kmax}] -> tmax/kmax] /. data;
nl = Length[restrs10];
A = {};
AB = {};
zerosb = Thread[varsb -> 0];

For[k = 1, k < nl, k++,
 AppendTo[A, Grad[restrs10[[k]], varsb]];
 AppendTo[AB, restrs10[[k]] /. zerosb]
 ]

Ab = IdentityMatrix[kmax - 1];
Bb = Table[lambda, {k, 1, kmax - 1}] /. data;
Ac = -Ab;
Bc = Table[-A1, {k, 1, kmax - 1}] /. data;
AA = Join[A, Ab];
AA = Join[AA, Ac];
BB = Join[-AB, Bb];
BB = Join[BB, Bc];

x = LinearProgramming[-coefs, AA, BB]
coefs.x + c0

