Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Newbie - inverse method
Replies: 0

 amarender Posts: 112 Registered: 2/5/09
Newbie - inverse method
Posted: Apr 19, 2012 1:44 PM

Hey guys ,

This is my first time trying to work in matlab , and I need some help

I need to simulate a variable X with a density function and make its histogram.
f(x)=(exp(1)/(exp(2)-1))*exp(x) , x [-1,1]

And i tried with the inverse method
F^-1=log(1+(exp(2)-1)*U)-1

My code
n=100000;
U=rand(1,n);
X=log(1+(exp(2)-1)*U)-1;
d=1/30;
[r,s]=hist(X,30);
r=r/(d*m);
plot(s,r,?o?)
hold on
x=0:0.01:1;
y=(exp(1)/(exp(2)-1))*exp(x);
plot(x,y)
grid
xlabel(?x?)
ylabel(?density function?)

But i have errors and I'm not sure that my interpretation/understanding of the problem is that good , so I found a better code on User's Guide - which I don't understand.

mu = 1;
X = expinv(rand(1e4,1),mu);

numbins = 50;
hist(X,numbins)
set(get(gca,'Children'),'FaceColor',[.8 .8 1])
hold on

[bincounts,binpositions] = hist(X,numbins);
binwidth = binpositions(2) - binpositions(1);
histarea = binwidth*sum(bincounts);

x = binpositions(1):0.001:binpositions(end);
y = exppdf(x,mu);
plot(x,histarea*y,'r','LineWidth',2)

So i was wondering if anyone can explain it to me ? There are some lines that i cannot find them .

Thank you !! And sorry for the bother !!