Scalar division and Subtraction ?!!
Mostrar comentarios más antiguos
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 comentario
Sean de Wolski
el 13 de Jul. de 2011
You should report, the FULL error message.
Respuesta aceptada
Más respuestas (3)
Sean de Wolski
el 13 de Jul. de 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 comentarios
Susan
el 13 de Jul. de 2011
Sean de Wolski
el 13 de Jul. de 2011
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
el 13 de Jul. de 2011
Sean de Wolski
el 13 de Jul. de 2011
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
el 13 de Jul. de 2011
Sean de Wolski
el 13 de Jul. de 2011
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
el 13 de Jul. de 2011
Sean de Wolski
el 13 de Jul. de 2011
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
el 13 de Jul. de 2011
Sean de Wolski
el 13 de Jul. de 2011
sure.
Susan
el 13 de Jul. de 2011
Sean de Wolski
el 13 de Jul. de 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
el 13 de Jul. de 2011
Susan
el 13 de Jul. de 2011
12 comentarios
Fangjun Jiang
el 13 de Jul. de 2011
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
el 13 de Jul. de 2011
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
el 13 de Jul. de 2011
Susan
el 13 de Jul. de 2011
Fangjun Jiang
el 13 de Jul. de 2011
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
el 13 de Jul. de 2011
Fangjun Jiang
el 13 de Jul. de 2011
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
el 13 de Jul. de 2011
Fangjun Jiang
el 13 de Jul. de 2011
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
el 13 de Jul. de 2011
Fangjun Jiang
el 13 de Jul. de 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
el 13 de Jul. de 2011
Susan
el 13 de Jul. de 2011
0 votos
1 comentario
Sean de Wolski
el 13 de Jul. de 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.
Categorías
Más información sobre Axes Transformations en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!