Created a class fucntion but I am not able to find output for certain conditions even though the value exists.
1 view (last 30 days)
Show older comments
Samson David Puthenpeedika
on 14 Feb 2022
Commented: Samson David Puthenpeedika
on 17 Feb 2022
I have attached data file "air.mat"
1st column is energy (MeV)
2nd column is mass attenuation coefficient
calculated 3rd column as Attenuation coefficient
I tried to run the code in command window as follows :-
obj=material("air",0.0012);
AttenuationCoefficient=getAttenuationCoefficient(obj,0.060)
the code is giving out put for all values of energy (MeV) except 0.060,0.061,0.059 MeV
classdef material
properties (Access=public)
name
density
data_new
energy
mass_attenuation_coefficent
end
methods
function obj=material(name,density)
obj.name=name;
obj.density=density;
filetype=".mat";
text=strcat(obj.name,filetype);
data_1=load(text);
T = table(data_1.data(:,1),data_1.data(:,2),'VariableNames',{'Energy','mass_attentuation_coefficient'});
G = groupsummary(T,"Energy","mean");
a=G.Energy(1,1);
b=G.Energy(end,1);
c=a:a/10:b;
z=interpn(G.Energy,G.mean_mass_attentuation_coefficient,c,'spline');
obj.data_new=[c',z',c'.*z'];
obj.energy=obj.data_new(:,1);
obj.mass_attenuation_coefficent=obj.data_new(:,2);
end
end
methods
function AttenuationCoefficient=getAttenuationCoefficient(obj,energy)
row = obj.data_new(:,1) == energy;
AttenuationCoefficient=obj.data_new(row,3);
end
end
end
0 Comments
Accepted Answer
Matt J
on 14 Feb 2022
Edited: Matt J
on 14 Feb 2022
Tsk, tsk. You should know better than to test for exact equality with floating point values.
>> obj.energy(591)
ans =
0.0600
>> obj.energy(591)-0.060
ans =
6.9389e-18
Here is how I would implement the method,
function AttenuationCoefficient=getAttenuationCoefficient(obj,energy)
AttenuationCoefficient=interp1( obj.data_new(:,1), obj.data_new(:,3), energy);
end
More Answers (0)
See Also
Categories
Find more on Matrix Indexing 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!