Vectorized parametric step function
5 次查看(过去 30 天)
显示 更早的评论
I have implemented a parametric step function using object orientation and vectorization code but there is an inconstancy in some results. The code is as follows:
classdef A6
properties
eps
end
methods
function Y = value(obj,X)
Y=double(((X>=0) & (X<0.3)))...
+double((X>=0.3) & (X<0.4)).*obj.eps...
+double((X>=0.4) & (X<0.6))...
+double((X>=0.6) & (X<0.7)).*obj.eps...
+double((X>=0.7) & (X<=1));
end
end
end
I tested the function using, eps=0.8, dx=1/20 and X=(dx:dx:1-dx). When calculating Y=value(a,X), X(12)=0.6 but Y(12)=1 when I should expect the result to be 0.8 according to the previous inequalities. Any help will be appreciated. Thanks.
0 个评论
回答(1 个)
Abolfazl Chaman Motlagh
2021-9-10
it is not about your code.
i test it here :
X = 0.05:0.05:1-0.05
X(12)
X(12)<0.6
this result is wrong.
because :
X(12) - 0.6
so the way you create the X have a small telorance.
do this :
X = round( (0.05:0.05:1-0.05) * 100) / 100 ;
Y=double(((X>=0) & (X<0.3)))...
+double((X>=0.3) & (X<0.4)).*0.8...
+double((X>=0.4) & (X<0.6))...
+double((X>=0.6) & (X<0.7)).*0.8...
+double((X>=0.7) & (X<=1));
plot(X,Y)
Y(X==0.6)
-----------------------------------------------
it is strange because if you use linspace function, other part of code goes wrong:
X = linspace(0.05,1-0.05,19)
Y = double(((X>=0) & (X<0.3)))...
+double((X>=0.3) & (X<0.4)).*0.8...
+double((X>=0.4) & (X<0.6))...
+double((X>=0.6) & (X<0.7)).*0.8...
+double((X>=0.7) & (X<=1));
plot(X,Y)
X(8) , Y(8)
which is wrong, it should be 1.
i think it's error in the way matlab create vectors.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!