How to plot matlab stream plot?

I have a 2D domain which has diffrent width(X) and length(Y) as attached photo. Say the height Y=0.05 and width X=0.02;
I have each particle position array in X,Y and I know the corresponding velocity of the particle in U and V.
If I create meashgrid say
[X1,Y1] = meshgrid(0:0.005:0.02,0:0.005:0.05);
Then how I create the dimension of U and V equal to the dimension of X1 and Y1? So that I can plot
streamline(X1,Y1,U,V)
Please help,Thanks.

 Risposta accettata

U1 = interp2(X, Y, U, X1, Y1);
V1 = interp2(X, Y, V, X1, Y1);
streamline(X1, Y1, U1, V1);

13 Commenti

However, what is the X and Y, U ,V and what are the array size ? because the array size of X1 and Y1 is not equal to X,Y,U,V. I guess the array size should be same to interpolate.
You wrote,
"I have each particle position array in X,Y and I know the corresponding velocity of the particle in U and V."
I deduced from that that X and Y and U and V are 2D arrays that are all the same size, and that what you are trying to do is resample the 2D arrays at locations X1, Y1 (same size as each other but different size than X, Y, U, V)
Do you instead have the case where X, Y, U, V are vectors of scattered data, and the question is how to interpolate the scattered data into a regular 2D array?
Because if so, see griddata()
@Walter Roberson Thanks, I am really sorry if I confused you. let me clear more specifically
Say X= [ 1 2 3 5 7 ] % all particle x position,dimension/array size (1 by 5)
Y= [ 2 8 3 6 7 ] % all particle y position,dimension/array size (1 by 5)
U= [5 4 3 1 5 ] % all particle x velocity,dimension/array size (1 by 5)
V= [3 6 3 2 8 ] % all particle y velocity,dimension/array size (1 by 5)
Okay, now assume that these 5 particles located within the above geometry i.e problem doman.
Now, How I will draw the streamlines ? Thanks
X = [ 1 2 3 5 7 ]/200;
Y = [ 2 8 3 6 7 ]/200;
U = [ 5 4 3 1 5 ];
V = [ 3 6 3 2 8 ];
[X1,Y1] = meshgrid(0:0.005:0.02,0:0.005:0.05);
U1 = griddata(X, Y, U, X1, Y1);
V1 = griddata(X, Y, V, X1, Y1);
streamline(X1, Y1, U1, V1)
Rubel Ahmed
Rubel Ahmed il 14 Lug 2021
Modificato: Walter Roberson il 14 Lug 2021
@Walter Roberson thanks, I have rewrite the code lime this
[X1,Y1] = meshgrid(0:0.000049:0.02,0:0.000049:0.1);
AXX= ARPP(:,1); % Prticles x position with array size n by 1
AYY= ARPP(:,2); % Prticles y position with array size n by 1
UXX= gather(TRPV(:,1)); % Prticles x velocity component with array size n by 1
UYY= gather(TRPV(:,2)); % Prticles y velocity component with array size n by 1
U1 = griddata(AXX,AYY,UXX, X1, Y1);
V1 = griddata(AXX,AYY,UYY, X1, Y1);
figure(25)
streamline(X1, Y1, U1, V1)
Ths plot is not creating any figure ven I am not getting any error. Do you know what is/are the possible reasons?
Walter Roberson
Walter Roberson il 14 Lug 2021
Modificato: Walter Roberson il 14 Lug 2021
I would check min(AXX), max(AXX) against 0 to 0.02, and min(AYY), max(AYY) against 0:0.1, and check nnz(isnan(U1)), nnz(isnan(V1))
And of course nnz(U1), nnz(V1) to check whether maybe all of U1 and V1 ended up 0 somehow.
I have checked nnz(U1), nnz(V1) . Seems like ok since only few value are zero.
min(AXX), max(AXX) against 0 to 0.02 is okay. Min(AXX)= 0 and max(AXX)=0.02, Also same for the other y axis. What is the point of checking nnz(isnan(U1)), nnz(isnan(V1)) ?
still getting no error with empty figure. What is/are the possible reasons? Thanks again.
Walter Roberson
Walter Roberson il 3 Ago 2021
Modificato: Walter Roberson il 3 Ago 2021
nnz(isnan(U1)), nnz(isnan(V1)) is to detect the case where U1 or V1 are nan, since in such cases MATLAB cannot plot for that location.
Is it possible for you to attach your data as a .mat for testing?
@Walter Roberson Thanks !!!
plz find the attached data. Hope you can help me with this.
Your data exists in a sort of an sidewise L shape near the boundaries, with a thin rise on the y axes that is about 1/40 of the thickness of the width you are trying to streamline; the bottom of the L is about 1/10 of the height you are trying to streamline and extends about 1/2 way across. Sort of like
* |
* |
* |
* |
************** |
************** |
Because of this, the streamline process spends a lot of time trying to interpolate NaN values. Only approximately 6% of the streamlines are non-nan, because most of where you are trying to plot is outside of where the data is defined for.
@Walter Roberson thanks. So, is it possible to plot the streamlines only on the bath domain i.e. lower bath domain where the bath is mostly filled with liquid.
Walter Roberson
Walter Roberson il 5 Ago 2021
Modificato: Walter Roberson il 5 Ago 2021
You can do a bit better by calling stream2() yourself, see the first example at
The idea is that you would filter down your coordinates. For example break up into two blocks, one the left area and the other the bottom area, so that you avoid doing all that interpolation for the large part of your plot that is empty.
It might take a bit of work to figure out where the useful boundaries are given only the scattered data. One approach would be a crude 2d histogram to find out which parts are occupied.
By the way, I just opened a case recommending specific performance improvements to streamline(). I doubt we will see them before 2022 however.
I can plot the quiver plot on the lower bath region where the geometry filled with fluids by using the griddata function as attached figures. When I am again trying to plot the streamline by using the same interpolated data(data that used to plot the quiver plot), still no improvment. Do you khowwhats wrong now? Since my program can avoided the empty space of the L shape. Also, is it possible to add lines on the quiver plot?
My code is
AXX= ARPP(:,1); % Prticles x position with array size n by 1
AYY= ARPP(:,2); % Prticles y position with array size n by 1
UXX= gather(TRPV(:,1)); % Prticles x velocity component with array size n by 1
UYY= gather(TRPV(:,2)); % Prticles y velocity component with array size n by 1
for i = 1:40000
for j = 1
if AYY(i,j)< 0.01
X(i,j)=AXX(i,j);
Y(i,j)=AYY(i,j);
XU(i,j)=UXX(i,j);
YU(i,j)=UYY(i,j);
else
X(i,j)=0;
Y(i,j)=0;
XU(i,j)=0;
YU(i,j)=0;
end
end
end
X = X(X~=0);
Y = Y(Y~=0);
XU = XU(XU~=0);
YU = YU(YU~=0);
[X1,Y1] = meshgrid(-6*PDis:0.0006:0.01,-6*PDis:0.0006:0.01); % Total geometry is divided into gridpoints
U1 = griddata(X,Y,XU, X1, Y1);
V1 = griddata(X,Y,YU, X1, Y1);
figure(10)
% streamline(X1, Y1, U1, V1)
quiver(X1, Y1, U1, V1)
pbaspect([1 1.2 1])

Accedi per commentare.

Più risposte (0)

Categorie

Prodotti

Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by