How to plot a tangent line between two lines
8 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Paul
2021-10-31
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)
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)))
Or more simply expressed
y_black_func = matlabFunction(y_black(x,double(sol.m(1))))
0 个评论
更多回答(1 个)
Sulaymon Eshkabilov
2021-10-31
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!