Plot tangent line on part of the curve

3 ビュー (過去 30 日間)
Mark Sc
Mark Sc 2023 年 4 月 18 日
コメント済み: Star Strider 2023 年 4 月 18 日
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,

採用された回答

Star Strider
Star Strider 2023 年 4 月 18 日
Try this —
Data = readmatrix('data_tangent.xlsx')
Data = 13042×2
1.9803 7.3700 1.9823 7.3500 1.9853 7.3800 1.9873 7.4000 1.9903 7.3100 1.9933 7.5100 1.9953 7.5300 1.9973 7.4300 2.0003 7.5100 2.0022 7.7400
x = Data(:,1);
y = Data(:,2);
[ymax,idx] = max(y);
Binit = x(1:5) \ y(1:5)
Binit = 3.7086
Bymax = x(idx) \ y(idx)
Bymax = 3.2160
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 件のコメント
Mark Sc
Mark Sc 2023 年 4 月 18 日
@Star Strider seems working but may i ask what are the indices of 1:5?
as when I apply it to other data seems not working
Let me know ??
Thanks anyway
Star Strider
Star Strider 2023 年 4 月 18 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by