How to draw the tangent line on a curve

I would like to plot the tangent of the curve I (V) and find the slope and then the intersection of the tangent with the x axis (V0).
I am attaching the curve data (I, U) and a summary image of what I am looking for. Thank you in advance for your help

 Accepted Answer

Try this —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/668953/Tan.xlsx')
T1 = 42×2 table
I V ______ ________ 0 0 0 0.064516 0 0.14337 0 0.20072 0 0.36559 0 0.45878 4.6681 0.55197 19.593 0.63799 40.963 0.73118 58.96 0.7957 79.604 0.86022 114.14 0.9534 138.19 1.0108 164.38 1.0681 181.86 1.1039 207.75 1.1541
I = T1.I;
V = T1.V;
dI = gradient(I);
dV = gradient(V);
Vi = 1.3; % Choose Voltage Value To Calculate Slope
Ii = interp1(V, I, Vi);
dVi = interp1(V, dV, Vi)
dVi = 0.0299
dIi = interp1(V, dI, Vi)
dIi = 18.8851
Slope = dIi/dVi
Slope = 631.9586
YIntercept = Ii - Slope * Vi
YIntercept = -528.9232
XIntercept = -YIntercept / Slope
XIntercept = 0.8370
figure
plot(V,I)
hold on
plot(Vi, Ii, 'p')
plot(xlim, xlim*Slope+YIntercept, '-r')
plot(XIntercept, 0, 'sr')
hold off
grid
xlabel('V')
ylabel('I')
text(Vi, Ii, sprintf('$I = %.3f \\times V %+.3f \\rightarrow \\ $', Slope, YIntercept), 'Horiz','right', 'Vert','middle', 'Interpreter','latex')
text(XIntercept, 0, sprintf('$%.4f V \\downarrow $', XIntercept), 'Horiz','right', 'Vert','bottom', 'Interpreter','latex')
ylim([0 max(ylim)])
.

6 Comments

Thank you very much, it works.
I have a question please: How do I do it if I don't know the shape of the curve at the beginning and I don't know the inflection point.
Because this part will be included in a loop that will calculate the slope and V0 of each model (the different sheets of the Tan2.xls file). I attach the file Tan2.xlsx (I(U) of models 1-3) and your code in the loop
I think I need to find the inflection point of each model to draw their tangent. But I don't know exactly how to find it...
As always, my pleasure!
The inflection point problem was not part of the original request, so
I define ‘inflection point’ as the point where the slope changes sign, for example:
x = linspace(-2, 2, 250);
y = tanh(x);
dx = gradient(x);
dy = gradient(y);
slope = dy./dx;
d2yd2x = gradient(slope);
figure
yyaxis left
plot(x, y, x, slope)
yyaxis right
plot(x, d2yd2x)
grid
legend('$y(x)$','$\frac{dy}{dx}$','$\frac{d^2y}{dx^2}$', 'Location','best', 'Interpreter','latex')
So the inflection point here is at ‘x=0’.
The slope appeared to me to be essentially constant (with respect to ‘V’), so I chose ‘Vi’ arbitrarily on the basis of the supplied plot image. There is one large change in the slope (however not an ‘inflection point’ as I define it) at abouit ‘V=0.5’:
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/668953/Tan.xlsx');
I = T1.I;
V = T1.V;
dI = gradient(I);
dV = gradient(V);
d2Id2V = gradient(dI./dV); % Second Derivative
[~,idx1] = max(abs(d2Id2V))
idx1 = 7
Vi = V(idx1)
Vi = 0.5520
figure
yyaxis left
plot(V, dI./dV)
ylabel('$\frac{dI}{dV}$', 'Interpreter','latex')
yyaxis right
plot(V, d2Id2V)
ylabel('$\frac{d^2I}{dV^2}$', 'Interpreter','latex')
grid
xlabel('$V$', 'Interpreter','latex')
Using this with my earlier code:
Ii = interp1(V, I, Vi);
dVi = interp1(V, dV, Vi)
dVi = 0.0896
dIi = interp1(V, dI, Vi)
dIi = 9.7964
Slope = dIi/dVi
Slope = 109.3283
YIntercept = Ii - Slope * Vi
YIntercept = -55.6780
XIntercept = -YIntercept / Slope
XIntercept = 0.5093
figure
plot(V,I)
hold on
plot(Vi, Ii, 'p')
plot(xlim, xlim*Slope+YIntercept, '-r')
plot(XIntercept, 0, 'sr')
hold off
grid
xlabel('V')
ylabel('I')
text(Vi, Ii, sprintf('$I = %.3f \\times V %+.3f \\rightarrow \\ $', Slope, YIntercept), 'Horiz','right', 'Vert','middle', 'Interpreter','latex')
text(XIntercept, 0, sprintf('$%.4f V \\downarrow $', XIntercept), 'Horiz','right', 'Vert','bottom', 'Interpreter','latex')
ylim([0 max(ylim)])
This is the best I can do with these data.
I have no idea how you want to define the voltage at which to draw the tangent. With more information, I will do my best to help with that.
(I verified that the ‘Tan2.xlsx’ file is a duplicate of the original file, so I am not using it here.)
C3 = readcell('https://www.mathworks.com/matlabcentral/answers/uploaded_files/669983/Tan_Mat.txt');
.
Thank you for your explanations.
My mistake on the understanding of the inflection point, because precisely the slope is constant.
The point of intersection of the tangent with the x-axis must be within [0.7 ; 0.9], the first derivative gives a better estimate of this point.
As always, my pleasure!
I am slightly lost, however. If the objective is to find values of ‘I’ and ‘V’ to produice an x-axis intercept within those limits, a simple search using a for loop would likely work. If the intent is different, a somewhat more sophisticated approach would be necessary.
In your code you have taken Vi =1.3 , did you choose randomly or took a mean value ? Can you explain me.
Or can I choose any value ?
I chose that value for ‘Vi’ since although the exact point was not stated, it was certainly implied in the ‘image001.png’ plot.
So ‘Vi’ can be any point in the curve.

Sign in to comment.

More Answers (1)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Asked:

on 29 Jun 2021

Commented:

on 23 Jan 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!