Borrar filtros
Borrar filtros

Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

Value does not get updated the way it should in for loop

1 visualización (últimos 30 días)
Aarat
Aarat el 7 de Dic. de 2015
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
Initially i define my variable 'mu-value' as:
Start_Position = 30 ;
mu_value = Start_Position;
I want my mu-value to increase with time according to a random walk with drift. Also, I want to increase the time-step length(TimeStepLength) whih means that after Time goes 1,2,3 when TimeStepLength is 1, it goes 1,3,5 when TimeStepLength is 2 and 1,4,7 when TimeStepLength is 3 and so on.
for TimeStepLength = 1:1:(TotalTime) ;
for Time = 1:TimeStepLength:TotalTime ;
DL = [a 0 -a] ; %The sample space for a random-walk
Prob_DL = [Koff 0 Kon] ; % probabilities of each of the elements in DL
RandWalk = datasample(DL,1,'Replace',false, 'Weights',Prob_DL) ;
I also store my mu-value like this:
Tip_Position_Output(Time) = mu_value ;
Then the required calculations are performed:
Time_minus_TSL = Time - TimeStepLength ;
Time_minus_TSL(Time_minus_TSL <= 0) = 1 ;
DeltaL(Time) = (Tip_Position_Output(Time) -Tip_Position_Output(Time_minus_TSL));
MSD(Time) = mean((DeltaL(1:Time)).^2);
Then the mu-value is updated after every loop (so as to keep the random-walk with drift going) like this:
mu_value = mu_value + RandWalk ;
end
end
In these loops there are two problems, 1.The Tip_Position_Output does not always increment/decrease by 0.65, but by numbers that are multiples of 0.65. It should -according to me- increase by 0.65 only. 2. My final DeltaL(Time) always remains either +0.65 or -0.65, whereas it should increase with time.
I have tried fixing many things for a few weeks now but am unable to figure-out what is wrong with my code. I would greatly appreciate it if someone could advise me on how to move forward. Thank you very much!

Respuestas (1)

sam0037
sam0037 el 23 de Dic. de 2015
Editada: sam0037 el 23 de Dic. de 2015
1. The 'Tip_Position_Output' will not increase/decrease by 'a' only while moving along the array 'Tip_Position_Output'. But the new 'mu_value' will increase/decrease by 'a' due to 'RandWalk' which is either 'a' or '-a' due to the weighted data sampling.
Since the 'Tip_Position_Output' array is accessed not in a sequential fashion in the inner loop, we observe the values in 'Tip_Position_Output' to differ by multiples of 'a'.
2. The 'DeltaL' at index 'Time' is the difference of the 'Tip_Position_Output' value between two consecutive index of the innerloop (i.e. at 'Time' and 'Time-TimeStepLength') which again differs by 'RandWalk' which is '-a' or 'a'.
  • x = Tip_Position_Output(Time) = mu_value + RandWalk
  • y = Tip_Position_Output(Time-TimeStepLength) = mu_value //(Time-TimeStepLength is 1 when the value is negative)
  • DeltaL = x - y = RandWalk
You can try debugging through the code to check the value of the variable at each time step. Refer to the following MATLAB documentation link to know more about debugging in MATLAB:

La pregunta está cerrada.

Community Treasure Hunt

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

Start Hunting!

Translated by