Scalar division and Subtraction ?!!
이전 댓글 표시
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
Sean de Wolski
2011년 7월 13일
You should report, the FULL error message.
채택된 답변
추가 답변 (3개)
Sean de Wolski
2011년 7월 13일
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
Susan
2011년 7월 13일
Sean de Wolski
2011년 7월 13일
What's to debug? You need to figure out WHAT YOU WANT. Then we can code it/debug it etc. If you aren't sure of what while criterion you need then we bigger fish to fry.
Susan
2011년 7월 13일
Sean de Wolski
2011년 7월 13일
function Sa = trial713(lambdaMax,lambda,T)
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
disp(t)
t = t - log(u)/lambdaMax;
end
works for me. Well at least it converges and I get a plot
Susan
2011년 7월 13일
Sean de Wolski
2011년 7월 13일
So what you want is an index vector for all of the loop iterations that failed the if criterion? And then fill those in with data - probably just interpolation?
Susan
2011년 7월 13일
Sean de Wolski
2011년 7월 13일
well if u<=stuff
then you save that current value of t; if it isn't, then you don't. So what you need to know is; when you save stuff/when you don't save stuff?
Susan
2011년 7월 13일
Sean de Wolski
2011년 7월 13일
sure.
Susan
2011년 7월 13일
Sean de Wolski
2011년 7월 13일
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
2011년 7월 13일
Susan
2011년 7월 13일
댓글 수: 12
Fangjun Jiang
2011년 7월 13일
You still have u={} inside the while-loop. After fixing that, your problem is that the while-loop is running for ever. Your said your previous code runs with u as rand(). Is it rand or rand(1,6)?
Fangjun Jiang
2011년 7월 13일
Okay, in your current code above, inside the while-loop, u is always reset to [10,2,11,4,5,6], with the statement t=t-log(u)/lambdaMax, t is become smaller and smaller. So the condition while t<T is always true you you'll never get out of the loop. With previous u=rand, because u is between 0 and 1, log(u) is negative, so t becomes bigger and bigger. At one point, t will be bigger than T which is 20. So the while-loop is terminated and the figure statements below get executed. You need to re-check your code and be clear what you are trying to do.
Susan
2011년 7월 13일
Susan
2011년 7월 13일
Fangjun Jiang
2011년 7월 13일
Your blue curve only has 10 data points. How smooth could you expect it to be? If you change the 10 in X = linspace(min(Sa),max(Sa),10) to be 100 or 1000, would that be what you expected? What does your code try to do? I have hard time to understand it.
Susan
2011년 7월 13일
Fangjun Jiang
2011년 7월 13일
Okay, that's actually not the problem. You can leave that number to be 10 or 100. Your problem is this line "if (u <= lambda(t)/lambdaMax)". It should be "if (t <= lambda(t)/lambdaMax)".
Susan
2011년 7월 13일
Fangjun Jiang
2011년 7월 13일
The blue curve is the cosine curve. It's just a small potion of it (from 0 to 0.7). If you un-comment the %plot(Sa,lambda(Sa),'*') line, you'll see they over-lap nicely.
Susan
2011년 7월 13일
Fangjun Jiang
2011년 7월 13일
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
2011년 7월 13일
Susan
2011년 7월 13일
0 개 추천
댓글 수: 1
Sean de Wolski
2011년 7월 13일
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.
카테고리
도움말 센터 및 File Exchange에서 Descriptive Statistics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!