Interestingly, Shakespeare wrote Macbeth as a tribute to King James, the new monarch of England at the time. When Shakespeare wrote the play, he included several elements that would have appealed to the king: witchcraft and ancestry. First, King James was previously interested in demonology, including witchcraft. Several witches had been foiled in their attempt to place on a curse on James when he was king of Scotland. As a result, James wrote a text entitled Demonology, which was offered to the public as his treatise on witchcraft among other things. Clearly, Shakespeare uses this information as the basis for his 3 witches in Macbeth. Next, Shakespeare wrote Macbeth to praise King James and his ancestry. The character of Banquo was the ancestor of King James; you can observe this in Act IV when the witches show the apparitions of the 8 kings descending from Banquo. The final king holds a glass in his hand. In the staging of this scene, the final apparition would have held a glass (mirror) up towards James so that when James looked down at it, his reflection was seen in the mirror - thus insinuating that he was in the line of kings descending from Banquo and paying homage to James' lineage.
Shakespeare wrote "Macbeth" specifically for King James I. The king was a large supporter of the theater, and Shakespeare's company even became known as "The King's Men." King James I was king of Scotland and eventually became King of England; his ancestry could be traced back to Banquo, which was documented in the "Chronicles of England, Scotland, and Ireland" by Raphael Holinshed (1587). The "Chronicles" were a primary source for Shakespeare throughout the course of his career.
The question demands a specific answer but it is not so straightforward. The consideration that Macbeth was written in order to please the new King James the subject of which is Scottish history and and King James's ancestors is, no doubt, a fact but, it is only one part of the story. It is relevant to quote William C. Carroll (from his introduction to Macbeth : Texts and Contexts, Bedford / St. Martin's, Page - 6 ):
"Rather than clarifying and reinforcing the theories of kinship and sovereign power that King James proposed in his writings and speeches, the play seems to go out of its way to mystify and undermine those theories, and in doing this, Shakespeare's play powerfully reproduces some of the major political controversies of his day."
It is understated to say Macbeth was written to please James I of England (aka James the VI of Scotland). Macbeth was political propaganda to support the ligitimacy of James I/VI with an extensive litany of lies, some of which Shakespear borrowed from Holinshed's Chronicles. James was a distant cousin of Elizabeth I named to be her successor with wobbly claims to the thrones of both Scotland and England. As his family name (Stewart) suggests, his lineage traces back to the High Steward (business manager) under Robert the Bruce, who married that Scottish King's daughter to gain the throne. Banquo was an invention of Hector Boece, a scholor in the payment of the Stewarts, to create an alternate line of legitimacy, and this lie was repeated by Holinshed and Shakespeare. MacBeth did not have old, kind, King Duncan assassinated. He defeated this young, intemperate cousin in battle near Elgin when Duncan marched with his army into MacBeth's lands - Macbeth was Mormaer (Thane/Earl) of Moray and surroundings - Duncan wa likely seeking homage and payments. MacBeth, who had equal if not stronger claim to the throne, became king of Scotland in 1040 and ruled for 17 years, that were described as years of bounty in the Annuls of Ulster and earned a reputation for generosity. He was defeated by Duncan's son, Malcolm III, in 1057 who led an army of Saxons into Scottish lands, thus ending (until Robert the Bruce) any true Scottish independence from English political manipulation. MacBeth was slandered by Shakespeare (and Holinshed), to justify the continued subjugation of Scotland as a client state of England, this time in the personage of James I of England (James VI of Scotland).
It is important to remember that James' legitimacy was being questioned on several fronts and that his assassination had just been attempted in the Gunpowder Plot hatched by English Catholics. When a leader of the plot, Guy Faulkes, was tortured and asked why he wanted to blow up Parliament, he said "to blow your Scotch beggar back to his native mountains". Clearly, coming from a line of stewards, not true nobility, was seen as a problem, which the Stuarts sought to solve with the invention of Banquo and slander of other lines of Scottish royal blood.
Sadly, much of Shakespeare's histories and some tragedies (like Macbeth), when viewed in the light of English politics of the time, comes through as pure propaganda. This is the only way that his troup of actors could become "the kings men".
total C**P the lot of it
No one knows precisely why Shakespeare wrote the play "Macbeth," but it is conjectured that he wrote it for a celebration of King Jame's ancestors, and that the material Shakespeare used as the basis for the play was Holinshed's Chronicles, a history of Great Britain. In that book, a person by the name of Donwald exacts revenge on King Duff, who had killed Donwald's family for having contact with witches. Holinshed's Chronicles frequently mixed history with entertainment.
Most people viewed witches as entertainment, some viewed them as serious creatures, and King James himself had seen many earlier plays with witches and spirits.
To pleae his patron James I. He included Banquo as the line of kings and James I was related to Banquo. He included magic and sourcery in the play and James was very interested in this subject, even to the point of writing a book about it.