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 で Axes Transformations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!