필터 지우기
필터 지우기

Help with basic code

조회 수: 1 (최근 30일)
Susan
Susan 2011년 3월 22일
I attached the pdf its the last page 9 I meant to code that in MATLAB, I believe even the code is basic but I am not sure why is not working. I tried to change the code several time but no luck.
from the Paper:
(1) t = 0, I = 0.
(2) Generate a random number U ~ U(0, 1).
(3) t = t - (1/Lam)*ln(U). If t > T then stop.
(4) Generate a random number U ~ U(0, 1).
(5) If U <= Lam(t)/Lam, set I = I + 1, S(I) = t.
(6) Go to step 2.
Output:
I: the number of events at time T ,
S(1), . . . , S(I): the event times.
2. Direct generation of successive event times
function nonhomogeneous (i,s)
i = 0;
t = 010;
T = length(cos(t));
s = [0,t];
u = rand (1,1);
t = t - (1 /cos(t)*log(u));
while t <= T
u2 = rand (1,1);
if ( u2 <= cos(t) / max(cos(t)))
hold on
plot(t,cos(t))
i = i+1;
s(i)=t;
u = rand (1,1);
t = t - (1 /cos(t)*log(u));
end
end

채택된 답변

Andrew Newell
Andrew Newell 2011년 3월 22일
Here is some code that solves the problem and also illustrates an important programming principle: when you create a function, input the variables that you want to experiment with.
Here is a function that outputs a nonhomogeneous random sequence of times.
function S = nonhomogeneous(lambda0,lambda,T)
t = 0;
I = 0;
S = [];
u = rand;
t = t - log(u)/lambda0;
while t <= T
u = rand;
if (u <= lambda(t)/lambda0)
I = I+1;
S(I) = t;
end
u = rand;
t = t - log(u)/lambda0;
end
Save this in a file nonhomogenous.m. Here is a script to run it:
lambda0 = 50; % Needs to be much larger than T to get several numbers
T = 1;
lambda = @(x) lambda0*cos(x); % This is lambda0(t)/lambda0
S = nonhomogeneous(lambda0,lambda,T);
subplot 121
plot(S,lambda(S),'.')
xlabel('t')
ylabel('lambda(t)')
lambda = @(x) lambda0*sin(x);
S = nonhomogeneous(lambda0,lambda,T);
subplot 122
plot(S,lambda(S),'.')
xlabel('t')
I'm not sure what you mean by "spikes". Maybe you really want to plot something like
hist(S)
  댓글 수: 3
Andrew Newell
Andrew Newell 2011년 3월 22일
f(t) is lambda(t)/lambda. I should have written it this way for clarity, so I have edited the above code. Note that lambda0 is the maximum value for lambda in the code.
Susan
Susan 2011년 3월 22일
Thanks very much :)

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Andrew Newell
Andrew Newell 2011년 3월 22일
@Susan, thank you for formatting the code nicely. I wish more people would in their questions!
I notice your function doesn't have any outputs and your inputs are redundant. If you change the top line to
function [s,i] = nonhomogeneous
do you get what you want?
EDIT: I see several problems:
t = 010; % t=10
instead of
t = 0;
Also,
T = length(cos(t));
gives T=1 (is that what you're after?)
s = [0,t];
initializes your array s to [0 10] instead of [].
Yet another: there is no lambda. Instead, you're using
max(cos(t))
which is only the maximum for the current values of t, not 1 as it should be.
  댓글 수: 1
Susan
Susan 2011년 3월 22일
Thanks very much Ive changed it accordingly but still I dont get what I am expecting.. I am meant to make the lambda function e.g sin or cos and then to have a graph at the end with that function and the higher the curve of the function the more spikes and vice versa..
the plot is not working at all.

댓글을 달려면 로그인하십시오.


Jan Jensen
Jan Jensen 2011년 3월 22일
I've read the presentation, and it was quite interesting. Not sure I understand all the implications, though.
Please post your definition of the lambda function as well as the code.
A good starting point could also be to implement the simulation of Poisson process with constant decay (lambda = constant). The one on slide 5. The dimensions of the output variable S should be the same. If the plot of S for lambda=constant makes sense you have made a great step forward in debugging your code, including your plotting commands :)
  댓글 수: 1
Susan
Susan 2011년 3월 22일
Hey Jan..
I tried it with constant and it is workingm I just changed few things but its working finally :)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by