Plot tangent line on part of the curve
    조회 수: 5 (최근 30일)
  
       이전 댓글 표시
    
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
채택된 답변
  Star Strider
      
      
 2023년 4월 18일
        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
  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 Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


