In act 1, scene 5, why does Hamlet plan to feign madness?In act 1 scene5
Hamlet is already under intense scrutiny by Claudius, who suspects that his stepson is plotting against him. Claudius will be watching and analyzing Hamlet throughout the play, and he will use others to help him pry into his stepson's soul. In a beautiful metaphor he tells Polonius
There's something in his soul,
O'er which his melancholy sits on brood,
And I do doubt the hatch and the disclose
Will be some danger...
Notably, he sends for Rosencrantz and Guildenstern, Hamlet's old school friends, to ask the prince leading questions and try to learn his secret thoughts. Claudius also uses Polonius to question Hamlet, and Polonius uses Ophelia for a similar purpose. Claudius also tries to use Gertrude for that purpose, especially in the scene in which Polonius is hiding behind the arras.
After his meeting with the ghost of his father, Hamlet understands that he is going to have to be even more careful because he is now harboring several potent secrets, any one of which could cost him his life. He now knows that Claudius killed his father. He knows that his father's ghost is haunting Elsinore Castle. He knows that Claudius has unlawfully gained the crown which should be his. He knows that he is going to have to murder Claudius to avenge his father. He decides that the best way for him to conceal his true thoughts, feelings, and intentions from all those prying people and others is to act insane. He has to put on a false front to hide his real face. His "insanity" is a mask. Otherwise Claudius would quickly realize that Hamlet had changed overnight and would become even more suspicious.
If Claudius had any inkling that Hamlet had been talking to the ghost of his own father, or even if Claudius heard that the ghost had been seen on the battlements, Claudius would become very dangerous. He would fear that his own terrible secrets had been leaked out by the ghost, who was the only one who knew his about his crimes of regicide and usurpation. Hamlet believes that if Claudius thinks him insane, then the king would assume that Hamlet could only have been talking to the ghost in an hallucination, and the hallucinatory ghost would not be able to convey the information that a real ghost of the murdered king could reveal to his son.
Hamlet tries to insure that none of the guards who saw the ghost will say anything about it. He swears them to secrecy. But he still cannot be sure that Claudius will not find out about its presence at Elsinore. The ghost might appear again. Who knows what a ghost might do? Hamlet is overwhelmed with what he has learned that night--including the fact that it is possible to hold conversations with the dead. At this point Hamlet may not even be too sure about his own sanity.