You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Octave中绘制位置向量并实现向量相加可视化?

Plotting Vectors and Their Sum in Octave

Hey there! I’ve got you covered on this vector plotting and addition task. Let’s break down exactly how to fix those discrete points and draw proper vectors starting from the origin (or any point you want).

Step 1: Calculate the Vector Sum

First, let’s confirm the sum of your vectors—this is straightforward in Octave:

% Define your original vectors
v1 = [0; 2];
v2 = [1; 3];

% Compute their sum
v_sum = v1 + v2;

Step 2: Use quiver() to Draw Vectors (Not plot()!)

The reason you’re seeing discrete points is that plot() is designed for plotting data points or lines between coordinates—not vectors with arrows. Octave has a dedicated function for this: quiver(). It lets you specify the starting point of the vector and its x/y components, perfect for our needs.

Drawing Vectors from the Origin

Here’s how to plot all three vectors starting at (0,0), with distinct colors and styles to make them easy to tell apart:

% Turn on hold to overlay multiple vectors on the same plot
hold on;

% Plot v1 (red, medium thickness)
quiver(0, 0, v1(1), v1(2), 'Color', 'r', 'LineWidth', 1.5, 'MaxHeadSize', 0.5);

% Plot v2 (blue, medium thickness)
quiver(0, 0, v2(1), v2(2), 'Color', 'b', 'LineWidth', 1.5, 'MaxHeadSize', 0.5);

% Plot the sum vector (green, thicker to emphasize it)
quiver(0, 0, v_sum(1), v_sum(2), 'Color', 'g', 'LineWidth', 2, 'MaxHeadSize', 0.5);

Step 3: Polish the Plot for Clarity

Let’s add labels, a legend, and adjust the axes so everything looks clean:

% Set axis limits to ensure all vectors are fully visible
xlim([-1, 3]);
ylim([-1, 6]);

% Add axis labels
xlabel('X-axis');
ylabel('Y-axis');

% Add a legend to identify each vector
legend('v1', 'v2', 'v1 + v2');

% Turn on grid lines for better reference
grid on;

% Add a title
title('Vector Addition (Origin as Starting Point)');

% Turn off hold when you're done plotting
hold off;

Bonus: Plotting Vectors from Non-Origin Points

If you want to visualize the triangle rule of vector addition (drawing v2 starting at the end of v1), just adjust the starting coordinates in quiver():

hold on;

% Plot v1 from origin
quiver(0, 0, v1(1), v1(2), 'Color', 'r', 'LineWidth', 1.5);

% Plot v2 starting at the end of v1 (v1's x/y components are the start point)
quiver(v1(1), v1(2), v2(1), v2(2), 'Color', 'b', 'LineWidth', 1.5);

% Plot the sum vector from origin
quiver(0, 0, v_sum(1), v_sum(2), 'Color', 'g', 'LineWidth', 2);

% Same polishing steps as before
xlim([-1, 3]);
ylim([-1, 6]);
xlabel('X-axis');
ylabel('Y-axis');
legend('v1', 'v2', 'v1 + v2');
grid on;
title('Vector Addition (Triangle Rule)');
hold off;

That’s it! Run this code, and you’ll get clear, arrowed vectors instead of discrete points.

内容的提问来源于stack exchange,提问作者Zack Per

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:02:59