How to create continuous points and plot in quiver
显示 更早的评论
Hello, I would like to seek your help. I had created discrete points and plot vector field by using the quiver function. Now, I would like to create continuous points which are in the same range (0 to 300), and the result of vector field should be the same as discrete points.

my code as below,
clear all; close all; clc
% discrete points
v = 0:25:300;
[x,y] = meshgrid(v);
z = -cos(x).*(-x.^2 - y.^2)*1e-4;
[px,py] = gradient(z);
figure(1)
quiver(x,y,px,py)
hold off
回答(1 个)
Darshak
2025-3-3
Hi @Tina Hsiao,
When working with data, continuous data is typically represented by using finer discrete data points. To create a quiver plot that aligns with your use case, you might find the following sample code useful, as it effectively simulates continuous data, in this code, the following steps are undertaken:
- A finer grid is defined by generating more granular data points.
- Use “interp2” for interpolation of gradient data over this newly established grid.
- The interpolated vector field is then visualized using the “quiver” function.
v = 0:25:300;
[x, y] = meshgrid(v);
z = -cos(x).*(-x.^2 - y.^2) * 1e-4;
[px, py] = gradient(z);
% Continuous points
v_fine = 0:1:300; % Create a finer grid
[x_fine, y_fine] = meshgrid(v_fine);
% Interpolate the vector field
px_fine = interp2(x, y, px, x_fine, y_fine, 'cubic');
py_fine = interp2(x, y, py, x_fine, y_fine, 'cubic');
% Plot the continuous vector field
figure(2)
quiver(x_fine, y_fine, px_fine, py_fine)
title('Continuous Vector Field')
xlabel('x')
ylabel('y')
You can explore the “interp2” and “quiver” function’s documentation for a better understanding of the same.
类别
在 帮助中心 和 File Exchange 中查找有关 Vector Fields 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!