フィルターのクリア

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

1 回表示 (過去 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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by