It's not clear to me what you're trying to accomplish -- it appears you want your 'follow' to wrap in a 300-unit coordinate system (50,350) while your 'target' wraps in a 400-unit (0,width) coordinate system? What does that mean? What happens in those other 100 coordinate units? Seems like a really bizarre use!
At any rate, the fix *might* be simple: All it's doing now is transforming a's world coordinate frame into b's local coordinate frame, so that a is always +/- w/2 from b. But it does assume that they're at least both using the same origin (0). Since in your case they're not, you'd need to further correct for that transformation, here's one easy way:
a -= minX; // put a's origin back at 0
a += ...// the existing half-span origin transform
a += minX; // put a's origin back where it was
return lerp(...
Similarly, if your usage gets weird enough to slide b off 0 origin then you may have to correct b also, it's hard to guess. The question to always ask is: how do i get a's coordinate in terms of a b-centered coordinate frame? (and then put it back afterwards) In such a coordinate frame, the shortest distance from b to a will always be in the right direction.
And if as you show, b's coordinate system is 400 units wide while a's is only 300 units wide, then you'll potentially even want to add a scaling factor in there (400/300ths) or you'd be lerp'ing across the wrong range. Sorry, that's about as clear as I can be without understanding the use. Suggested approach:
putAandBinACommonCoordinateSystem();
wraplerp(a,b,t, commonCoordinateSystemModuloBase);
putAandBbackInTheirOwnCoordinateSystems();