フィルターのクリア

plot a tangent line of zero point

4 ビュー (過去 30 日間)
Mark Sc
Mark Sc 2023 年 4 月 10 日
コメント済み: Star Strider 2023 年 4 月 11 日
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 件のコメント
Star Strider
Star Strider 2023 年 4 月 10 日
@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
Mark Sc 2023 年 4 月 10 日
Thanks for your reply.
Actually it is a data from experimental work... I just wanna to draw a tangent line
I tried the following code and it seems ok.. I just wanna a bit of translation of the tangent line as attached titled desired_fig
Hope you could understand what I wanna and help me!
Wanna the same line but a bit translated (as presented in the bold blue line)
Looking to hearing from you
slopp=polyfit(x,y,1)
x1=x;
y1=polyval(slopp,x1)
figure
plot(x,y,'-')
hold on
plot(x1,y1)
hold off
Thanks,

サインインしてコメントする。

採用された回答

Star Strider
Star Strider 2023 年 4 月 10 日
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 = 1508
idx-1
ans = 1507
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)
slopp = 1×2
3.4580 0.6735
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 件のコメント
Mark Sc
Mark Sc 2023 年 4 月 11 日
Thank you so much.. it works fine now, however, May I ask the possbility to have the tangent curve starts from from zero,
I tried with your previous post slope =x(:)\y(:)
but gives me only a horizontal line
Star Strider
Star Strider 2023 年 4 月 11 日
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 = 1508
idx-1
ans = 1507
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)
slopp = 1×2
3.4580 0.6735
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
B = 3.6013
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.
.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMeasurements and Feature Extraction についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by