Vectorized parametric step function
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
Abolfazl Chaman Motlagh
am 10 Sep. 2021
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.
Siehe auch
Kategorien
Mehr zu Performance and Memory finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!