How to draw a tangent line on a curve

655 views (last 30 days)
Gulfer Ozcetin
Gulfer Ozcetin on 26 Oct 2018
Commented: Star Strider on 25 Dec 2021 at 2:04
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:

Accepted Answer

Star Strider
Star Strider on 27 Oct 2018
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
  14 Comments
Star Strider
Star Strider on 25 Dec 2021 at 2:04
Ali Gharekhani — Thank you!
.

Sign in to comment.

More Answers (2)

Sergey Selivanov
Sergey Selivanov on 27 Dec 2019
Dear Star Strider. Very useful information from you.
I wanted to clarify with you. Is this program suitable for plotting the transfer function:
clear
clc
Ra=2; ta=0.3; tm=0.6; Ce=0.005; % исходные данные для расчёта
Wa=tf([1/Ra],[ta 1]) % Передаточная функция для тока якоря (звено первого порядка)
Wm=tf([1/Ce],[tm 1]) % Передаточная функция для механической инерции (звено первого порядка)
Wo=Wa*Wm % Взаимодействие звеньев первого уровня
step(10*Wo,'r'),grid on,title('Определение динамического запаздывания'); % Построение графика
  1 Comment
Star Strider
Star Strider on 11 Mar 2020
I did not see this until now.
The step call must return outputs to use my code:
[y,t] = step(10*Wo);
You can then plot it with the calculated tangent line.

Sign in to comment.


shanu abeyrathne
shanu abeyrathne on 15 Apr 2021
hi guys, i am new to matlab. i need to know how to draw a tangent plane to z at the point (9/2,9/2,sqrt(9/2)).
this is my function,z=@(x,y)sqrt(9-x^2-y^2)

Community Treasure Hunt

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

Start Hunting!