How to set a tolerance?
Show older comments
I'm trying to set a tolerance and it's not working. The values for abs(pm0_1-rmp) should return a number smaller than my very large tolerance, but my code still says that the difference between the two is larger than the tolerance. I cannot find what's wrong with my code.
clc
clear all
data_1 = load('DataSet2_Test.txt');
%Characterize columns
t1_1 = data_1(:,1)/1000; % time s
%convert from bits to V
resolution = 2^10-1;
%Data as assigned pairs *10^8 to transfer within the MeV to GeV range
D1_1 = (data_1(:,2)*5)/resolution; D2_1 = (data_1(:,3)*5)/resolution;
D3_1 = (data_1(:,4)*5)/resolution; D4_1 = (data_1(:,5)*5)/resolution;
D5_1 = (data_1(:,6)*5)/resolution; D6_1 = (data_1(:,7)*5)/resolution;
D7_1 = (data_1(:,8)*5)/resolution; D8_1 = (data_1(:,9)*5)/resolution;
%Test 1
%Proton detector
for ap_1 = 1:length(D8_1)
T_1 = [];
compare_1 = D8_1(ap_1);
T_1(1) = compare_1;
T_1(2) = t1_1(ap_1);
index_1 = 2;
for bp_1 = ap_1+1:length(D2_1)
if abs(D2_1(bp_1) - compare_1) < 1e-8
index_1 = index_1 + 1;
T_1(index_1) = t1_1(bp_1);
end
end
full_T_1{ap_1} = T_1;
end
%for 1
pV_repeat_1 = full_T_1{2}(1); %V
pt_ini_1 = full_T_1{2}(2); %t
pt_repeat_1 = full_T_1{2}(3); %t
pdelt_1 = full_T_1{2}(3)-full_T_1{2}(2); %s
%for all
for ap_1 = 1:length(D8_1)
T_1 = full_T_1{ap_1};
if numel(T_1) > 2
pV_rep_1(ap_1) = T_1(1);
pt_in_1(ap_1) = T_1(2);
pt_rep_1(ap_1) = T_1(3);
ptofall_1(ap_1) = pt_rep_1(ap_1) - pt_in_1(ap_1);
else
pV_rep_1(ap_1) = T_1(1);
pt_in_1(ap_1) = T_1(2);
pt_rep_1(ap_1) = NaN;
ptofall_1(ap_1) = NaN;
end
end
ptofall_1;
c = 299792458;
pm0_1 = (D8_1(ap_1)*10^(22))./(ptofall_1.*c*0.10).^2
rmp = 938.272*10^6; %eV/c^2
tolp = 10^30;
if abs(pm0_1-rmp) < tolp;
y = sum(abs(pm0_1-rmp) <= tolp);
disp(y)
fprintf('Protons detected: %3.2f \n', y)
else abs(pm0_1-rmp) > tolp;
fprintf('No Protons detected')
end
abs(pm0_1-rmp)
Accepted Answer
More Answers (1)
Steven Lord
12 minutes ago
0 votes
You indicated you've resolved your problem, but depending on your exact goal some tools that might help you with problems like this in the future:
- discretize
- histcounts
- isapprox
- ismembertol
- uniquetol
Categories
Find more on Logical 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!