For Loop Significantly Increases Run Time of Script
조회 수: 1 (최근 30일)
이전 댓글 표시
Hello again! For this ode solver, I am trying to tell it to solve a system of equations for every time step in my for loop. The ode solver "ode15s" easily solves the trajectory on its own, however once I add a for loop, the script runs "infinitely" (Many hours). When I force stop the script, I see the trajectory plotted so far, but it shouldn't be taking as long as it is. Any reason why the for loop causes it to take much longer to solve?
The ode solver can solve this iteration of the code (WO for loop) very quickly:
[X,Y,Z] = meshgrid(-0.1:.01:0.1,-0.1:.01:0.1,-0.1:.01:0.1);
[Bx, By, Bz] = B_test();
Bfieldx = arrayfun(Bx,X,Y,Z);
Bfieldy = arrayfun(By,X,Y,Z);
Bfieldz = arrayfun(Bz,X,Y,Z);
icv = [0.05; 0.05; 0.; -1.7585E+7; -1.7585E+7; 0.]; %Format: [x; y; z; vz; vy; vz]
%Time Span (sec)
opts = odeset('MaxStep',1E-10);
tspan = [0 1E-8];
[T,W] = ode15s(@bdipuniodefun, tspan, icv);
[rownum,colnum] = size(W);
plot3(W(:,1), W(:,2), W(:,3), '-r', 'LineWidth',2,'color',[0 1 0])
xlabel 'x';
ylabel 'y';
zlabel 'z';
grid on
hold on
quiver3(X,Y,Z,Bfieldx,Bfieldy,Bfieldz)
axis equal
xlabel 'x'
ylabel 'y'
zlabel 'z'
title('Quiver Plot of B-Field Solution')
hold off
However when I add the for loop in this version, this is where it slows down:
[X,Y,Z] = meshgrid(-0.1:0.01:0.1,-0.1:0.01:0.1,-0.1:0.01:0.1);
[Bx, By, Bz] = B_test();
Bfieldx = arrayfun(Bx,X,Y,Z);
Bfieldy = arrayfun(By,X,Y,Z);
Bfieldz = arrayfun(Bz,X,Y,Z);
hold on
quiver3(X,Y,Z,Bfieldx,Bfieldy,Bfieldz)
xlabel 'x in m'
ylabel 'y in m'
zlabel 'z in m'
title('Particle Trajectory Solution with Collisions Densities')
%Initial position and velocity
spart = [0.05 0.05 0.];
vpart = [-1.7585E+7 -1.7585E+7 0.];
opts = odeset('MaxStep',1E-10);
tstep = 1E-10;
tfin = 1E-7;
for t = 0:tstep:tfin
x = spart(1);
y = spart(2);
z = spart(3);
vx = vpart(1);
vy = vpart(2);
vz = vpart(3);
icv = [x; y; z; vx; vy; vz];
tspan = [0 tstep];
[T,W] = ode15s(@bdipuniodefun, tspan, icv);
plot3(W(:,1), W(:,2), W(:,3), '-r', 'LineWidth',2,'color',[randi(0:1) randi(0:1) randi(0:1)])
xlabel 'x';
ylabel 'y';
zlabel 'z';
grid on
vparts(1) = W(rownum,4);
vparts(2) = W(rownum,5);
vparts(3) = W(rownum,6);
sparts(1) = W(rownum,1);
sparts(2) = W(rownum,2);
sparts(3) = W(rownum,3);
end
hold off
댓글 수: 0
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!