0

Here is my MATLAB code to plot P(X>x) for an exponential r.v. for E(X) = 10. I am pretty certain that my calculations are correct. The plot looks weird. Can anyone help?

clear; clc; U=rand(1,100000); lambda = 1/10; expValue = -log(1-U)/lambda; prob=exp(-lambda*expValue); plot(U,log(prob));

1 Answers1

1

Just add '.' in the last command:

plot(U,log(prob),'.');

Paglia
  • 1,274
  • Should the plot be a straight line? – user100503 Mar 23 '14 at 18:27
  • the fact is the the points are not ordered, thus the resulting plot is just a weird line that makes a strange path :) – Paglia Mar 23 '14 at 18:31
  • it makes a strange tour, but in fact it passes through the right points! – Paglia Mar 23 '14 at 18:35
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – mookid Mar 23 '14 at 18:44
  • Thank you. Will sort(U) instead of U inside the plot function make it a straight line? – user100503 Mar 23 '14 at 18:47
  • @mookid the question was, namely, "can anyone help?" and I helped him. Frankly, I don't see the problem... – Paglia Mar 23 '14 at 18:54
  • @user100503 yes, you can also sort the vector U before computing the vector prob – Paglia Mar 23 '14 at 18:55
  • @Paglia : in my opinion, it sould have been a comment. – mookid Mar 23 '14 at 18:57
  • 1
    Thank you very much. I appreciate the responses. – user100503 Mar 23 '14 at 18:58
  • Here is my MATLAB code for the similar plot for Poisson. I think I am not computing P(X>x) correctly. Can anyone help? clear; clc; U=rand(1,100000); for i=1:numel(U) k = 0; lambda = 10; p = exp(-lambda); G = p; while U(i) > G p = (lambdap)/(k+1); G=G+p; k=k+1; end X(i)=k; end disp(mean(X)) prob=exp(-lambdaX); plot(U,log(X), '.'); – user100503 Mar 24 '14 at 01:07