フィルターのクリア

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

1 回表示 (過去 30 日間)
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

採用された回答

Matt J
Matt J 2022 年 2 月 14 日
編集済み: Matt J 2022 年 2 月 14 日
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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by