How to find inverse of a self written function?
2 次查看(过去 30 天)
显示 更早的评论
I have written a function cdf_func to calculate cdf of a variable. When I give "llr" as an argument, the function returns "h" as answer. What should I do to find an inverse of this function, such that when I give "h" as an argument to this inverse function, "llr" should be returned as answer.
function result = cdf_func(llr)
var1=2;
a=-0.5*((1/var1)-1);
b=-0.5*log(var1);
x=(llr-b)/a;
x1=(-llr-b)/a;
h=zeros(1,length(llr));
for k=1:length(llr)
if (llr >= abs(b))
h=normcdf(sqrt(x),0,var1)-normcdf(-sqrt(x),0,var1);
elseif(llr<abs(b))
h=2*(normcdf(sqrt(x),0,var1)-normcdf(sqrt(x1),0,var1));
end
end
result = h;
return;
Thanks and Regards, Sumedha
0 个评论
回答(1 个)
Hugo
2014-5-7
You can do the following
sqrtllrh=fminsearch(@(sqrtllr)(cdf_func(sqrtllr.^)-h)^2,1);
llrh=sqrtllrh^2;
The explanation is as follows:
1) cdf_func as you defined it is monotonous increasing, and therefore it can be inverted. 2) However, it is not well defined for negative values. 3) The function fminsearch will look for the minimum of a function. 4) What I did then was to construct a function for which the minimum occurs at the value llr that you are looking for. That function is (cdf_func(llr)-h)^2. The minimum will occur at the point in which cdf_func(llr)=h and the function will give you the value of llr. 5) But, as I mentioned before, since your function does not handle negative values, and fminsearch does not know that, I instead used the function (cdf_func(sqrtllr^2)-h)^2, where sqrtllr is the square root of the value of llr. That guarantees that the function will always get positive values. 6) The last value inside the function is completely arbitrary and it is the starting point where fminsearch starts looking for the minimum. 7) After the function finishes, the value that you are looking for will be the square of what the function gives you as an answer.
Hope this helps.
1 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!