MATLAB绘图:随机彩色圆点图

在这里插入图片描述

这段代码在MATLAB中生成并绘制了500个随机位置和颜色的散点图。通过随机生成的x和y坐标以及颜色,用户可以直观地观察到随机点的分布。这种可视化方式在数据分析、统计学和随机过程的演示中具有广泛的应用。

运行结果

在这里插入图片描述

代码

clc; clear; close all;

n = 500; % 点的数量
x = rand(n, 1) * 10; % x坐标
y = rand(n, 1) * 10; % y坐标
colors = rand(n, 3); % 随机颜色

figure;
scatter(x, y, 100, colors, 'filled');
title('随机彩色圆点');
xlabel('X 轴');
ylabel('Y 轴');
axis equal;
grid on;

代码讲解

下面是对这段MATLAB代码的逐步讲解,解释其功能和执行过程:

clc; clear; close all;
  • clc:清除命令窗口的内容。
  • clear:清除工作空间中的所有变量,以确保没有旧数据干扰。
  • close all:关闭所有打开的图形窗口,确保图形的清晰展示。
n = 500; % 点的数量
  • 这行代码定义了变量 n,表示将要生成的随机点的数量,这里设置为500。
x = rand(n, 1) * 10; % x坐标
y = rand(n, 1) * 10; % y坐标
  • x = rand(n, 1) * 10:生成一个包含500个随机数的列向量 x,这些随机数均匀分布在 [0, 10] 区间内,作为点的x坐标。
  • y = rand(n, 1) * 10:同样生成一个包含500个随机数的列向量 y,作为点的y坐标。
colors = rand(n, 3); % 随机颜色
  • colors = rand(n, 3):生成一个500×3的矩阵 colors,每一行表示一个点的颜色,颜色值在 [0, 1] 区间内,分别对应RGB三个通道。这意味着每个点的颜色都是随机的。
figure;
scatter(x, y, 100, colors, 'filled');
  • figure:创建一个新的图形窗口。
  • scatter(x, y, 100, colors, 'filled'):使用 scatter 函数绘制散点图:
    • xy 是点的坐标。
    • 100 指定每个点的大小。
    • colors 指定每个点的颜色。
    • 'filled' 表示点是实心的。
title('随机彩色圆点');
xlabel('X 轴');
ylabel('Y 轴');
axis equal;
grid on;
  • title('随机彩色圆点'):设置图形的标题为“随机彩色圆点”。
  • xlabel('X 轴'):设置x轴的标签为“X 轴”。
  • ylabel('Y 轴'):设置y轴的标签为“Y 轴”。
  • axis equal:确保x轴和y轴的单位长度相同,使得图形比例正确。
  • grid on:显示网格线,以便于观察点的分布。

如需帮助,或有导航、定位滤波相关的代码定制需求,请点击下方卡片联系作者

