Plotting Quiver with Contour

7 ビュー (過去 30 日間)
Hasan Al Tarify
Hasan Al Tarify 2021 年 10 月 28 日
回答済み: Vedant Shah 2025 年 4 月 30 日
Hello,
I need help with plotting using Quiver function such that the arrows of this function follows the contour lines. The equations in the following code are right since I have a got the desired contour plot. PLease note that 𝑢=𝜕𝜓/𝜕𝑦,𝑣=−𝜕𝜓𝜕𝑥. I have attached a picture as an example. Thanks
clc
clear all
syms x y
L = 10000;
H = 6000;
sig = 0.03;
alp = 0;
r1 = (-alp+sqrt(alp^2+4*pi^2/H^2))/2;
r2 = (-alp-sqrt(alp^2+4*pi^2/H^2))/2;
x = linspace(0,L,100);
y = linspace(0,H,100);
[X,Y] = meshgrid(x,y);
p = (sig*H^2/pi^2).*sin(pi*Y/H).*(((exp(r2*L)-1).*exp(r1*X)+(1-exp(r1*L)).*exp(r2*X))/(exp(r2*L)-exp(r1*L))-1);
U = (sig*H/pi).*cos(pi*Y/H).*(((exp(r2*L)-1).*exp(r1*X)+(1-exp(r1*L)).*exp(r2*X))/(exp(r2*L)-exp(r1*L))-1);
V = -1.*(sig*H^2/pi^2).*sin(pi*Y/H).*(((r1.*exp(r2*L)-1).*exp(r1*X)+r2.*(1-exp(r1*L)).*exp(r2*X))/(exp(r2*L)-exp(r1*L))-1);
contour(X,Y,p)
hold on
quiver(X,Y,U,V)
hold off
xlabel('X');
ylabel('Y');
title('sig = 0.03, alp = 0');

回答 (1 件)

Vedant Shah
Vedant Shah 2025 年 4 月 30 日
To make the arrows of the quiver plot follow the contour lines, you need to ensure that the vectors are tangent to the contour lines. This can be achieved by calculating the gradient of the contour plot and then using it to adjust the direction of the arrows.
To calculate the gradient, you can replace the below lines creating “U” and “V”:
U = (sig*H/pi).*cos(pi*Y/H).*(((exp(r2*L)-1).*exp(r1*X)+(1-exp(r1*L)).*exp(r2*X))/(exp(r2*L)-exp(r1*L))-1);
V = -1.*(sig*H^2/pi^2).*sin(pi*Y/H).*(((r1.*exp(r2*L)-1).*exp(r1*X)+r2.*(1-exp(r1*L)).*exp(r2*X))/(exp(r2*L)-exp(r1*L))-1);
with
[px, py] = gradient(p, x, y);
% Normalizing the vectors
magnitude = sqrt(px.^2 + py.^2);
px = px ./ magnitude;
py = py ./ magnitude;
% Assigning to U & V
U = -py;
V = px;
Replacing this to the existing code, we get results as below:
For more information, refer to the following MATLAB documentation links:

カテゴリ

Help Center および File ExchangeVector Fields についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by