Some thing to try to check if it is a software or a hardware problem:
a). Wipe the cache partition and then check if the internet connection is stable.
b). Start the phone in safe mode and then check if the internet connection is stable.
If it is then a downloaded app is the cause of the problem. The trick is to find out which one. You will need to uninstall each downloaded app, one by one and then test the phone in normal mode between each uninstall. When it works OK then the last downloaded app that was uninstalled was the culprit. A downloaded app is one that you downloaded and installed on the phone and is not one that came pre-installed on the phone when you first got it.
If still no good then it may be a hardware problem.
Try using the phone’s WiFi as close as possible to the router (<30cm) and check if the internet connection remains stable.
If it does then it may be a loose or faulty WiFi antenna (example only to show what it looks like) in the phone that is causing the loss of connection.
Here’s some images taken from a teardown video for the phone showing the antenna routing and connections at both ends of the antenna. It’s not the best view of it but hopefully it may help.
(click on image to enlarge for better viewing)