Ubuntu 16.04 completely freezes when using Nvidia card

If I change the PRIME profile configuration on Nvidia XServer Settings to NVIDIA performance mode, it tells me to logout and log in again to apply the alterations. When I do that, nothing changes. However, when I reboot, the changes finally take place, I am able to use the system for a couple of minutes and then Ubuntu freezes. Nothing works. I can’t even switch to tty and the only thing I can do is to press the power button. When the system starts again, Intel is running and if I change to Nvidia, the process starts again (reboot, freezes, etc)

My graphics card is a GeForce 940m. If I run lspci -k | grep -EA2 ‘VGA|3D’ the result is the following:

00:02.0 VGA compatible controller: Intel Corporation Broadwell-U Integrated Graphics (rev 09)
Subsystem: ASUSTeK Computer Inc. Broadwell-U Integrated Graphics
Kernel driver in use: i915
04:00.0 3D controller: NVIDIA Corporation GM108M [GeForce 940M] (rev ff)
Kernel driver in use: nvidia
Kernel modules: nvidiafb, nouveau, nvidia_367, nvidia_367_drm
Oddly, the result of prime-select query while using Intel is unkown. During the few minutes I am able to use the Nvidia card, the result of prime-select query is nvidia.

I don’t believe there is any hardware issue with the graphics card, since it works fine on Windows.