Here are six things you can do when you're discouraged or frustrated with your writing:
1) Don't berate yourself for having these feelings. Accept them. They are natural emotions, and it is perfectly reasonable to experience them from time to time.
2) Observe the specific emotion in your mind. Listen to your internal mental dialog about the feeling. Don't try to analyze or explore your feelings--just watch and listen. You'll find even the most debilitating emotions become much less overwhelming under patient, quiet observation.
3) Meditate quietly for 15-20 minutes and imagine your frustration as a physical object, perhaps as an anchor or a heavy weight chained to you. Visualize yourself cutting the anchor or the weight away, and then imagine how light and free you feel once you are free of the burden.
4) Put up your writing for an hour or two and do something completely different. Exercise. Call your parents. Cook dinner. Do something to help someone else. Teach someone something new. Then return to your writing task.
5) Indulge yourself a little. Enjoy a glass of good wine or some dark chocolate. Have some ice cream. And then try another writing session.
6) Finally, use the emotions as tools. Channel them into your writing. Create characters who are allegories of these emotions. Create a villain who brings these feelings onto others, or a hero who struggles mightily with them. If some critic's opinion of your work happens to set you off, channel your emotions by lampooning the critic.
These techniques can be used individually or in combination. For me, #6 tends to be the most effective and reliable, followed by #3. Which ones work best for you?
Most importantly, make sure you keep writing. Don't give in.