Effacer les filtres
Effacer les filtres

Created a class fucntion but I am not able to find output for certain conditions even though the value exists.

1 vue (au cours des 30 derniers jours)
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

Réponse acceptée

Matt J
Matt J le 14 Fév 2022
Modifié(e) : Matt J le 14 Fév 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

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by