And Jacob begat Joseph the husband of Mary, of whom was born Jesus, who is called Christ.
And Jesus himself began to be about thirty years of age, being (as was supposed) the son of Joseph, which was the son of Heli,
The contradiction in the references about the different fathers of Jospeh is caused by the general assumption that the Joseph mentioned in these two gospels is one and the same person, i.e. the husband of Mary, the mother of Jesus.
For a solution to this apparent contradiction one must recognize that the genealogies given in Matthew and Luke are different in various points. They obviously serve different purposes and show that Jesus Christ is the son of David and the son of Abraham (Matthew) as well as the son of man (Luke). In Matthew therefore the genealogy leads from Abraham down to Jesus, in Luke however it goes back from Jesus beyond Abraham all the way back to the first man, Adam. In Matthew we have the lineage of Jesus' ancestors through his mother Mary, in Luke however we have the genealogy via his supposed father Joseph. Both genealogies go back to king David, but through different sons of David. In Matthew, the rightful claim of Jesus to the throne of David is emphasized because he was by the descent of his mother Mary of the royal lineage of David (through Solomon); in Luke it is shown that even Joseph, Jesus' "supposed father" was of the house of David (through David's son Nathan).
That Matthew gives the genealogy of the lineage through Mary (and not that of her husband Joseph) becomes clear from the text itself which traces the line from Abraham to Jesus in 3 x 14 generations (Matthew 1:17). If one puts the generations mentioned in their proper order, there are 14 generations from Abraham to David; then from Solomon to Jechonias again 14 generations, but from Salathiel to Christ there would then only 13 (!) generations if the Joseph mentioned was the husband of Mary, but 14 generations if the Joseph mentioned was the father of Mary.
The word for "husband" in Matthew 1:16 in the Greek text (aner) and in the Aramaic text (gavra) first and foremost only designates an adult male person, and it can then according to the context be further defined (cp. Luke 24:19 -- prophet; Acts 3:14 -- murderer; Romans 7:2 -- husband, etc.) In Matthew 1:19, the Aramaic text uses a different word for "husband" (bala) which is the proper word for "husband". From the statement in Matthew 1:17 about the 14 generations in each of the 3 divisions of the genealogy, it is clear that the "Joseph" in Matthew 1:16 ("the husband of Mary") could not have been her husband but must have been her father, and it would have been more accurate to translate the words used in Greek and Aramaic as "father".
This way the records in Matthew are in harmony with each other, and the apparent contradiction between Matthew and Luke regarding the different fathers of Joseph is solved. Matthew speaks of Joseph, the father of Mary whose father was Jacob; Luke mentions Joseph, the husband of Mary whose father's name was Heli.
Some other explanations given do perhaps explain the different "fathers" of Joseph, but still don't solve the problem with the 14 generations mentioned, and therefore cannot be correct. They also rest on the assumption that Matthew gives the genealogy of Joseph, and Luke gives the lineage of Mary. In Luke however, Mary isn't even mentioned in his genealogy, and on the other hand Joseph, Mary's husband, is not mentioned in the genealogy as the above study has shown. In reality, it is just opposite, and in Matthew we have the lineage through Mary, whereas Luke gives the lineage of Jesus's supposed father Joseph.