Iterations taking a long time
Mostra commenti meno recenti
I am trying to do particle tracking by updating the position at time t using at iterative process. Side by side, I wish to check if my y component of position is within a certain distance from defined surface's y value. If it is I want to stop the iterative process and plot only the points till now. I was running the iterative process without this 'if' statement and it took a while but gave me the right result. With the added if condition checking in the loop, my computation is taking too long (> 20 min). How do I make the code more efficient ? I need to keep the time step the same.
t = linspace(0,10,12000); % need to use 12000 time steps
xp = zeros();
yp = zeros();
yp(1) = 10*10^(-6); % setting y start point of particle (m)
x_val = linspace(-10*a,10*a,12000);
y_val = zeros(size(x_val));
y_val(1) = 10*10^(-6);
y0 = 10*10^-6;
i = 1;
d = 10*10^-6;
while i <= length(t)-1
h = t(i+1)-t(i);
xp(i+1) = xp(i) + vpa(vx(i+1),6)*h;% updating position( I haven't provided the code for computation of xp and yp as it is working fine)
yp(i+1) = yp(i) + vpa(vy(i+1),6)*h;
psi = 0;
x = x_val(i+1);
f = @(y) psi - norm(vf0)*y + m*atan2(2*a*y,x^2+y^2-a^2);
y_val(i+1) = fzero(f,y0); %obtaining the y value for the surface
y0 = y_val(i+1);
if abs(yp(i+1)-y_val(i+1))<= d/2 %checking if the difference between particle's y position value and surface y value at the same point is less than a threshold
i = length(t)-1; %attempt to end computation by making loop condition false
end
i=i+1;
end
4 Commenti
Walter Roberson
il 14 Giu 2020
Why not just use break if you want to end the computation ?
Sanjana Singh
il 14 Giu 2020
Walter Roberson
il 14 Giu 2020
We will need the other functions in order to be able to test the code. And we will need the initialization, such as of h
Bjorn Gustavsson
il 14 Giu 2020
Maybe you could use some of the built-in ode-integrating functions (ode23, ode45, ode15s etc) to integrate your
equations of motion. For handling of events (such as colliding with a surface) have a look at ballode and the help and documentation for that function.
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Programming in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!