Problem understanding vector gradient component output

2 visualizaciones (últimos 30 días)
s3bbo
s3bbo el 25 de Nov. de 2021
Respondida: Aditya el 23 de Feb. de 2024
I'm trying to understand vector analysis and thought I'd generate some plots with matlab and play around with the input to see how it affects the output.
Mathematically I understand the gradient operation used on a scalar vector/matrix will - for each point in the matrix - generate a vector in the direction of the steepest slope.
I did the following
Define a vector / scalar field:
vec3 = [ 2 0 0 0 1 1 1 0 0 0 0;
0 0 0 1 2 2 2 1 0 0 0;
0 0 1 2 3 3 3 2 1 0 0;
0 1 2 3 4 4 4 3 2 1 0;
1 2 3 4 5.0 5 5.0 4 3 2 1;
1 2 3 4 5 6 5 4 3 2 1;
1 2 3 4 5.0 5 5.0 4 3 2 1;
0 1 2 3 4 4 4 3 2 1 0;
0 0 1 2 3 3 3 2 1 0 0;
0 0 0 1 2 2 2 1 0 0 0;
0 0 0 0 1 1 1 0 0 0 0]
Then use some commands I found in the livescript examples that I adapted:
X2 = [ -5 -4 -3 -2 -1 0 1 2 3 4 5]
Y2 = X2'
[px, py] = gradient (vec3)
figure
hold on
contour (X2, Y2, vec3,'ShowText','on' )
quiver(X2,Y2,px,py)
contour works just as I expect, it draws isopotential lines just fine.
However, I have a problem understanding the output.
First of all, the lower left corner, would have expected the vectors and isopotential in the upper left corner.
Secondly, when I look at the matlab output
vec3 =
2 0 0 0 1 1 1 0 0 0 0
0 0 0 1 2 2 2 1 0 0 0
0 0 1 2 3 3 3 2 1 0 0
0 1 2 3 4 4 4 3 2 1 0
1 2 3 4 5 5 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1
1 2 3 4 5 5 5 4 3 2 1
0 1 2 3 4 4 4 3 2 1 0
0 0 1 2 3 3 3 2 1 0 0
0 0 0 1 2 2 2 1 0 0 0
0 0 0 0 1 1 1 0 0 0 0
px =
-2.0000 -1.0000 0 0.5000 0.5000 0 -0.5000 -0.5000 0 0 0
0 0 0.5000 1.0000 0.5000 0 -0.5000 -1.0000 -0.5000 0 0
0 0.5000 1.0000 1.0000 0.5000 0 -0.5000 -1.0000 -1.0000 -0.5000 0
1.0000 1.0000 1.0000 1.0000 0.5000 0 -0.5000 -1.0000 -1.0000 -1.0000 -1.0000
1.0000 1.0000 1.0000 1.0000 0.5000 0 -0.5000 -1.0000 -1.0000 -1.0000 -1.0000
1.0000 1.0000 1.0000 1.0000 1.0000 0 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000
1.0000 1.0000 1.0000 1.0000 0.5000 0 -0.5000 -1.0000 -1.0000 -1.0000 -1.0000
1.0000 1.0000 1.0000 1.0000 0.5000 0 -0.5000 -1.0000 -1.0000 -1.0000 -1.0000
0 0.5000 1.0000 1.0000 0.5000 0 -0.5000 -1.0000 -1.0000 -0.5000 0
0 0 0.5000 1.0000 0.5000 0 -0.5000 -1.0000 -0.5000 0 0
0 0 0 0.5000 0.5000 0 -0.5000 -0.5000 0 0 0
py =
-2.0000 0 0 1.0000 1.0000 1.0000 1.0000 1.0000 0 0 0
-1.0000 0 0.5000 1.0000 1.0000 1.0000 1.0000 1.0000 0.5000 0 0
0 0.5000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.5000 0
0.5000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.5000
0.5000 0.5000 0.5000 0.5000 0.5000 1.0000 0.5000 0.5000 0.5000 0.5000 0.5000
0 0 0 0 0 0 0 0 0 0 0
-0.5000 -0.5000 -0.5000 -0.5000 -0.5000 -1.0000 -0.5000 -0.5000 -0.5000 -0.5000 -0.5000
-0.5000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -0.5000
0 -0.5000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -0.5000 0
0 0 -0.5000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -0.5000 0 0
0 0 0 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 0 0 0
vec3 looks like i entered.
But
Funnily, the -2|-2 gradient is also in the "upper left corner" of px and py.
However, when I look on to the the values of py, i am confused.
I have the feeling they are inversed.
On the quiver plot, the arrows show into the middle of the plot, from all directions (except the corner). Why,
e.g. py for -4,-3 = -0.5
(it is, absolutely seen, 1,3; one column to the right, three rows up, from the lower left)
I would have assumed the y-component of this vector would point upwards, not into the negative direction.
The whole lower half of the py-component is negative, altough vectors are showin up. And the upper half is reversed.
Why is that? I have the feeling i'm missing something. Is there some convention? Or might i have misunderstood some fundamentials here...
Thanks!

Respuestas (1)

Aditya
Aditya el 23 de Feb. de 2024
Hi s3bbo,
I understand that you're looking for a clear explanation as to why the output from MATLAB's gradient function may not match the expected orientation in the Cartesian coordinate system and how to correct this.
In MATLAB, matrices are displayed with the first row at the top, which is opposite to the Cartesian coordinate system where the y-axis increases as you move upwards. This difference can lead to confusion when interpreting the output of gradient vectors, as they may appear inverted on the y-axis compared to traditional Cartesian plots.
To address this issue, you can flip the matrix along its vertical axis before performing the gradient operation. This way, the matrix will be oriented correctly with respect to the Cartesian coordinate system, with the first row of the matrix corresponding to the lowest y-value (bottom of the plot) and increasing as you move upwards.
Here's the MATLAB code that flips the matrix vec3 using the flipud function:
% Flip the matrix upside down to align with the Cartesian coordinate system
vec3_cartesian = flipud(vec3);
% Now calculate the gradient
[px, py] = gradient(vec3_cartesian);
By using flipud(vec3), you create a new matrix vec3_cartesian that, when plotted, will have the same orientation as if it were plotted on a standard Cartesian plane. The gradient vectors calculated from vec3_cartesian will then be consistent with the Cartesian coordinate system, thus eliminating the confusion.
For more information on “flipud” function, refer to the MATLAB documentation here:
I hope this helps!

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by