Sometimes tension, both emotional and physical can cause loss of the erection. When you masturbate you are in the best position for you and you take all the time you need. When you have intercourse with your partner, you might be tensed both because of the expectations and the physical tiredness. You might want to visit a sexual therapist to learn certain relaxation techniques.