
How to draw a tangent line on a curve
조회 수: 144 (최근 30일)
이전 댓글 표시
Hello, I am a MATLAB newbie. I have a transfer function of a process and need to derive a two-parameter model using a tangent line. Now I have the graph of it, all I need to do is getting the "most vertical" tangent line as far as I can do. Just thought choosing a random point on the curve and then writing a piece of code for a tangent line might be useful (for example, it can be (6.5,8)). Couldn't find any answer on plotting a tangent line using a graph that comes from a transfer function, I hope someone can help.
Gs1 = tf([1],[1 5 10 10 5 1],'InputDelay',3) step(Gs1)
and this is how the plot looks like:

댓글 수: 0
채택된 답변
Star Strider
2018년 10월 27일
One option:
Gs1 = tf([1],[1 5 10 10 5 1],'InputDelay',3);
[y,t] = step(Gs1);
h = mean(diff(t));
dy = gradient(y, h); % Numerical Derivative
[~,idx] = max(dy); % Index Of Maximum
b = [t([idx-1,idx+1]) ones(2,1)] \ y([idx-1,idx+1]); % Regression Line Around Maximum Derivative
tv = [-b(2)/b(1); (1-b(2))/b(1)]; % Independent Variable Range For Tangent Line Plot
f = [tv ones(2,1)] * b; % Calculate Tangent Line
figure
plot(t, y)
hold on
plot(tv, f, '-r') % Tangent Line
plot(t(idx), y(idx), '.r') % Maximum Vertical
hold off
grid

댓글 수: 18
Arkadiy Turevskiy
2024년 11월 12일
편집: Arkadiy Turevskiy
2024년 11월 12일
I'd like to add that you can also use System Identification Toolbox for this. It has a function procest for estimating process models.
Here is how you can use it in this case:
Gs1 = tf([1],[1 5 10 10 5 1],'InputDelay',3);
[y,t]=step(Gs1);
z=iddata(y,ones(size(y)),t(2)-t(1)); %create data object with step response,
% step input, and sampling time
model=procest(z,'P1D'); % estimate 1st order process model with delay
compare(data,model)

Star Strider
2024년 11월 13일
@Arkadiy Turevskiy — Interesting!
I’ve used the System Identification Toolbox relatively often, although not procest so far. I’ll keep it in mind.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Response Computation and Visualization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!