필터 지우기
필터 지우기

Scalar division and Subtraction ?!!

조회 수: 1 (최근 30일)
Susan
Susan 2011년 7월 13일
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일
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에서 Mathematics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by