How to draw a tangent line on a curve
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
1 개 추천
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
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
Gulfer Ozcetin
2018년 10월 27일
Very helpful, much appreciated!
Star Strider
2018년 10월 27일
As always, my pleasure!
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
2018년 10월 27일
too many thanks to you! It saved my life!
Star Strider
2018년 10월 27일
As always, my pleasure!
can you explain mathematicly lines 3 to 7 in more detials thanks
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
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
2020년 3월 11일
The gradient applied here isn't the same as the impulse response of that particular transfer function?
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
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
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
2021년 12월 24일
That was so helpful, thank you.
Star Strider
2021년 12월 25일
Ali Gharekhani — Thank you!
.
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개)
카테고리
도움말 센터 및 File Exchange에서 Linear Model Identification에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
