MATLAB: How to display the tangent of a plot, which intersects the x-axis at a specified point

MATLABplottangent

I have a non-linear plot between two variables, which is created as shown below:
gam6 = 0:0.001:0.025;
tau6 = 7.*(gam6-20.*gam6.^2);
figure
plot(gam6,tau6)
title('\tau_6 vs. \gamma_6')
ylabel('\tau_6')
xlabel('\gamma_6')
There is a point on the x-axis, located at (-0.035,0), and I'd like to show the tangent line of the plot, which also intersects the point on the x-axis. Ideally, I'd also like to know where on the plot this tangent line hits (specific x y coordinates). Is this something I can do in Matlab?
A screenshot of my plot can be seen below, where the red * on the x-axis is the desired intersection point of the tangent line.

Best Answer

gam6 = 0:0.001:0.025;
tau6 = 7.*(gam6-20.*gam6.^2);
pts = [-0.035,0] ;
figure
hold on
plot(gam6,tau6,'*-b')
plot(pts(1),pts(2),'*r')
title('\tau_6 vs. \gamma_6')
ylabel('\tau_6')
xlabel('\gamma_6')
%%Draw tangnet
dy=gradient(tau6)./gradient(gam6) ;
tang = zeros(length(gam6)) ;
A = zeros(length(gam6),1) ;
for i = 1:length(gam6)
tang(i,:)=(gam6-gam6(i))*dy(i)+tau6(i) ;
hold on
plot(gam6,tang(i,:))
A(i) = (dy(i)*gam6(i)-tau6(i))/dy(i) ;
end
idx = knnsearch(A,pts(1),'k',2) ;
fprintf('The required tangent lies between points %d and %d\n',idx(1),idx(2))