When you calculate the gradient:
Then your m will have the same dimension as your xq and yq arrays. So when you want to calculate the tangent line from the point [xq(3),yq(3)] you need to use the corresponding element of your gradient array - that is the 3rd. You just have a typo in your code:
tangent1 = m(3)*(xq-xq(3))+yq(3);
There you'll take the 3rd element out of m, and it should really be the 3rd element out of yq not out of y.
You should also avoid making it a habit of having numbered variables like tangent1 (why 1? At least make it 3?), start adapting to storing things like that in arrays that you can index to with a number later in your code instead of having numbered variables. Here you could chose between a cell-array for your tangents (+: you can use tangent-lines with different number of elements. -: slightly more cumbersome to use.) or a simple matrix (+: easier to compare different tangents, and displaying them at once. -: all better be same sized.)