plot a tangent line of zero point
Show older comments
Hi all,
I am kinda new to matlab.. I have a x-y data and would like to plot a zero-point tangent to the curve which I have...
I have checked several old codes, but not working with my data,
Could anyone please help me ?
I already attched the data and code (which I already got it from here, but have no idea why it is not giving me what I wanna ??
Thanks,
clearvars
clc;
close all
%% READ DATA FROM EXCEL
data = xlsread('matlab_1.xlsx','Sheet2');
x = data(:,1);
y = data(:,2);
%% PLOTTING TANGENT
h = mean(diff(x));
dy = gradient(y, x); % Numerical Derivative
[~,idx] = max(dy) % Index Of Maximum
idx-1
b = [x([idx,idx+1]) ones(2,1)] \ y([idx,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(x,y);
hold on
plot(tv, f, '-r') % Tangent Line
plot(x(idx), y(idx), '.r') % Maximum Vertical
hold off
grid
5 Comments
Star Strider
on 10 Apr 2023
Walter Roberson
on 10 Apr 2023
dy = gradient(y, h); % Numerical Derivative
No, that is wrong. You should be using
dy = gradient(y, x); % Numerical Derivative
If you want to then make this into something equally spaced, then use interp1() on dy
Mark Sc
on 10 Apr 2023
Star Strider
on 10 Apr 2023
@Mark Sc — Your data are extremely noisy, and your code happens to choose the maximum slope of the noise. (They are also not sampled even close to uniformly.) The maximum slope is not actually an inflection point, since the data appeare to be approximately linear, simply the maximum slope of a noisy signal. After using resample on the signal (with a sampling frequency of 400) and filtering out the noise (lowpass with a cutoff of 8 and choosing an elliptic filter), the maximum slope is part of the initial transient response at the third data pair. I doubt that there is any other inflection point in the signal, or that the initial transient response could be considered an inflection point, however I do not know what the data are or what you want to do with them.
Mark Sc
on 10 Apr 2023
Accepted Answer
More Answers (0)
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




