There can be many causes of ED. Some may be as a result of an underlying condition and in others it could stem from childhood trauma or any other sort of trauma. Usually if ED has developed suddenly then it's a symptom of a mental health problem and may resolve once the mental state improves.
In most cases a psychological cause is more likely especially when there are times one can maintain an erection e.g. during masturbation or in the morning when they wake up.
In many cases individuals wonder can stress and anxiety cause erectile dysfunction? The simple answer is yes it contributes to it.
Stress
Individuals may experience such symptoms of ED during times of high stress. This could be due to job, money or relationship problems. It could result in a low sex drive which can then contribute to the stress of not being interested in sex, and the cycle continues.
Anxiety
Individuals may suffer from anxiety as a condition or sometimes as a one off experience. Once the feeling of not performing occurs, men may overly worry about it happening again and develop sexual performance anxiety. When this happens frequently it can form a fear of sexual failure resulting in erectile dysfunction.
This could further go into guilt of not being able to satisfy the partner which can also lead into progressive anxiety.
Depression
Experiencing depression can have both physical and psychological effects. Low self esteem can occur due to previous episodes of inability to achieve an erection or as a result of any other factor in life. These feelings can also happen due to problems with relationships. Also could be as a result of age and loss of interest in sexual activity.
Treating the underlying reason could help completely reverse the dysfunction. However sometimes individuals get even more stressed, anxious or depressed when they experience ED which then creates a vicious cycle.