How can I create a random walk for a given function?

1 visualización (últimos 30 días)
Sayed Rezwanul Islam
Sayed Rezwanul Islam el 15 de Feb. de 2020
Editada: John D'Errico el 17 de Feb. de 2020
Hello, I want to create a random walk for the given function.
Cost(s) = (400 – (s – 21)^2) * sin(s*pi/6)
Constraints: 0≤s≤500
I tried a lot but cannot solve it. It will be a great help for me
  1 comentario
John D'Errico
John D'Errico el 17 de Feb. de 2020
Editada: John D'Errico el 17 de Feb. de 2020
What does a random walk for a function mean? What happens randomly here? All you have said is that s lives in the interval [0,500] and then a relationship that allows us to compute Cost, as a function of s.
So where does the randomness (randomnity? :) ) come in?
For example, I could imagine that s might vary ranomdly. Then just gnerate a random sequence for s. and then compute Cost(s). WTP?
Conversely, you might choose some random sequence for Cost. Then you will need to solve for s. But see there are potentially infinitely many solutions to that.
So you need to explain your question, CLEARLY. What is random here?

Iniciar sesión para comentar.

Respuestas (1)

Jakob B. Nielsen
Jakob B. Nielsen el 17 de Feb. de 2020
You can use a for loop, and then add your randomness in whatever way you like. Example:
for i=1:100 %anything that follows will run 100 times, once for i=1, once for i=2.... for i=100
s=500*rand(1,1); %this will generate a random number between 0 and 500, both included.
Cost(i) = (400-(s-21)^2) * sin(s*pi/6); %evaluate - but, inportantly, index into i, not into s!
end
Another way of going about it is to create your 500 random numbers first, like this;
s=500*rand(100,1); %creates 100 random numbers between 0 and 500 in an array
Cost=(400-(s-21).^2) .* sin(s*pi/6);
%but then you must use dots before power and multiplication operators.
Both ways will give similar results (not the same results, of course - its a random walk!)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by