How to plot a tangent line between two lines

15 views (last 30 days)
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
Matt J on 31 Oct 2021
How do you define when something is "tangent" to a discretely sampled curve?
Abdullah Alhebshi
Abdullah Alhebshi on 31 Oct 2021
Edited: Abdullah Alhebshi on 31 Oct 2021
% The red line equation is:
TL2 = 43.3; TL1 = 29.4;
x = linspace(TL1-10,TL2,1000);
y_red = 20312*exp(0.0524*x);
% The yellow line equation is:
y_yellow = 4187*x-51372

Sign in to comment.

Accepted Answer

Paul
Paul on 31 Oct 2021
Can use the Symbolic Math Toolbox for the exact result
syms x real
y_yellow(x) = 4187*x - 51372;
y_red(x) = 20312*exp(0.0524*x);
TL1 = sym(29.4); TL2 = sym(43.3);
x1 = TL1 - 10;
y1 = y_yellow(x1);
y_redslope(x) = diff(y_red(x),x);
syms m real
y_black(x,m) = m*(x - x1) + y1;
sol = solve([y_black(x,m) == y_red(x), m == y_redslope(x)],[x m]);
sol.x;
sol.m;
double(sol.x)
ans = 2×1
33.6817 -10.7272
Looks lik we want the first solution, because sol.x(1) > TL1 - 10
figure; hold on;
fplot(y_red(x),double([TL1-10 TL2+10]),'r')
fplot(y_yellow(x),double([TL1-10 TL2+10]),'g') % make it green for better visibility
fplot(y_black(x,sol.m(1)),double([TL1-10 TL2+10]),'k')
Make y_black into a numeric function if desired
y_black_func = matlabFunction(y_black(x,sol.m(1)))
y_black_func = function_handle with value:
@(x)exp(lambertw(0,exp(-2.01656).*(-1.46986018117369))+2.01656).*(x-9.7e+1./5.0).*1.0643488e+3+2.98558e+4
Or more simply expressed
y_black_func = matlabFunction(y_black(x,double(sol.m(1))))
y_black_func = function_handle with value:
@(x)x.*6.216966742866688e+3-9.075335481161375e+4

More Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 31 Oct 2021
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

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!