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:

 채택된 답변

Star Strider
Star Strider 2018년 10월 27일

11 개 추천

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

Gulfer Ozcetin
Gulfer Ozcetin 2018년 10월 27일
Very helpful, much appreciated!
Star Strider
Star Strider 2018년 10월 27일
As always, my pleasure!
Gulfer Ozcetin
Gulfer Ozcetin 2018년 10월 27일
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 2018년 10월 27일
too many thanks to you! It saved my life!
Star Strider
Star Strider 2018년 10월 27일
As always, my pleasure!
can you explain mathematicly lines 3 to 7 in more detials thanks
Star Strider
Star Strider 2019년 9월 26일
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 2019년 12월 27일
이동: John D'Errico 2024년 11월 13일
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 2020년 3월 11일
The gradient applied here isn't the same as the impulse response of that particular transfer function?
Star Strider
Star Strider 2020년 3월 11일
이동: John D'Errico 2024년 11월 13일
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 2020년 11월 27일
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.
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 2021년 7월 12일
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 2021년 12월 24일
That was so helpful, thank you.
Star Strider
Star Strider 2021년 12월 25일
Ali Gharekhani — Thank you!
.
Arkadiy Turevskiy
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
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개)

카테고리

도움말 센터File Exchange에서 Linear Model Identification에 대해 자세히 알아보기

질문:

2018년 10월 26일

이동:

2024년 11월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by