Intersection between fittetfunction and line

1 view (last 30 days)
Data = readtable('Data.xlsx','Range','A1:S9','VariableNamingRule','modify');
R = sqrt(3) * 267.1;
x = Data.Frequenz;
ZL = Data.MessungZLInOhm;
ZC = Data.MessungZCInOhm;
ZLR = Data.RL;
ZCR = Data.RC;
figure
plot(x,ZL,'rx')
axis([0 1000 0 1000])
xlabel('f in Hz')
ylabel('Z in \Omega')
grid on
hold on
[curveZL] = fit(x,ZL,'poly1');
h1 = plot(curveZL,'r--')
plot(x,ZC,'bx')
[curveZC] = fit(x,ZC,'power1');
h2 = plot(curveZC,'b--')
plot(x,ZLR,'gx')
[curveZLR] = fit(x,ZLR,'poly2');
h3 = plot(curveZLR,'g--')
plot(x,ZCR,'mx')
[curveZCR] = fit(x,ZCR,'power2');
h4 = plot(curveZCR,'m--')
h5 = plot([0 1000], [R R],'c-')
h = legend([h1 h2 h3 h4 h5],{'$Z_L$','$Z_C$','$Z_{LR}$','$Z_{CR}$','$\sqrt(3)R$'});
set(h,'Interpreter','latex','fontsize',10)
hold off
I need to find the intersection between the plots "Z_L and sqrt(3) R aswell with Z_C" i know that shouldnt be that hard. but all answers i find are to complex and do more then i asked or work only for functions i enter. As a absolute matlab beginner i am in way over my head and already had big problems with fitting a curve to 1/x since the most fitting answers i found was with polyfit.
Now all my functions look like i want to and i need to find the intersections. And if possible mark them with a horrizontel line and a point on the x-axes. for example "f_L" at the intersection with Z_L and the line.
Please excuse my bad english and if didn't express my self clearly. If you see something in my code that you would do differently, i am always open to advice.
Greatings

Accepted Answer

Mathieu NOE
Mathieu NOE on 9 Dec 2021
hello Luis
so this my little demo for intersection - the crossing point coordinates are obtained by linear interpolation, so the accuracy is much better compared to methods looking only to the nearest available point.
i did the demo using the measured and not the fiiting models as I don't have the Curve Fitting Toolbox
hope it helps
clc
clearvars
Data = readtable('Data.xlsx','Range','A1:S9','VariableNamingRule','modify');
R = sqrt(3) * 267.1;
x = Data.Frequenz;
ZL = Data.MessungZLInOhm;
ZC = Data.MessungZCInOhm;
ZLR = Data.RL;
ZCR = Data.RC;
% ZL crossing
threshold = R; % your value here
[x0_pos,s0_pos,x0_neg,s0_neg]= crossing_V7(ZL,x,threshold,'linear'); % positive (pos) and negative (neg) slope crossing points
% ind => time index (samples)
% t0 => corresponding time (x) values
% s0 => corresponding function (y) values , obviously they must be equal to "threshold"
figure(1)
plot(x,ZL,'r',x,threshold*ones(size(x)),'k--',x0_pos,s0_pos,'db','linewidth',2,'markersize',12);grid on
legend('ZL','threshold','positive slope crossing points');
text(x0_pos*1.05,threshold*0.95,['f_L = ' num2str(x0_pos) ' Hz']);
% ZC crossing
threshold = R; % your value here
[x0_pos,s0_pos,x0_neg,s0_neg]= crossing_V7(ZC,x,threshold,'linear'); % positive (pos) and negative (neg) slope crossing points
figure(2)
plot(x,ZC,'r',x,threshold*ones(size(x)),'k--',x0_neg,s0_neg,'dg','linewidth',2,'markersize',12);grid on
legend('ZC','threshold','negative slope crossing points');
text(x0_neg*1.05,threshold*1.05,['f_C = ' num2str(x0_neg) ' Hz']);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [t0_pos,s0_pos,t0_neg,s0_neg] = crossing_V7(S,t,level,imeth)
% [ind,t0,s0,t0close,s0close] = crossing_V6(S,t,level,imeth,slope_sign) % older format
% CROSSING find the crossings of a given level of a signal
% ind = CROSSING(S) returns an index vector ind, the signal
% S crosses zero at ind or at between ind and ind+1
% [ind,t0] = CROSSING(S,t) additionally returns a time
% vector t0 of the zero crossings of the signal S. The crossing
% times are linearly interpolated between the given times t
% [ind,t0] = CROSSING(S,t,level) returns the crossings of the
% given level instead of the zero crossings
% ind = CROSSING(S,[],level) as above but without time interpolation
% [ind,t0] = CROSSING(S,t,level,par) allows additional parameters
% par = {'none'|'linear'}.
% With interpolation turned off (par = 'none') this function always
% returns the value left of the zero (the data point thats nearest
% to the zero AND smaller than the zero crossing).
%
% check the number of input arguments
error(nargchk(1,4,nargin));
% check the time vector input for consistency
if nargin < 2 | isempty(t)
% if no time vector is given, use the index vector as time
t = 1:length(S);
elseif length(t) ~= length(S)
% if S and t are not of the same length, throw an error
error('t and S must be of identical length!');
end
% check the level input
if nargin < 3
% set standard value 0, if level is not given
level = 0;
end
% check interpolation method input
if nargin < 4
imeth = 'linear';
end
% make row vectors
t = t(:)';
S = S(:)';
% always search for zeros. So if we want the crossing of
% any other threshold value "level", we subtract it from
% the values and search for zeros.
S = S - level;
% first look for exact zeros
ind0 = find( S == 0 );
% then look for zero crossings between data points
S1 = S(1:end-1) .* S(2:end);
ind1 = find( S1 < 0 );
% bring exact zeros and "in-between" zeros together
ind = sort([ind0 ind1]);
% and pick the associated time values
t0 = t(ind);
s0 = S(ind);
if ~isempty(ind)
if strcmp(imeth,'linear')
% linear interpolation of crossing
for ii=1:length(t0)
%if abs(S(ind(ii))) >= eps(S(ind(ii))) % MATLAB V7 et +
if abs(S(ind(ii))) >= eps*abs(S(ind(ii))) % MATLAB V6 et - EPS * ABS(X)
% interpolate only when data point is not already zero
NUM = (t(ind(ii)+1) - t(ind(ii)));
DEN = (S(ind(ii)+1) - S(ind(ii)));
slope = NUM / DEN;
slope_sign(ii) = sign(slope);
t0(ii) = t0(ii) - S(ind(ii)) * slope;
s0(ii) = level;
end
end
end
% extract the positive slope crossing points
ind_pos = find(sign(slope_sign)>0);
t0_pos = t0(ind_pos);
s0_pos = s0(ind_pos);
% extract the negative slope crossing points
ind_neg = find(sign(slope_sign)<0);
t0_neg = t0(ind_neg);
s0_neg = s0(ind_neg);
else
% empty output
ind_pos = [];
t0_pos = [];
s0_pos = [];
% extract the negative slope crossing points
ind_neg = [];
t0_neg = [];
s0_neg = [];
end
end
  4 Comments
Luis Winter
Luis Winter on 9 Dec 2021
Since i stoped using polyfit i worked. in retrospect thats obvious. i have trouble finding the intersection since most solutions used fzero like this
ffL = @(x) R - curveZL(x);
fL = fzero(ffL, 0);
but since 1/x functions don't have a zero crossing that doesn't work, so i took the manuel route.

Sign in to comment.

More Answers (0)

Categories

Find more on Particle & Nuclear Physics 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!