Plot tangent line on part of the curve
1 view (last 30 days)
Show older comments
Hello all,
I am trying to plot tangent line but only on part of the curve not all, like first 25% or less on the curve.....
Also would like the tangent to start from zero
Then add another tangent a bit on right side...
I attached both file and matlab code, but I could not do it...
Looking to hearing from anyone
clearvars
clc
close all;
data = xlsread('data_tangent.xlsx');
x = data(:,1);
y = data(:,2);
x_n = x-x(1);
y_n = y-y(1);
B = x_n\y_n; % Force Origin Intercept
x1 = [0;x_n];
y1 = B*x1;
fig=figure;
hold on
plot(x_n,y_n)
plot(x1,y1)
Thank you,
0 Comments
Accepted Answer
Star Strider
on 18 Apr 2023
Try this —
Data = readmatrix('data_tangent.xlsx')
x = Data(:,1);
y = Data(:,2);
[ymax,idx] = max(y);
Binit = x(1:5) \ y(1:5)
Bymax = x(idx) \ y(idx)
Line_init = x*Binit;
Line_initv = Line_init <= ymax;
Line_ymax = x*Bymax;
Line_ymaxv = Line_ymax <= ymax;
figure
plot(x, y)
hold on
plot([0;x(Line_initv)], [0;Line_init(Line_initv)], '-r')
plot([0;x(Line_ymaxv)], [0;Line_ymax(Line_ymaxv)], '-r')
% plot(x(idx),y(idx),'r+')
hold off
grid
axis('equal')
xlabel('X')
ylabel('Y')
axis([0 max(x) 0 max(y)])
.
2 Comments
Star Strider
on 18 Apr 2023
My pleasure!
Indices 1:5 are simply the initial part of the curve that is used to calculate the slope constant. They can be anything you want to use to define the initial part of the curve.
My code was designed for the data provided. To use it with other data, I need to see the other data and to know what the tangent lines for it are supposed to be, or at least more generally what the second tangent line criteria are.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!