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

조회 수: 3 (최근 30일)
Sayed Rezwanul Islam
Sayed Rezwanul Islam 2020년 2월 15일
편집: John D'Errico 2020년 2월 17일
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
John D'Errico
John D'Errico 2020년 2월 17일
편집: John D'Errico 2020년 2월 17일
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?

댓글을 달려면 로그인하십시오.

답변 (1개)

Jakob B. Nielsen
Jakob B. Nielsen 2020년 2월 17일
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!)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by