OK, this is maybe still somewhat incomplete, but I've done the best I can for today, so decided to post.
Here's a symbolic partial exact answer for the no atmosphere (ignoring weather, refraction, dispersion, etc), spherical Earth, mostly circular orbit model. Angles in degrees. Probably some Northern hemisphere assumptions are unintentionally present. Ingredients we will need:
Inputs:
n = day number in the year, from 1 to 365.
L = latitude = 0 at equator, 90 at pole.
Parameters:
d = declination = solar noon signed zenith angle of the sun at the equator. This varies from -23.5 to 23.5, due to the Earth's tilt.
t = angle hour = 0 at solar noon = the signed angle the earth has rotated from solar noon.
(-a,a) = angle hours over which we integrate. A source of difficulty per the previous post.
a' = a * pi / 180 (convert a to radians)
S = unit vector of the Sun's location.
N = PV panel normal vector; panel azimuth is towards the equator.
Output:
T = PV panel's tilt angle = tilt towards equator compared to N being locally overhead.
Let's use a fixed frame of reference where the origin is the center of the Earth, the positive z-axis goes through our hemisphere's pole, the y-z plane includes our location (assume we aren't at a pole) and we have positive y, and the Earth is fixed with the sun apparently rotating overhead from negative x at sunrise to positive x at sunset.
The path of the sun is just the circle of latitude d (declination). So S(t) = (cos d sin t, cos d cos t, sin d). When the sun has risen sufficiently for the PV panel to produce, the instantaneous PV output's geometric factor is S dot N. If we integrate S dot N over the time period (-a, a) we get the total production geometric factor for that time period. We want to choose N to maximize that integral.
As commented earlier, if N is fixed for the day, the integration commutes with the dot product with N, so we can integrate S first, then take the dot product. That means we want N to point in the same direction as integral_(-a,a) S to maximize the dot product. [Assuming the bounds of integration don't depend on the choice of N, and S is never more than 90 degrees from N.] sin t is an odd function; d is fixed for the day, so cos d and sin d are constants; and the antiderivative of cos is sin. That makes the integral (0, cos d sin t, (sin d) t) | (-a,a) = (0, 2 sin a cos d, 2 a' sin d). [For integrating we need to use angles in radians.]
The angle this integral vector makes with the x-y plane is arctan (a' * tan d / sin a). The angle of N with the x-y plane is L - T. So we want L - T = arctan(a' * tan d / sin a), or T = L - arctan(a' * tan d / sin a).
Now we just need formulas for a and d as a function of day number n. Declination angle d is discussed here:
Declination Angle | PVEducation It gives d = -23.45 * cos (360*(n+10)/365) for the circular orbit approximation, as well as other more exact formulas.
As to a, there's a choice involved. The simplest case is that we integrate from sunrise to sunset, which should work in mid-latitudes between the fall equinox and spring equinox. In which case
Sunrise equation - Wikipedia gives a = arccos (-tan L * tan d).
Putting that all together, for these approximations and limitations, and inputs n, L, with angles in degrees, we get:
d = -23.45 * cos(360*(n+10)/365)
a = arccos(-tan L * tan d)
T = L - arctan( (a * pi / 180) * tan d / sin a)
For the other half of the year, you could try using an azimuth formula for the sun's location and choosing a to be the hour angle when the sun is due west. Then regardless of the panel tilt angle, the sun won't have set on the panel.
Cheers, Wayne