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

1 次查看(过去 30 天)
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