Filter löschen
Filter löschen

Scalar division and Subtraction ?!!

3 Ansichten (letzte 30 Tage)
Susan
Susan am 13 Jul. 2011
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 Kommentar
Sean de Wolski
Sean de Wolski am 13 Jul. 2011
You should report, the FULL error message.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Fangjun Jiang
Fangjun Jiang am 13 Jul. 2011
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 Kommentare
Sean de Wolski
Sean de Wolski am 13 Jul. 2011
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 am 13 Jul. 2011
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;

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

Sean de Wolski
Sean de Wolski am 13 Jul. 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 Kommentare
Sean de Wolski
Sean de Wolski am 13 Jul. 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
Susan am 13 Jul. 2011
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 :)

Melden Sie sich an, um zu kommentieren.


Susan
Susan am 13 Jul. 2011
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 Kommentare
Fangjun Jiang
Fangjun Jiang am 13 Jul. 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
Susan am 13 Jul. 2011
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..

Melden Sie sich an, um zu kommentieren.


Susan
Susan am 13 Jul. 2011
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 Kommentar
Sean de Wolski
Sean de Wolski am 13 Jul. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Mathematics finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by