Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

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


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Newbie - inverse method
Replies: 0  

Advanced Search

Back to Topic List Back to Topic List  
amarender

Posts: 109
Registered: 2/5/09
Newbie - inverse method
Posted: Apr 19, 2012 1:44 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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 !!



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.