Scalar division and Subtraction ?!!

3 次查看(过去 30 天)
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 :)

采纳的回答

Fangjun Jiang
Fangjun Jiang 2011-7-13
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 个评论
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
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 个评论
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
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
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
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by