How to connect nested while function to calculate particle movement?

1 view (last 30 days)
I have a 3-d matrix of u-comp velocity uuuu(x,y,t) and v-comp velocity vvvv(x,y,t) with 260x350x10. x is horizontal grid (grid x), y is vertical grid (grid y), t is time. I would like to calculate particle movement from the data uu and vv by using the formula lagrangian, defined as X(i)=X0+U*dt . X(i) is the particle movement toward times, X0 is initial particle position or previous position, U is velocity at the distance a from grid (U=u1+(u2-u1*a/dx)), dx is the distace for every grid (1000 m), and dt is the time step of the model. I set initial time (t=1) and t-end (tt=192). I also set every grid size into 1000 scale, since the grid from model is 1 km -> m.
1) I make the meshgrid to set the initial condition of X0,Y0 as the reference position of the particle. I choose the X0,Y0 at the distance 165000 m (at x direction) and 153000 m (at y-direction).
2) I would like to calculate the distance X(i) and Y(i) for the next time step untill the end. However, in every changing of X(i) and Y(i) that make the position of particle is in another grid location, I would like the U value is calculated from the u,v value in this grid. So that, I use nested while function here to apply two condition of the calculation. First condition, I would like to run while t<=tt, it will calculate the position of initial X0,Y0 and indexing the u2 and u1 at grid X0,Y0. Second condition, when the position X0,Y0 is changing postion into position X(i),Y(i), I would like to calculate the U by indexing the u2 and u1 at grid X(i),Y(i).
But, I found the second while function here is not connect to the first while function. So that, is there any another solution should I apply? I would appreciate if there any expertist to solve my problem. Thank you
Remark for attachement:
1) mesh grid data, assuming the whole grid data of uu and vv as x,y grid
2) 1-grid data, assuming the one component grid data contain u,v
  3 Comments
Anom Sulardi
Anom Sulardi on 29 Jun 2020
Hi Darova,
I am sorry for my complex cases. After I try to figure it out. Here is my simple problem ones.
I have a=6.5, I would like to define "if function" inside the "for loop", for i=1:10, it will do the loop imin < a < imax, and if the "if function" is correct, I would like to use the b= imin (in which the a function is correct).
My expectation toward the code is b=6. Since the 6.5 is in between number for loop 6 and 7. And I want to use 6 (imin where the a is in correct statemen for if function)
How do I code that in matlab?
a=6.5;
for i=1:10
imin=i;
imax=imin+1
if imin<a<imax
b=imin;
end
if imax==10;
end
end

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!