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
Sean de Wolski 2011 年 7 月 13 日
You should report, the FULL error message.

サインインしてコメントする。

 採用された回答

Fangjun Jiang
Fangjun Jiang 2011 年 7 月 13 日

2 投票

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 件のコメント

Susan
Susan 2011 年 7 月 13 日
there is no error now but It is not even running.. nothing come up at all ??
Susan
Susan 2011 年 7 月 13 日
Originally I have u = rand; and It bring up figure with plot but I want to test it with known data to me that's why I defined thses numbers but with using array nothing happens?? why is it working with rand but not with a set of data????
Fangjun Jiang
Fangjun Jiang 2011 年 7 月 13 日
You need to learn how to debug your code. Put some break points in your code and run the code step by step. What is your input of the function, the value of lambdaMax,lambda,T ?
You can put in break points by clicking the column just at the right of the line numbers in your M-editor. Then press F5 and F10. Watch the error message or values of your variable.
Susan
Susan 2011 年 7 月 13 日
the Input data is above.. I put my code with the script
Sean de Wolski
Sean de Wolski 2011 年 7 月 13 日
no it is not. What is lambdamac, lambda (a function handle we presume by looking at the recursive nature of your function, and T?
Susan
Susan 2011 年 7 月 13 日
lambdaMax = 50; is representing the maximum point of the function lambda which i set it to 50.. lambda is the function i want it to be drawn lambda =@(Sa) lambdaMax*cos(Sa); and T is the time period and I set it to T=20;

サインインしてコメントする。

その他の回答 (3 件)

Sean de Wolski
Sean de Wolski 2011 年 7 月 13 日

1 投票

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
Susan 2011 年 7 月 13 日
Ok.. even though I changed it to t>T but still not running.. Can you please tell me how to debug it because at the moment Its not allowing me to put breakpoints?
Sean de Wolski
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
Susan 2011 年 7 月 13 日
I know what I want.. the trial code is non-homogeneous possion process algorithm and I should not change it.. t<T {that how it should be ]... and in the original algorithm u=rand (and at least with that I get a plot and figure) but the lambda (cos function) is not smooth {if you run it with changing u = rand, you will know what I am talking about).. but I thought the reason the cosine is not smooth because with this algorithm sometimes there will be data generated and for some time period no data at all, so the cosine is plotted (with the missing data) thats why it is not smooth because it just connect the dots together for the missing data.. So I thought I will create data maself (so I wont have missing data and the plot will be smooth)..
I hope that explains what is all about and if you run it by changing u = rand;... It will be clear { the red dashes is the random data]..
Cheers !
Sean de Wolski
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
Susan 2011 年 7 月 13 日
Yeah that was working for me 2, but as I explained above the cosine function (the blue one) is not smooth like it meant to be { It should be just the normal curved cosine function)... I reckon is because of the missing data thats why I am trying to generate data maself so I wouldnt have missing data because this algorithm generates for some time period missing data,,
Sean de Wolski
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
Susan 2011 年 7 月 13 日
Can you explain to me a little bit more.. I will try this method.?
Sean de Wolski
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
Susan 2011 年 7 月 13 日
Can I just create the cosine function(the blue one) INDEPENDENT from the random generating data(red dashes).. so the blue cosine function just represent regular cosine function??
Sean de Wolski
Sean de Wolski 2011 年 7 月 13 日
sure.
Susan
Susan 2011 年 7 月 13 日
How can I do that.. thats what I was trying to find out.. how to just create it Independently?
Sean de Wolski
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
Susan 2011 年 7 月 13 日
Yeah, I tried its slightly different from what I want but I got the overall Idea,, Thanks very much for your effort and taking your time to help me sort out this.. MATLAB is problematic to be and this work makes it worse but you guys helped me.. Thanks :)

サインインしてコメントする。

Susan
Susan 2011 年 7 月 13 日

0 投票

This is trial
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
this is the script called test to run the code in trial
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' )
I can't even set breakpoints not sure whats happening, Its not even running and no error?? I don't get whats the problem..

12 件のコメント

Fangjun Jiang
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
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
Susan 2011 年 7 月 13 日
Its u = rand.. and it works for that but not the way i want it though.. I explained below what I am looking for and what I believe is the problem but Can't fix it??
Susan
Susan 2011 年 7 月 13 日
If you run it the cosine(lambda) is not smooth because the algorithm for sometime period dont generate data so it just connect the dots.. How can I generate the data maself so I can get the smooth cosine function... I want the blue plot to be smooth curved cosine function and my data is the red( that bit works)???
Fangjun Jiang
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
Susan 2011 年 7 月 13 日
My code is regarding non-homogeneous possion process and the red lines are the actual random data generated. the blue should be the lambda function and the code is simply generating randomly data over time and sometimes period the data are not generated thats why the red data in some places are missing... I tried 100 and 100 and It looks smoother than 10.. Can you please change it instead to 100 and If you see the cosine is almost clearer now but however for the missing data it just connect the dots and that doesn't look right.. I meant to have the normal curved cosine function { the bigger the curve the more data{red lines) smaller the curve the less data)..
this article is what I am mainly working on to acheive what I said above.. Now with your suggestion increasing it to 100 it looks better but still there is a slight problem because the figure still doesn't show the normal cosine curve, I hope when you run it you will understand what I am saying more..
http://www.stat.sdu.dk/matstat/yuri-st505/w5slides1.pdf
Fangjun Jiang
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
Susan 2011 年 7 月 13 日
but not It is just a curved line, noting looks like the cosine function and in the algorithm the article I sent you (last page) the algorithm says if (u <= lambda(t)/lambdaMax)..Can you explain why do we have to make that change but still its not doing the right thing??
Fangjun Jiang
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
Susan 2011 年 7 月 13 日
Is it working to you?,, I did un-comment it but still not working.. Can you please show me what you have
Fangjun Jiang
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
Susan 2011 年 7 月 13 日
I am not sure at this stage but I made the changes to the code.. Thanks very much for your help and explanation.. I really appreciate the time and effort you put in..

サインインしてコメントする。

Susan
Susan 2011 年 7 月 13 日

0 投票

This is the link for the non-homogeneous algorithm I am doing.. Its the trial code I am doing..
The last page only is the relevant algorithm I am coding..
Cheers,

1 件のコメント

Sean de Wolski
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.

サインインしてコメントする。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by