plot a tangent line of zero point
6 views (last 30 days)
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
@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.
Accepted Answer
Star Strider
on 10 Apr 2023
That should work, and it appears to be appropriate.
Try something like this —
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
slopp=polyfit(x,y,1)
x1=x;
y1=polyval(slopp,x1);
figure
plot(x,y,'-')
hold on
plot(x1,y1)
hold off
title('Original')
ofst = 1; % Choose Appropriate Value For 'ofst' ('Offset')
figure
plot(x,y,'-')
hold on
plot(x1,y1+ofst)
hold off
title('Offset')
.
2 Comments
Star Strider
on 11 Apr 2023
Sure!
All that is necessary is to concatenate 0 to ‘x1’ in the polyval and plot calls —
% 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
slopp=polyfit(x,y,1)
x1=x;
y1=polyval(slopp,[0;x1]);
figure
plot(x,y,'-')
hold on
plot([0;x1],y1)
hold off
title('Original')
ofst = 1; % Choose Appropriate Value For 'ofst' ('Offset')
figure
plot(x,y,'-')
hold on
plot([0;x1],y1+ofst)
hold off
title('Offset')
If you want the regression to begin at the origin, that requires a slightly different approach —
B = x\y % Force Origin Intercept
x1 = [0;x];
y1 = B*x1;
figure
plot(x,y,'-')
hold on
plot(x1,y1)
hold off
title('Original (Origin Intercept)')
figure
plot(x,y,'-')
hold on
plot(x1,y1+ofst)
hold off
title('Offset')
Obviously, the ‘Offset’ line is not going to go through (0,0).
Also, the easiest way to calculate the numerical derivative (in the context that you are using it here) is:
dydx = gradient(y) ./ gradient(x);
That automatically accounts for the intervals of ‘x’ not being constant.
.
More Answers (0)
See Also
Categories
Find more on Measurements and Feature Extraction 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!