Scalar division and Subtraction ?!!
3 views (last 30 days)
Show older comments
I am trying to use some artificial data to see if my code is working.. but there is a error for the division and subtraction part.. See below the Code...
function Sa = trial(lambdaMax,lambda,T)
t = 0;
I = 0;
Sa = [];
u = {10,2,11,4,5,6};
t = t - log(u)/lambdaMax;
while t < T
u = {10,2,11,4,5,6};
if (u <= lambda(t)/lambdaMax)
I = I+1;
Sa(I) = t;
end
u = {10,2,11,4,5,6};
t = t - log(u)/lambdaMax;
end
lambdaMax=50;
T=20;
lambda =@(Sa) lambdaMax*cos(Sa);
Sa = trial(lambdaMax,lambda,T);
figure
hold on
%plot(Sa,lambda(Sa),'*')
xlabel('t')
ylabel ('cos(x)')
X = linspace(min(Sa),max(Sa),10);
Y = pchip(Sa,lambda(Sa),X);
plot(X,Y)
line(repmat(Sa,2,1),repmat([0;1],1,length(Sa)),'color','r' )
Thanks all in advance :)
1 Comment
Accepted Answer
Fangjun Jiang
on 13 Jul 2011
Why do you use cell for your variable u? change it to be data array.
u = {10,2,11,4,5,6}
u = [10,2,11,4,5,6]
lambdaMax=50;
T=20;
lambda =@(Sa) lambdaMax*(cos(Sa)+1.1);
Sa = trial(lambdaMax,lambda,T);
figure;
hold on;
plot(Sa,lambda(Sa),'*')
xlabel('t')
ylabel ('cos(x)')
X = linspace(min(Sa),max(Sa),100);
Y = pchip(Sa,lambda(Sa),X);
plot(X,Y)
line(repmat(Sa,2,1),repmat([0;1],1,length(Sa)),'color','r' )
function Sa = trial(lambdaMax,lambda,T)
t = 0;
I = 0;
Sa = [];
u=rand;
t = t - log(u)/lambdaMax;
while t < T
u=rand;
if (u <= lambda(t)/lambdaMax)
I = I+1;
Sa(I) = t;
end
u=rand;
t = t - log(u)/lambdaMax;
end
6 Comments
Sean de Wolski
on 13 Jul 2011
no it is not. What is lambdamac, lambda (a function handle we presume by looking at the recursive nature of your function, and T?
More Answers (3)
Sean de Wolski
on 13 Jul 2011
t converges to:
-20888 -6288 -21753 -12576 -14600 -16254
All of those are less than T. The while loop never exits. Perhaps you want while t>T?
13 Comments
Sean de Wolski
on 13 Jul 2011
The easiest way would just be to pull
I = I+1;
outside of the if statement. 'I' will get bigger every time and then all of the non-zero values in SA are places to be filled in.
Susan
on 13 Jul 2011
12 Comments
Fangjun Jiang
on 13 Jul 2011
Does it require the lambda function be positive? I modified your lambda function to make it always positive. See the code in my answer section.
Susan
on 13 Jul 2011
1 Comment
Sean de Wolski
on 13 Jul 2011
t = 0;
I = 0;
Sa = [];
u = rand;
t = t - log(u)/lambdaMax;
while t <= T
if (u <= lambda(t)/lambdaMax)
I = I+1;
Sa(I) = t;
end
u = rand;
t = t - log(u)/lambdaMax;
u = rand;
end
Is how I interpret that last page.
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!