getting an error in this code

32 views (last 30 days)
Bobby on 29 Apr 2022
Answered: Mathieu NOE on 29 Apr 2022
clc
clear
%shipcostpmf.m
sx = (1:8);
px = [0.15*ones(4,1), 0.1*ones(4,1)];
gx = (sx<=5).*(105*sx-5*(sx.^2)) + ((sx>5).*500);
sy = unique(gx);
fprintf('sy =\n');
disp(sy);
py = finitepmf(gx,px,sy);
not sure why my py is getting an error because it says Unrecognized function or variable 'finitepmf'. but finitepmf should be a command?

Mahmoud Ashraf on 29 Apr 2022
you should define funstion of finitrepmf
as shown in this code
clc
clear
%shipcostpmf.m
sx = (1:8);
px = [0.15*ones(4,1), 0.1*ones(4,1)];
gx = (sx<=5).*(105*sx-5*(sx.^2)) + ((sx>5).*500);
sy = unique(gx);
fprintf('sy =\n');
disp(sy);
py = finitepmf(gx,px,sy);
function pmf=finitepmf(sx,px,x)
% finite random variable X:
% vector sx of sample space
% elements {sx(1),sx(2), ...}
% vector px of probabilities
% px(i)=P[X=sx(i)]
% Output is the vector
% pmf: pmf(i)=P[X=x(i)]
pmf=zeros(size(x(:)));
for i=1:length(x)
pmf(i)= sum(px(find(sx==x(i))));
end
end

Voss on 29 Apr 2022
Maybe finitepmf should be defined as follows:
function pmf=finitepmf(sx,px,x)
% finite random variable X:
% vector sx of sample space
% elements {sx(1),sx(2), ...}
% vector px of probabilities
% px(i)=P[X=sx(i)]
% Output is the vector
% pmf: pmf(i)=P[X=x(i)]
pmf=zeros(size(x(:)));
for i=1:length(x)
pmf(i)= sum(px(find(sx==x(i))));
end
end
which was found here:
If that seems right and you haven't done so already, put that code into an m-file called finitepmf.m. If you have done that already, then make sure that finitepmf.m is on your MATLAB search path.

Mathieu NOE on 29 Apr 2022
hi
here you are
sx = (1:8);
px = [0.15*ones(4,1), 0.1*ones(4,1)];
gx = (sx<=5).*(105*sx-5*(sx.^2)) + ((sx>5).*500);
sy = unique(gx);
fprintf('sy =\n');
disp(sy);
py = finitepmf(gx,px,sy)
function pmf=finitepmf(sx,px,x)
% finite random variable X:
% vector sx of sample space
% elements {sx(1),sx(2), ...}
% vector px of probabilities
% px(i)=P[X=sx(i)]
% Output is the vector
% pmf: pmf(i)=P[X=x(i)]
pmf=zeros(size(x(:)));
for i=1:length(x)
pmf(i)= sum(px(sx==x(i)));
end
end

Categories

Find more on Entering Commands in Help Center and File Exchange

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by