MATLAB Answers

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

3 views (last 30 days)
Sayed Rezwanul Islam
Sayed Rezwanul Islam on 15 Feb 2020
Edited: John D'Errico on 17 Feb 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 Comment

John D'Errico
John D'Errico on 17 Feb 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?

Sign in to comment.

Answers (1)

Jakob B. Nielsen
Jakob B. Nielsen on 17 Feb 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!
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!)


Sign in to comment.

Sign in to answer this question.

Translated by