Create random path with known end and start point
Show older comments
Hi,
I would like to create "random" paths starting from one known value and ending to another one. They should not be too extreme so I have limited that I don't accept paths who get over twice as big or half as small than starting and ending points.
I have partial solution but it doesn't work when StartPoint and EndPoint are equal as it forces all values to be equal (forcing to different values to be same results this be definition)...
Any ideas? Not need to be perfect, but I would like to generate some of these to compare case where we get from start point to end point via straight line.
PathLength=100;
NumberOfSimulations=10;
StartPoint=1000;
EndPoint=2000;
Saved=NaN*zeros(PathLength, NumberOfSimulations);
n=0;
while n~=NumberOfSimulations
r =randn(PathLength,1); %Random nromally distributed numbers
c=cumsum(r);
%Linear equation to scale values with start and end point
k=(EndPoint-StartPoint)/(c(end)-c(1));
b=StartPoint-k*c(1);
d=c*k+b;
if (max(d)>(2*max([StartPoint,EndPoint]))|| min(d)<min([StartPoint, EndPoint])/2);
%not good path
else
n=n+1;
all(:,n)=d;
end
end
figure; hold on; plot(all) ; plot(mean(all,2),'k','Linewidth',2.5);
//Edit I found one solution but it is only for same size steps. Still need help.
%%http://cnr.lwlss.net/ConstrainedRandomWalk/
figure;close all;
%Biased random walk
NumberOfSimulations=10;
n=100; % number of steps, nt increasing and n(t-1) decreasing
x0=10; %StartPoint
xtarg=20; %End point after n steps
Saved=NaN*zeros(n+1,NumberOfSimulations+2);
Saved(1,:)=x0;
Saved(n+2,:)=xtarg;
for q=1:NumberOfSimulations
unifs=rand(n+1,1);
x=x0;
for i=0:(n-1)
t =(1-(xtarg-x)/(n-i))/2;
if unifs(i+1,1)<=t
x=x-1;
else
x=x+1;
end
Saved(i+2,q)=x;
end
end
figure; hold on; plot(Saved) ; plot(mean(Saved,2),'k','Linewidth',2.5);
Accepted Answer
More Answers (1)
Image Analyst
on 14 Aug 2016
Edited: Image Analyst
on 14 Aug 2016
Kaisa, what's wrong with just using linspace to set the starting and ending points, and number of steps in between, and then adding noise.
numSteps = 20;
x = 1 : numSteps;
y = linspace(1, 50, numSteps);
noise = rand(1, length(y));
yNoisy = y + 20 * noise - 10; % Adjust 20 as desired.
% Make sure first and last points are not altered.
y(2:end-1) = yNoisy(2:end-1); % Set all but first and last points.
plot(x, y, 'bo-');
grid on;
1 Comment
Alutsyah Luthfian
on 19 Aug 2020
thanks man....
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!