Ho to find the interception

2 views (last 30 days)
I have a fitted Guassian plot to which I have plotted yline. Now I am interested to extrac two x- interception points through which my line is passing. I am not able to find any easy way to do it. so any help is appreciable.
code:
clc;close all;clear;
resultsdir = 'results';
Row = dir('cross_section_*.mat');
%Coefficients (with 95% confidence bounds):
a1 = 394.5 ;
b1 = 985.1 ;
c1 = 114.8 ;
a2 = 511.4 ;
b2 = 878.5 ;
c2 = 231.2 ;
a3 = -529.8 ;
b3 = 989.8 ;
c3 = 133.6 ;
xData = zeros(1,1640);
yData = zeros(1,1640);
color = ['r','g','b']
for X = 1:9
Filename = Row(X).name;
dest = fullfile(resultsdir, Filename);
Filedata(X) = load(Filename);
figure()
plot(Filedata(X).x)
[xData, yData] = prepareCurveData( [], Filedata(X).x);
ft = fittype( 'gauss3' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.Lower = [-Inf -Inf 0 -Inf -Inf 0 -Inf -Inf 0];
opts.StartPoint = [446.913 842 37.5821195501848 401.619376603545 761 47.8742720232778 393.885950193204 926 55.7197242033307];
[fitresult, gof] = fit( xData, yData, ft, opts );
figure()
h = plot( fitresult,'k', xData, yData);
set(h,'LineWidth', 2)
title(sprintf('Name:%s , R-Square = %0.4f',Filename,gof.rsquare))
legend( h, 'Experiment', 'fit ', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
ylabel('Intensity values')
xlabel( 'Along X of the CMOS', 'Interpreter', 'none' );
grid on
f = fitresult.a1*exp(-((xData-fitresult.b1)/fitresult.c1).^2) + fitresult.a2*exp(-((xData-fitresult.b2)/fitresult.c2).^2) + fitresult.a3*exp(-((xData-fitresult.b3)/fitresult.c3).^2);
Max(X) = max(f)
exp_perc(X) = Max(X)/exp(2);
yline(exp_perc(X))
% Max(X) = max(fitresult.val)
% f = a1*exp(-((Filedata(X)-b1)/c1).^2) + a2*exp(-((Filedata(X)-b2)/c2).^2) + a3*exp(-((Filedata(X)-b3)/c3).^2);
end
plot:

Accepted Answer

Star Strider
Star Strider on 23 Jun 2022
If you simply want to do it yourself, try something like this —
xData = 1:150;
yData = 50*exp(-(xData-75).^2 * 0.01) + randn(1,150);
ft = fittype( 'gauss3' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
[fitresult, gof] = fit( xData(:), yData(:), ft, opts );
h = plot( fitresult,'k', xData, yData);
ylineval = 10; % This Needs To Be Stated Separately From The 'yline' Call
yline(ylineval, 'DisplayName','yline');
hold on
xfit = h(2).XData; % Get Fitted Curve Data
yfit = h(2).YData; % Get Fitted Curve Data
ixv = find(diff(sign(yfit-ylineval))); % Calculate The Approximate Intersection Indicess
for k = 1:numel(ixv)
idxrng = max(1,ixv(k)-1) : min(numel(xfit),ixv(k)+1);
xv(k) = interp1(yfit(idxrng), xfit(idxrng), ylineval); % Interpolate 'Exact' Values
yv(k) = interp1(xfit(idxrng), yfit(idxrng), xv(k));
end
% xv
% yv
plot(xv, yv, 'sr','MarkerSize',10, 'DisplayName','Intersections')
text(10,40,sprintf('Intersections:\nX = %.3f\nX = %.3f',xv))
hold off
.
  2 Comments
Star Strider
Star Strider on 23 Jun 2022
As always, my pleasure!

Sign in to comment.

More Answers (1)

KSSV
KSSV on 23 Jun 2022
  1 Comment
Navaneeth Tejasvi Mysore Nagendra
Thank you for the suggestion, I will take a look

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!