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 CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by