Vectorized parametric step function

5 次查看(过去 30 天)
Pablo C.
Pablo C. 2021-9-10
评论: Pablo C. 2021-9-10
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.

回答(1 个)

Abolfazl Chaman Motlagh
it is not about your code.
i test it here :
X = 0.05:0.05:1-0.05
X = 1×19
0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500
X(12)
ans = 0.6000
X(12)<0.6
ans = logical
1
this result is wrong.
because :
X(12) - 0.6
ans = -1.1102e-16
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)
ans = 0.8000
-----------------------------------------------
it is strange because if you use linspace function, other part of code goes wrong:
X = linspace(0.05,1-0.05,19)
X = 1×19
0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500
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)
ans = 0.4000
ans = 0.8000
which is wrong, it should be 1.
i think it's error in the way matlab create vectors.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by