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:

 Réponse acceptée

Star Strider
Star Strider le 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

18 commentaires

Gulfer Ozcetin
Gulfer Ozcetin le 27 Oct 2018
Very helpful, much appreciated!
Star Strider
Star Strider le 27 Oct 2018
As always, my pleasure!
Gulfer Ozcetin
Gulfer Ozcetin le 27 Oct 2018
Hello,
Thanks for your help again, if you don't mind; I have another question regarding to this topic. Tried getting the tangent line a bit longer (make it has an intersection on y axis - or imagine it has no boundaries and goes to infinity - in other words) but could not manage to do it... Sorry if I am asking weird questions; I am still trying! :')
No worries!
To extend the tangent line to the full range of ‘t’, the ‘f’ calculation becomes:
f = [t ones(size(t))] * b; % Calculate Tangent Line
and the plot code becomes:
figure
plot(t, y)
hold on
plot(t, f, '-r') % Tangent Line
plot(t(idx), y(idx), '.r') % Maximum Vertical
hold off
grid
No other changes to my code are necessary.
Gulfer Ozcetin
Gulfer Ozcetin le 27 Oct 2018
too many thanks to you! It saved my life!
Star Strider
Star Strider le 27 Oct 2018
As always, my pleasure!
Abdulkader Alquwatli
Abdulkader Alquwatli le 26 Sep 2019
can you explain mathematicly lines 3 to 7 in more detials thanks
Star Strider
Star Strider le 26 Sep 2019
The gradient function needs to have a uniform step size and needs to know the correct value for best results. The ‘h’ calculation does that. The inflection point will be the maximum of the gradient vector, and it is necessary to know the index of that value in order to correctly draw the tangent line. The ‘b’ assignment calculates the linear regression parameters. (I could have calculated the ‘b’ value (linear regression parameters) using polyfit. Using the mldivide function is faster.) The ‘tv’ value calculates the independent variable ‘t’ limits to draw the tangent line, and ‘f’ calculates it for the plot. That is straightforward vector-matrix multiplication.
Sergey Selivanov
Sergey Selivanov le 27 Déc 2019
Déplacé(e) : John D'Errico le 13 Nov 2024
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('Определение динамического запаздывания'); % Построение графика
Mirel Buzila
Mirel Buzila le 11 Mar 2020
The gradient applied here isn't the same as the impulse response of that particular transfer function?
Star Strider
Star Strider le 11 Mar 2020
Déplacé(e) : John D'Errico le 13 Nov 2024
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.
Naveed Mazhar
Naveed Mazhar le 27 Nov 2020
Thanks for this wonderful work. Just update Line-7 by following
tv = [-b(2)/b(1); (max(y)-b(2))/b(1)]; % Independent Variable Range For Tangent Line Plot
Because step response does not necessarily ends at 1 for open loop system.
Shantanu Jahagirdar
Shantanu Jahagirdar le 12 Juil 2021
I need some help, I get the error "Array indices must be positive integers or logical values" for the line "b = [t([idx-1,idx+1]) ones(2,1)] \ y([idx-1,idx+1]);", I checked and for my curve I get the value of idx as 1, any idea how do I solve this? @Star Strider@Naveed Mazhar@Gulfer Ozcetin
Star Strider
Star Strider le 12 Juil 2021
Shantanu Jahagirdar — If ‘idx’ is 1, then ‘idx-1’ will be 0. In MATLAB, array subscripts must be integers greater than 0.
Ali Gharekhani
Ali Gharekhani le 24 Déc 2021
That was so helpful, thank you.
Star Strider
Star Strider le 25 Déc 2021
Ali Gharekhani — Thank you!
.
Arkadiy Turevskiy
Arkadiy Turevskiy le 12 Nov 2024
Modifié(e) : Arkadiy Turevskiy le 12 Nov 2024
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
Star Strider le 13 Nov 2024
@Arkadiy Turevskiy — Interesting!
I’ve used the System Identification Toolbox relatively often, although not procest so far. I’ll keep it in mind.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by