首页 > 解决方案 > 在matlab中模拟随机游走

问题描述

你好这是我所做的 在此处输入图像描述

我已经完成了 F 点

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

因为这样的事情必须出来

你能帮我解决最后一点吗?

在此处输入图像描述 我的代码:

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')

标签: matlabmatlab-figure

解决方案


Plotting 3 Trajectories with 20 Movements Each

Using another outer for-loop can allow you to repeat the entire cycle. To plot multiple plots on the same axis add 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')

Ran using MATLAB R2019b


推荐阅读