Plotting Quiver with Contour
์กฐํ ์: 10 (์ต๊ทผ 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');
๋๊ธ ์: 0
๋ต๋ณ (1๊ฐ)
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:
๋๊ธ ์: 0
์ฐธ๊ณ ํญ๋ชฉ
์นดํ ๊ณ ๋ฆฌ
Help Center ๋ฐ File Exchange์์ Vector Fields์ ๋ํด ์์ธํ ์์๋ณด๊ธฐ
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!