I would not recommend in a 5 gallon at all, but I have read about some rather interesting ways to keep a mandarin in a smaller aquarium. It kind of depends how badly you want a mandarin and how much trouble you are willing to go through to keep it healthy and happy.
Here is what i have learned:
Their digestive tract is basically a straight shot. They don't take a long time to digest their food, and they very limited means of storing energy, they need a constant food supply. In nature they feed ALL DAY LONG. Honestly this is to some extent true of a lot of aquarium fish, but most are not as negatively affected as mandarins, nor is their diet so specific.
There is a danger with ones captive raised that will eat pellet or frozen food, that they will stop hunting all together, so that if you can't feed with frozen often enough, they will still be deprived of food a large portion of the day, which is unhealthy for them. They seem to tend towards being exclusive to a food type, and it will be luck of the draw whether you get one that may both hunt and accept prepared foods also. So eating frozen or pellet is not necessarily a guarantee of a healthy happy mandarin. Also as some have pointed out, the nutrients are different between frozen foods and live copepods, i don't know if i would consider them inferior like some have said, it's just different than the foods they eat by default.
You have to have structures in the aquarium that can allow copepods to reproduce without being hunted to extinction by the mandarin. In a small aquarium they can easily eat them all and leave pretty much none to reproduce. Some people construct pod hotels with an assortment of natural and unnatural materials, where the goal is simply to allow the pods to seek shelter and multiply within a space the mandarin can't get to. You will likely need a number of these structures, however you choose to create them.
You need a steady supply of fresh copepods. They will reproduce in a refugium and are safe from the mandarin hunting them there. I would say you need a rather large refugium compared to the display size, and i feel that from research I can safely say that hob refugiums that just flow directly from the compartment with the algae and live rock over into the aquarium, present the greatest chance of getting a steady stream of life pods into the aquarium. I would suggest having as much hob refugium as possible, as well as a refugium in a sump. And then another refugium that runs into that refugium, and a refugium to add onto the refugium with the refugium in it. Refugium refugium refugium.....refugium....
You will probably need to continually add copepods. For one thing, if you never introduce new ones, i'm just guessing that there could be some cross eyed, 6 toed inbred copepods in your system. Lack of genetic diversity is certainly not good for the copepod population. Obviously there is also the benefit of keeping the population bumped up. More than likely you will need to actually culture copepods to keep things affordable. You can start a copepod culture by buying a bottle of live copepods, and some bottles of phytoplankton, and adding a light source for photosynthesis(or sticking the container in a greenhouse) with i guess some minor water agitation, probably an airstone is enough. The only real way to get the copepods out of the water I think though is to put structures in the culture tank that the copepods will infest, and then you transfer those into your refugiums and display to spread out from there.
There are possibly some interesting options for using dosing pumps on a timer to pump water with phyto and copepods into the display tank at regular intervals, just little spurts all day, to keep introducing some live copepods. Problem is I think only copepod larvae swim in the water column, the adults that the mandarin will eat I think stick to rocks and other structures pretty exclusively. The moving of a pod hotel back and forth is probably the best way to ensure transfer.
Brine shrimp feeder. I can't remember their name, but one person came up with this idea and posted it on these forums some place. You basically have to create an underwater feeding station where brine shrimp can slowly escape, and fish will come eat the ones that was in the process of escaping, and mandarins have been observed to eat them. The design they tried was to cut out the center of the lid of a flat round container, then put some sort of mesh over the hole in the lid, just fine enough to make it a slow process for baby brine shrimp to wiggle through. They epoxied a hard airline tube into the flat round container that goes up to the surface of the aquarium and then has a funnel inserted or epoxied to it. They pour in live baby brine shrimp and it takes hours for all of them to travel down the funnel and escape the mesh, and the mandarin will pick at it for hours. This of course requires hatching and culturing baby brine shrimp and seining out the babies to dump in the funnel.
That's all i can think of for now.