How to implement derived function?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Philipp
am 7 Apr. 2014
Bearbeitet: per isakson
am 7 Apr. 2014
Hello, I have the following simple code. The abstract class DistributionBase declares a probability distribution, and the derived class UniformDistribution implements uniform distribution on [0,1]. The following code (from the command line) produces an error:
UDF = UniformDistribution (); d = UDF.distributionFunction(3); Error using UniformDistribution/distributionFunction Too many input arguments.
The code for the two classes is below.
Thanks in advance for help! Philipp
classdef (Abstract) DistributionBase methods (Abstract = true) d = density(theta) d = distributionFunction(theta) end end
...
classdef UniformDistribution < DistributionBase
methods
function d = density(theta)
if(theta < 0 || theta > 1)
d = 0;
return;
else
d = 1;
end
end
function d = distributionFunction(theta)
if(theta < 0)
d = 0;
return;
elseif(theta > 1)
d = 1;
return;
else
d = theta;
return;
end
end
end
end
0 Kommentare
Akzeptierte Antwort
per isakson
am 7 Apr. 2014
Bearbeitet: per isakson
am 7 Apr. 2014
You missed obj in distributionFunction( obj, theta). Try
>> udf = UniformDistribution;
>> d = udf.distributionFunction(3)
d =
1
where
classdef UniformDistribution
methods
function d = density( obj, theta)
if(theta < 0 || theta > 1)
d = 0;
else
d = 1;
end
end
function d = distributionFunction( obj, theta)
if(theta < 0)
d = 0;
elseif(theta > 1)
d = 1;
else
d = theta;
end
end
end
end
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Software Development Tools 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!