matlab - 在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')
解决方案
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
.
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
推荐阅读
- ajax - 带有返回部分视图的 Asp.net 核心控制器功能,其中 select2 不起作用,远程功能验证未在模式弹出窗口中触发
- mysql - Docker mysql容器不允许远程连接
- python - 拆分时超出范围(基本)
- kotlin - Kotlin 中类型投影的使用站点与声明站点的差异
- c# - 循环遍历某个字符串的所有出现并用 C# 中的不同值替换每个字符串?
- r - 在 R 中使用行函数时出现意外的“,”
- jquery - 如果选中单选按钮,则显示隐藏 div
- c++ - 使用均匀分布的随机数进行蒙特卡罗模拟
- typescript - 在 TypeScript 中循环设置对象字段
- android - 在没有 GUI 和 HAL 的 android 系统上解决配对 pin 交换