Okay, I think I managed to find the precise answer to this problem!
It involves solving a non-linear exponential equation, but I also found a good approximate solution using the truncated Taylor series. See below for a spreadsheet you can use.
Finding the future value:
Let's start by defining the growth factors per period, for money in the bank and money invested:


Now, let S be the amount ready to be invested after n+1 periods; so the first of that money has earned interest for n periods. That is,

The key step to solve the problem was to fix the total number of periods considered. So let's introduce a new variable:
t = the total number of time periods elapsed
So if money is ready to invest every n+1 periods, there will be t/(n+1) separate investments, and the future value of the investments will be:

This formula is exact in the case of integer t and n, and a good approximation when t and n are not integers. Substituting S, we get the version of the formula which explicitly depends on n:

Derivative of the future value with respect to n:
Fortunately, only a couple of terms in FV depend on n, so we can find the derivative after some effort:

Maximising the future value:
Equating the derivative to zero, we can remove the denominator, and assuming t is greater than zero, we can divide by the constant ( 1-G t ):

To simplify the equation, we can define some extra constants:



Then, we can define a function f(n) and write the equation as:

Note that α, β, γ, G, and R are all constant.
Solutions:
From here there are two options:
Use Newton's method or another numerical method for finding the positive root of f(n). This can be done in a number of software packages like MATLAB, Octave, etc, or by using a graphics calculator.
Solve approximately using a truncated Taylor series polynomial. I will use this method here.
Approximating the solution with a Taylor series:
The Taylor series of f(n), centred around n=0, is:

Truncating the series to the first three terms, we get a quadratic polynomial (with constant coefficients):

Closed-form approximate solution:
Using R, G, α, β and γ defined above, let c0, c1 and c2 be the coefficients of the truncated Taylor series for f(n):



Then,

n should be rounded to the nearest whole number. To be certain, check the values above and below n using the formula for FV.
Example:
Using the example from the question:
For example, I might put aside $100 every week to invest into a stock
with an expected growth of 9% p.a., but brokerage fees are $10/trade.
For how many weeks should I accumulate the $100 before investing, if I
can put it in my high-interest bank account at 4% p.a. until then?
Using Newton's method to find roots of f(n) above, we get n = 14.004.
Using the closed-form approximate solution, we get n = 14.082.
Checking this against the FV with t = 1680 (evenly divisible by each n + 1 tested):
- When n = 13, FV = $903,861.85
- When n = 14, FV = $903,891.13
- When n = 15, FV = $903,865.89
Therefore, you should wait for n = 14 periods, keeping that money in the bank, investing it together with the money in the next period (so you will make an investment every 14 + 1 = 15 weeks.)
Spreadsheet:
Here's one way to implement the above solution with a spreadsheet. StackExchange doesn't allow tables in their syntax at this time, so I'll show a screenshot of the formulae and columns you can copy and paste:
Formulae:

Copy and paste column A:
s
d
r
g
b
.
R
G
alpha
beta
gamma
.
c0
c1
c2
.
n (unrounded)
n
.
t (periods)
FV(n-1)
FV(n)
FV(n+1)
Copy and paste column B:
=100
=1/52
=0.04
=0.09
=10
.
=(1+B3/12)^(12*B2)
=(1+B4)^B2
=(B1-B5+B5*B7)*LN(B8)
=B1*LN(B8/B7)
=B1*LN(B7)
.
=0.5*(B9*B8/B7*LN(B8/B7)^2-B10*B8*LN(B8)^2)
=B9*B8/B7*LN(B8/B7)-B10*B8*LN(B8)
=B9*B8/B7-B10*B8-B11
.
=SQRT(B14^2-4*B13*B15)/(2*ABS(B13))-B14/(2*B13)
=ROUND(B17,0)
.
=LCM(B18+1,B18+1-1,B18+1+1)
=(B1*(1-B7^(B18-1+1))-B5*(1-B7))*(1-B8^B20)/((1-B7)*(1-B8^(B18-1+1)))
=(B1*(1-B7^(B18+1))-B5*(1-B7))*(1-B8^B20)/((1-B7)*(1-B8^(B18+1)))
=(B1*(1-B7^(B18+1+1))-B5*(1-B7))*(1-B8^B20)/((1-B7)*(1-B8^(B18+1+1)))
Results:

Remember, n is the number of periods to accumulate money in the bank. So you will want to invest every n+1 weeks; in this case, every 15 weeks.