フィルターのクリア

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 Center および File ExchangeMathematics についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by