How to plot a tangent line between two lines
Show older comments
I'm attempting to plot a tangent line to the red curve, but the tangent line must begin at the pinch point on the yellow line, which has been highlighted in black.

% Required Data
G = 1.356; L = 1.356; CL = 4.187*10^3; TL2 = 43.3; TL1 = 29.4;
% Red line data
x = linspace(TL1-10,TL2,1000);
y = 20312*exp(0.0524*x);
% Calculation to optain the yellow line data
H1 = 0.0165; TG1 = 29.4;
Hy1 = (((1.005+(1.88*H1))*1000)*(TG1 - 0)) + ((2.501*10^6)*H1);
Hy2 = ((L*CL*(TL2 - TL1))/G) + Hy1;
P1 = [TL1 Hy1];
P2 = [TL2 Hy2];
% Values for the yellow line
x_values = [TL1 TL2];
y_values = [Hy1 Hy2];
I want it to be like this black line

2 Comments
Matt J
on 31 Oct 2021
How do you define when something is "tangent" to a discretely sampled curve?
Abdullah Alhebshi
on 31 Oct 2021
Edited: Abdullah Alhebshi
on 31 Oct 2021
Accepted Answer
More Answers (1)
Here is how it can be done with a relatively simple polyfit() and polyval() fcns along with syms and diff():
% Required Data
G = 1.356; L = 1.356; CL = 4.187*10^3; TL2 = 43.3; TL1 = 29.4;
% Red line data
x = linspace(TL1-10,TL2,1000);
y = 20312*exp(0.0524*x);
% Calculation to optain the yellow line data
H1 = 0.0165; TG1 = 29.4;
Hy1 = (((1.005+(1.88*H1))*1000)*(TG1 - 0)) + ((2.501*10^6)*H1);
Hy2 = ((L*CL*(TL2 - TL1))/G) + Hy1;
P1 = [TL1 Hy1];
P2 = [TL2 Hy2];
% Values for the yellow line
x_values = [TL1 TL2];
y_values = [Hy1 Hy2];
TL2 = 43.3; TL1 = 29.4;
x = linspace(TL1-10,TL2,1000);
y = 20312*exp(0.0524*x);
plot(x, y, 'b-x', 'LineWidth', 0.5);
hold on
FM = polyfit(x,y, 2); % Polynomial fit
syms z
FM_sym = FM(1)*z^2+FM(2)*z+FM(3);
dY = diff(FM_sym,z);
dY_Model = double(coeffs(dY));
z = x-TL1;
m = polyval(fliplr(dY_Model), TL1); % Slope
HY1 =polyval(FM, TL1); % y1 value
yy = m*z+HY1*1; % Tangent Line
plot(x, yy, 'k-', 'linewidth', 2), shg
legend('Fcn', 'Tangent Line', 'location', 'best')
hold off
Categories
Find more on Calculus in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
