Create a function that will return the cdf at any given point to implement the bisection code.

조회 수: 11 (최근 30일)
Trying to implement a random sampling algorithm via the transformation S = Fs-1(U) where U~U(0,1).
My given pdf is c*e^-x^3 for x>0, o for x<0. Solved c to be 1.1198. I used numerical integration to find the cdf, ~0.999958. But now I do not know how to invert the cdf to create random sampling. I tried to set up the bisection method below, to find a value x where h(x) = u, maintain values xmin and xmax where h(xmin) < u and h(xmax) > u. A point x is chosen at the midpoint between xmin and xmax. Based on the value of h(x), either xmin or xmax is updated to halve the difference between them, but I do not know how to properly do it.
I need to create a function that will return the cdf at any given point and ultimately plot an estimated pdf from these samples.
syms y
expr = 1.1198*exp(-y.^3);
F = int(expr,0,inf);
h= F;
u = 1;
xmin = 1.5;
xmax = 3;
tol = 0.000000000001;
while xmax - xmin > tol
x = (xmax+xmin)/2 ;
if h > u
xmax = x ;
else
xmin = x ;
end
XX=x
end
end
fprintf('It is %g\n', x)

채택된 답변

Torsten
Torsten 2022년 12월 3일
편집: Torsten 2022년 12월 4일
syms c x real
f = c*exp(-x.^3);
cnum = solve(int(f,x,0,Inf)==1,c);
f = subs(f,c,cnum);
F = int(f,0,x);
F = matlabFunction(F);
y = rand(500,1);
Finv = arrayfun(@(y)fsolve(@(x)F(x)-y,1,optimset('Display','off')),y);
Finv = Finv(abs(imag(Finv))<1e-8);
hold on
x = 0:0.1:5;
fnum = matlabFunction(f);
plot(x,fnum(x))
histogram(Finv,'Normalization','pdf')
hold off

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by