<think> 我们使用MATLAB来绘制PSO算法的原理,主要步骤包括: 1. 初始化粒子群的位置和速度 2. 绘制初始状态(包括搜索空间、粒子位置、速度向量、最优位置标记等) 3. 进行迭代更新,并在每次迭代后更新形,形成动画 根据引用[1]和[2],PSO算法的核心是粒子位置和速度的更新公式: 速度更新公式:$$V_i^{t+1} = \omega V_i^t + c_1 r_1 (Pbest_i - X_i^t) + c_2 r_2 (Gbest - X_i^t)$$ 位置更新公式:$$X_i^{t+1} = X_i^t + V_i^{t+1}$$ 在绘制原理时,我们通常选择二维空间以便可视化。我们将绘制: - 粒子:用圆圈表示 - 速度向量:用箭头表示(使用quiver函数) - 个体最优位置(Pbest):用星号表示 - 全局最优位置(Gbest):用五角星表示 此外,为了展示优化问题的地形,我们可以绘制等高线(例如一个测试函数,如Rastrigin函数或Sphere函数)。 步骤: 1. 定义测试函数(适应度函数) 2. 初始化粒子群(位置、速度、Pbest、Gbest) 3. 设置参数(惯性权重ω,加速常数c1、c2,最大迭代次数等) 4. 创建形窗口,绘制初始状态(包括等高线、粒子、速度向量、最优位置) 5. 进入迭代循环,更新粒子位置和速度,更新形 注意:为了清晰展示,我们可以用不同颜色表示不同粒子,用虚线箭头表示速度更新中的认知项和社会项(可选,但这样更详细)。 由于绘制详细的分解项(认知项和社会项)会使形复杂,我们通常只绘制总的速度向量。但为了教学目的,我们可以选择在某一代(比如第一代)的某个粒子上绘制分解项。 以下是一个MATLAB代码框架,用于绘制PSO原理(动态演示): </think> ### 使用MATLAB实现PSO算法原理绘制的方法 以下是使用MATLAB绘制PSO算法原理的详细步骤和方法,结合您提供的引用资源: #### 一、核心绘图步骤 1. **初始化搜索空间与粒子群** - 创建二维坐标系,定义粒子位置和速度 - 绘制适应度函数地形(如Sphere函数) ```matlab % 定义搜索空间 [X,Y] = meshgrid(-5:0.1:5, -5:0.1:5); Z = X.^2 + Y.^2; % Sphere函数示例 % 绘制等高线地形 contour(X, Y, Z, 20, 'LineWidth', 1.2); hold on; colorbar; title('PSO算法搜索空间'); xlabel('X'); ylabel('Y'); % 初始化粒子群(10个粒子) n_particles = 10; positions = rand(n_particles, 2)*10 - 5; % 随机位置[-5,5] velocities = zeros(n_particles, 2); % 初始速度 ``` 2. **标记最优位置** - 个体最优(Pbest)用蓝色星号标记 - 全局最优(Gbest)用红色五角星标记 ```matlab % 初始化最优位置 pbest = positions; % 个体最优位置 [~, gbest_idx] = min(sum(positions.^2, 2)); gbest = positions(gbest_idx, :); % 全局最优位置 % 绘制最优位置标记 pbest_plot = plot(pbest(:,1), pbest(:,2), 'b*', 'MarkerSize', 8); gbest_plot = plot(gbest(1), gbest(2), 'rp', 'MarkerSize', 12, 'MarkerFaceColor', 'r'); ``` 3. **绘制粒子与速度向量** - 使用`quiver`函数绘制速度向量 - 粒子用彩色圆圈表示 ```matlab % 绘制粒子位置 particles_plot = scatter(positions(:,1), positions(:,2), 50, 'filled', 'CData', jet(n_particles)); % 绘制速度向量(蓝色箭头) quiver_plot = quiver(positions(:,1), positions(:,2), ... velocities(:,1), velocities(:,2), 0.5, ... 'b', 'LineWidth', 1.5, 'MaxHeadSize', 0.5); ``` 4. **实现迭代更新与动态可视化** - 在循环中更新粒子位置和速度 - 使用`drawnow`实现动画效果 ```matlab % PSO参数(参考引用[2][4]) w = 0.8; % 惯性权重 c1 = 2; % 认知系数 c2 = 2; % 社会系数 for iter = 1:50 % 50次迭代 % 更新速度(带惯性权重的PSO公式) r1 = rand(n_particles, 2); r2 = rand(n_particles, 2); velocities = w*velocities + ... c1*r1.*(pbest - positions) + ... c2*r2.*(gbest - positions); % 更新位置 positions = positions + velocities; % 更新最优位置 fitness = sum(positions.^2, 2); [min_fit, idx] = min(fitness); if min_fit < sum(gbest.^2) gbest = positions(idx, :); end % 更新形 set(particles_plot, 'XData', positions(:,1), 'YData', positions(:,2)); set(quiver_plot, 'XData', positions(:,1), 'YData', positions(:,2), ... 'UData', velocities(:,1), 'VData', velocities(:,2)); set(pbest_plot, 'XData', pbest(:,1), 'YData', pbest(:,2)); set(gbest_plot, 'XData', gbest(1), 'YData', gbest(2)); drawnow; pause(0.1); % 控制动画速度 end ``` #### 二、关键技巧与优化 1. **速度分解可视化**(参考引用[4][5]) 在特定迭代中分解显示速度向量: ```matlab % 选择第一个粒子进行分解演示 idx = 1; % 绘制速度分量 inertia = w*velocities(idx,:); cognitive = c1*rand(1,2).*(pbest(idx,:) - positions(idx,:)); social = c2*rand(1,2).*(gbest - positions(idx,:)); quiver(positions(idx,1), positions(idx,2), inertia(1), inertia(2), 0, ... 'g', 'LineWidth', 1.5, 'LineStyle', '--'); quiver(positions(idx,1)+inertia(1), positions(idx,2)+inertia(2), ... cognitive(1), cognitive(2), 0, 'm--', 'LineWidth', 1.5); quiver(positions(idx,1)+inertia(1)+cognitive(1), ... positions(idx,2)+inertia(2)+cognitive(2), ... social(1), social(2), 0, 'c--', 'LineWidth', 1.5); ``` 2. **收敛过程可视化** 添加收敛轨迹线: ```matlab % 记录全局最优位置变化 gbest_history = zeros(50, 2); for iter = 1:50 % ...更新代码... gbest_history(iter,:) = gbest; end plot(gbest_history(:,1), gbest_history(:,2), 'r-', 'LineWidth', 1.5); ``` 3. **科研级形优化** - 添加例:`legend('粒子', '速度向量', '个体最优', '全局最优')` - 设置坐标轴等比例:`axis equal` - 添加公式标注: ```matlab text(-4.5, 4.5, sprintf('$$V_i^{t+1} = %.1fV_i^t + %.1fr_1(Pbest_i - X_i^t) + %.1fr_2(Gbest - X_i^t)$$', w, c1, c2), ... 'Interpreter', 'latex', 'FontSize', 12) ``` #### 三、完整代码框架 ```matlab %% PSO原理绘制 figure('Color', 'white', 'Position', [100, 100, 800, 600]); % 1. 创建搜索空间地形 [X,Y] = meshgrid(-5:0.1:5, -5:0.1:5); Z = X.^2 + Y.^2; contourf(X, Y, Z, 20); colormap(jet); colorbar; hold on; title('粒子群优化算法(PSO)原理', 'FontSize', 14); xlabel('X'); ylabel('Y'); % 2. 初始化粒子群 n_particles = 10; positions = rand(n_particles, 2)*10 - 5; velocities = (rand(n_particles, 2)-0.5)*0.5; % 3. 绘制初始状态 particles = scatter(positions(:,1), positions(:,2), 70, 'filled', 'CData', jet(n_particles)); quivers = quiver(positions(:,1), positions(:,2), velocities(:,1), velocities(:,2), 0.5, 'b', 'LineWidth', 1.5); % 4. 标记最优位置 [~, gbest_idx] = min(sum(positions.^2, 2)); gbest = positions(gbest_idx, :); pbest = positions; pbest_plot = plot(pbest(:,1), pbest(:,2), 'b*', 'MarkerSize', 10); gbest_plot = plot(gbest(1), gbest(2), 'rp', 'MarkerSize', 15, 'MarkerFaceColor', 'r'); % 5. 迭代更新(带动画) w = 0.8; c1 = 1.5; c2 = 1.5; for iter = 1:50 % 速度更新(参考引用[2][4]) r1 = rand(n_particles, 2); r2 = rand(n_particles, 2); velocities = w*velocities + c1*r1.*(pbest - positions) + c2*r2.*(gbest - positions); % 位置更新 positions = positions + velocities; % 更新最优位置(参考引用[3]) fitness = sum(positions.^2, 2); update_idx = fitness < sum(pbest.^2, 2); pbest(update_idx, :) = positions(update_idx, :); [min_fit, best_idx] = min(fitness); if min_fit < sum(gbest.^2) gbest = positions(best_idx, :); end % 更新形 set(particles, 'XData', positions(:,1), 'YData', positions(:,2)); set(quivers, 'XData', positions(:,1), 'YData', positions(:,2), ... 'UData', velocities(:,1), 'VData', velocities(:,2)); set(pbest_plot, 'XData', pbest(:,1), 'YData', pbest(:,2)); set(gbest_plot, 'XData', gbest(1), 'YData', gbest(2)); drawnow; pause(0.1); end % 6. 添加标注 legend([particles, quivers(1), pbest_plot, gbest_plot], ... {'粒子位置', '速度向量', '个体最优(Pbest)', '全局最优(Gbest)'}, ... 'Location', 'northeastoutside'); text(-4.8, 4.5, 'PSO算法模拟鸟群觅食行为', 'FontSize', 12, 'BackgroundColor', 'white'); ``` #### 四、输出效果说明 最终生成的原理将包含: 1. 彩色地形表示适应度函数值 2. 彩色圆点表示粒子位置 3. 蓝色箭头表示速度向量 4. 蓝色星号标记个体最优(Pbest) 5. 红色五角星标记全局最优(Gbest) 6. 动态展示粒子向最优位置收敛的过程 通过调整参数(粒子数量、惯性权重ω、学习因子c1/c2),可模拟不同优化场景[^1][^2]。此方法可直接用于科研论文插,清晰展示PSO算法的群体智能优化机制[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MATLAB卡尔曼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值