Plotting Quiver with Contour

8 views (last 30 days)
Hasan Al Tarify
Hasan Al Tarify on 28 Oct 2021
Answered: Vedant Shah on 30 Apr 2025
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');

Answers (1)

Vedant Shah
Vedant Shah on 30 Apr 2025
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:

Categories

Find more on Vector Fields in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!