在Matlab中模拟随机游走

问题描述

你好,这就是我所做的

enter image description here

我已经完成了指向F点的操作

我所缺少的,我不知道怎么做是最后一点(G)

因为这样的东西必须出现

最后一点,您能帮我吗?

enter image description here

我的代码

clc;clear;close all;
x=zeros; %store all x
y=zeros; %store all y
cx=0; %current x
cy=0; %current y
for i=1:20
x1 = rand(1); %generate random value
x2 = rand(1); %generate random value
s = 100*log(x1); %step size
angle = 2*pi*x2;
dx = s*cos(angle); %step size along x
cx = cx+dx; %new x position
dy = s*sin(angle); %step size along y
cy = cy+dy; %new y position
x = [x cx]; %add to array
y = [y cy]; %add to array


end

plot(x,y); %plot

title('Random Walk');
xlabel('X')
ylabel('Y')

解决方法

绘制3个轨迹,每个轨迹20个运动

使用另一个外部for循环可以使您重复整个循环。要在同一轴上绘制多个图,请添加hold on

Random Walk Plot

clc;clear;close all;

Number_Of_Trajectories = 3;
for Trajectory = 1: Number_Of_Trajectories
x= zeros; %store all x
y= zeros; %store all y
cx=0; %current x
cy=0; %current y


for Step = 1: 20
x1 = rand(1); %generate random value
x2 = rand(1); %generate random value
s = 100*log(x1); %step size
angle = 2*pi*x2;
dx = s*cos(angle); %step size along x
cx = cx+dx; %new x position
dy = s*sin(angle); %step size along y
cy = cy+dy; %new y position
x = [x cx]; %add to array
y = [y cy]; %add to array
end

plot(x,y); %plot
hold on

end


Limit = 1000; 

axis([-Limit Limit -Limit Limit]);
grid on;
title('Random Walk');
xlabel('X')
ylabel('Y')

使用MATLAB R2019b运行

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...